Can't change text on hololens - c#

I just started using a Hololens and have been messing around with the voice recognition scripts from the tutorials on the Microsoft website.I have been using unity to create 3D texts, however, if I try to change the text using voice commands it does not work!
This is my following code :
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.Speech;
using UnityEngine.UI;
public class VoiceRecognized : MonoBehaviour {
KeywordRecognizer kr = null;
Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
// Use this for initialization
public Text name_text;
private string testName = "Drop";
void begin () {
keywords.Add("Change", () =>
{
this.BroadcastMessage("OnReset");
});
kr = new KeywordRecognizer(keywords.Keys.ToArray());
kr.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
kr.Start();
}
public void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
System.Action keywordAction;
if (keywords.TryGetValue(args.text, out keywordAction))
{
keywordAction.Invoke();
}
}
// Update is called once per frame
void Update () {
}
}
The other program that changes the text upon hearing "Change".
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class ChangeText : MonoBehaviour {
public Text name_text;
void Start()
{
}
void OnReset()
{
name_text.text = "Change";
}
}
In this code, I am using voice recognition script to change the initial text "Hello World" to "Change". I am not sure if there a problem with my Unity, however I am currently following everything Microsoft is doing with their tutorials.

Try invoking same method on button click, see if that works. You can use Debug.Log to see if you method gets called with voice command.

Related

Netcode For Gameobjects - How to allow the user to put in a username?

So i have followed the tutorial series from Dilmer Valecillos on how to use Netcode and got it working with friends and all! I wanted to add a simple Inputfield in the menu to type in your username, and then apply that to the username logic the video had. However, whenever i do this, the host sees everyone as there own username, and so do the clients.
PlayerHud.CS
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Netcode;
using UnityEngine;
public class PlayerHud : NetworkBehaviour
{
private NetworkVariable<NetworkString> playersName = new NetworkVariable<NetworkString>();
private TMP_InputField nameInput;
private NetworkManagerUI uimanager;
private bool overlaySet = false;
public override void OnNetworkSpawn()
{
if (IsServer)
{
uimanager = GameObject.FindObjectOfType<NetworkManagerUI>();
playersName.Value = uimanager.username.Value + $" ({OwnerClientId})";
}
}
public void SetOverlay()
{
var localPlayerOverlay = gameObject.GetComponentInChildren<TMP_Text>();
localPlayerOverlay.text = playersName.Value;
}
private void Update()
{
if (!overlaySet && !string.IsNullOrEmpty(playersName.Value))
{
SetOverlay();
overlaySet = true;
}
}
}
Any idea how to fix this?
Tried alot of PlayerPrefs, input etc but did not work

I tried scriptable object as an input reader from input actions script but it won't work for some reason

i'm trying to invoke input events from a scriptable object as a channel or a layer between the input action script and the player like what unity does in its open project "chop chop" but it is not working at all with no errors or warnings . i don't know why ? can u help me please ? (using the new input system) this is the code for the channel :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.InputSystem;
[CreateAssetMenu(fileName ="testScriptableObject", menuName ="game/test SO")]
public class InputSOChannel : ScriptableObject
{
public event Action playerFire;
private TestControls testControl;
private void OnEnable() {
testControl = new TestControls();
testControl.test.fire.started += onfire;
}
private void onfire(InputAction.CallbackContext context)
{
playerFire?.Invoke();
Debug.Log("the event invoked!");
}
private void OnDisable() {
testControl.test.fire.started -= onfire;
}
}
and this for the player or input receiver :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectController : MonoBehaviour
{
[SerializeField] InputSOChannel inputSOChannel;
private void OnEnable() {
inputSOChannel.playerFire += isfiring;
}
private void OnDisable() {
inputSOChannel.playerFire -= isfiring;
}
private void isfiring()
{
Debug.Log("the event fired from the lowend!");
}
}
i really appreciate any help . thank you!
i just forgot to enable the input action script , beginner's mistake

Change function within unity inspector

I have a gameObject that needs to call lots of other functions. I was hoping to do something like this
Where I can select the gameobject and script for it to run. I can't figure out how to do this.
I'm calling a function on button click for a gui.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class AvatarSystem : MonoBehaviour
{
public Button general;
public Button face;
public Button hair;
public Button eyebrows;
public Button clothing;
// Start is called before the first frame update
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
general = root.Q<Button>("general");
face = root.Q<Button>("face");
hair = root.Q<Button>("hair");
eyebrows = root.Q<Button>("eyebrows");
clothing = root.Q<Button>("clothing");
general.clicked += generalClicked;
face.clicked += faceClicked;
hair.clicked += hairClicked;
eyebrows.clicked += eyebrowsClicked;
clothing.clicked += clothingClicked;
}
void generalClicked()
{
}
void faceClicked()
{
}
void hairClicked()
{
}
void eyebrowsClicked()
{
}
void clothingClicked()
{
}
}
The 5 bottom functions should do something like the top image. Thanks
You can this script which creates buttons in the inspector.
This should be outside of your AvatarSystem class.
using Unity.Editor;
[CustomEditor(typeof(AvatarSystem))]
public class AvatarSystemGUI : Editor
{
public override void OnInspectorGUI()
{
AvatarSystem t = AvatarSystem(target);
if (GUILayout.Button("Test"))
{
t.ExampleMethod();
}
}
}
Let me know of any problems! :)
You want to use a Unity Event
You can use UnityEvent<T> to pass a dynamic type in your function.
Then you can use Unity IPointerClickHandler to capture a click on the object.
You can use AddListener of UnityEvent;
you can write the code like this;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class AvatarSystem : MonoBehaviour
{
public Button general;
public Button face;
public Button hair;
public Button eyebrows;
public Button clothing;
// Start is called before the first frame update
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
general = root.Q<Button>("general");
face = root.Q<Button>("face");
hair = root.Q<Button>("hair");
eyebrows = root.Q<Button>("eyebrows");
clothing = root.Q<Button>("clothing");
general.onClick.AddListener(() => {
this.generalClicked();
this.faceClicked();
this.hairClicked();
this.eyebrowsClicked();
this.clothingClicked();
}) ;
}
void generalClicked()
{
}
void faceClicked()
{
}
void hairClicked()
{
}
void eyebrowsClicked()
{
}
void clothingClicked()
{
}
}

How to delete input binding in input system unity

I have an issue to delete bindings in input system unity.
I know how to rebind and add binding with code but can't delete bindings with code.
You can remove an input binding from an input action using one of these methods:
action.ChangeBinding(...).Erase();
action.ChangeBindingWithId(...).Erase();
action.ChangeBindingWithPath(...).Erase();
action.ChangeBindingWithGroup(...).Erase();
Example
using UnityEngine;
using UnityEngine.InputSystem;
public class TestScript : MonoBehaviour
{
private InputAction action = new InputAction();
private void Start()
{
action.AddBinding("<Keyboard>/#(a)");
action.performed += OnActionPerformed;
action.Enable();
}
private void OnActionPerformed(InputAction.CallbackContext e)
{
Debug.Log($"Action performed: {e.control.name}");
action.ChangeBinding(0).Erase();
}
}

Not registering when the trigger is pressed

i have some code, that when the trigger of the right hand controller is pressed, it shoots a gun, however it doesn't appear to be working, i've tried to add a debug into the actual TriggerPressed section, and that doesn't show up in the log when i click it in game either, im just not sure where this problem is orginating from, and im stumped as to why it isn't working. It also compiles, so any guidance is appreciated
Edit: Not sure of its importance but decided to add this anyway, im using oculus rift controller's
Here's the (edit: UPDATED) code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EZEffects;
public class adamadam : MonoBehaviour
{
public SteamVR_TrackedController controllerRight;
private SteamVR_TrackedObject trackedObj;
private SteamVR_Controller.Device device;
private SteamVR_TrackedController controller;
public EffectTracer TracerEffect;
public Transform muzzleTransform;
// Use this for initialization
void Start()
{
// controller = controllerRight.GetComponent<SteamVR_TrackedController>();
controller.TriggerClicked += TriggerPressed;
// trackedObj = controllerRight.GetComponent<SteamVR_TrackedObject>();
}
private void TriggerPressed(object sender, ClickedEventArgs e)
{
Debug.Log("Clicked");
ShootWeapon();
}
public void ShootWeapon()
{
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(muzzleTransform.position, muzzleTransform.forward);
device = SteamVR_Controller.Input((int)trackedObj.index);
device.TriggerHapticPulse(750);
TracerEffect.ShowTracerEffect(muzzleTransform.position,
muzzleTransform.forward, 250f);
if (Physics.Raycast(ray, out hit, 5000f))
{
if (hit.collider.attachedRigidbody)
{
Debug.Log("Hit" + hit.collider.gameObject.name);
}
}
}
}

Categories

Resources