MRTKv2 'Interactable' script for dynamically added gameobjects - unity3d

So I want to be able to use the 'Interactable' script in MRTKv2 for some of my gameobjects. I've had success when attaching the 'Interactable' and 'NearInteractionTouchable' script to the corresponding gameobject, but not when attempting to add the same functionality dynamically in the script.
Since the 'Microsoft.MixedReality.Toolkit.UI' isn't currently defined, I know I can't reference the 'Interactable' script in the script directly, but in using the 'IMixedRealityFocusHandler', 'IMixedRealityPointerHandler', 'IMixedRealityTouchHandler', and 'IMixedRealityInputHandler' interfaces instead I still haven't had success in receiving any input.
The namespaces currently defined by MRTKv2
All of the namespaces in MRTKv2
Event mapping from HL1 to HL2
Any ideas on a step that I'm missing to recreate the functionality of the 'Interactable' script that can be attached to gameobjects dynamically?

Interactable does exist in the MRTK namespace under using Microsoft.MixedReality.Toolkit.UI; perhaps you are missing references to your projects? The following code works for me, using the latest mrtk_development:
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.UI;
using UnityEngine;
public class AddInteractableTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
gameObject.AddComponent<Interactable>();
gameObject.AddComponent<NearInteractionTouchable>();
}
}

Related

I can't attach scripts to a GameObject Unity 2020.3.33f1 [duplicate]

This question already has answers here:
Can't add script component because the script class cannot be found?
(12 answers)
Closed 6 months ago.
Whenever I try to attach a script to a game object ( whether via dragging or add Component ) It always returns this error saying, "Can't add script component'scipt' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match." Anyone know how to solve this problem?
You checked editor console? No error logs? If yes attach console log with errors here
Check name your script, one of the classes in this script must named by file name
Example: MyClass.cs contains public class MyClass : MonoBehaviour
1.Your Class should inherit MonoBehaviour, otherwise it can't attach to a GameObject.
2.The file name should be same as the class name
Here's an example:
using UnityEngine;
public MyClass:MonoBehaviour
{
void Start(){}
void Update(){}
}

Assign UIElements button.clickable, using Visual Scripting Graph

I am trying to use Unity3D's UIToolkit with Visual Scripting Graph...
Typically, I can Query for the proper element, and then add the necessary function to the callback... However, as I am new to Visual Scripting, I am not sure how to get to this point. I've got the 'clickable' object of the button isolated, but I'm not sure how to assign the follow-up execution in the graph.
Usually in the code, I would do something like
clickable.clicked += ExampleFunction();
What I am messing up in the graph, is how to access the '.clicked' part. I can get the proper button element, and I can isolate it's clickable property, but I can't figure out how to assign some kind of functionality to react to the button getting clicked.
I could use a custom node to make this work, but I am looking for a way to do this with built-in nodes, if possible.
Alright... I had to write a custom node, but I figured this out. Here is the graph for the solution.
You have to grab the UIDocument from whichever GameObject it is attached to... You then need to get the Root Visual Element, do NOT clone or instantiate it. You then need to Query for the desired button, using the name you gave it in the UI Builder. It is easier if you use the U Query Extensions nodes... After that, I just made a custom node to subscribe the functionality. I am not familiar of any nodes that do this.
Here is the 'Subscribe Start Result' node code:
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
public class SubscribeStartResult : Unit
{
[DoNotSerialize]
[PortLabelHidden]
public ControlInput inputTrigger;
[DoNotSerialize]
[PortLabelHidden]
public ValueInput element;
protected override void Definition()
{
element = ValueInput<Button>("element");
inputTrigger = ControlInput("inputTrigger", (flow) =>
{
flow.GetValue<Button>(element).clicked += () =>
{
Debug.Log("Button clicked");
};
return null;
});
}
}
With this setup, clicking the 'Start Button' in play-mode will log "Button clicked" in the Console.
The 'return null;' line is an artifact of the lambda. It is required to continue the control flow in the event this node has a follow-up... Otherwise, this combination of nodes and code allow you to assign callbacks for the UI Builder elements, using the Visual Scripting Graph.

MRTK - can't get the value from UI_KeyboardInputField object

Hello i am using the MRTK 2.7.3 and I am trying the read the value from a UI_KeyboardInputField object but seems something is wrong on my code
using Microsoft.MixedReality.Toolkit.Experimental.UI;
public void onSubmit()
{
GameObject username = GameObject.Find("Username");
UI_KeyboardInputField inputUsername = username.GetComponent<UI_KeyboardInputField>();
Debug.Log("username" + inputUsername.Text); <<<ERROR
}
'UI_KeyboardInputField' does not contain a definition for 'Text' and no accessible extension method 'Text' accepting a first argument of type 'UI_KeyboardInputField' could be found (are you missing a using directive or an assembly reference?)
Maybe my approach is wrong how can i get the text value from this field ?
thanks
From UI_KeyboardInputField
Only used in Unity 2018.4. No longer used in Unity 2019.3 and later versions (becomes an empty MonoBehaviour and is only around for compatibility) and you can safely remove it if you wish
And also
A component that can be added to InputField to make it work with Windows Mixed Reality's system keyboard.
Sounds to me like this is/was only a helper component for forwarding the virtual keyboard input and the actual thing is all happening in a usual InputField component attached to the same GameObject and you rather want to access the .text of that one.

MRTK V2.2 - Access Speech Command via Script

In my scenario, buttons are created during runtime. These are to be clicked by a voice command. For this reason I try to find out how I can add voice commands during runtime. But I can't find any approach.
What I tried:
I have extended the interface IMixedRealitySpeechSystem with two methods, RefreshRecognition and AddSpeechCommand:
/// <summary>
/// Refresh recognition after adding new commands
/// </summary>
void RefreshRecognition();
/// <summary>
/// Add command to already existing commands[]
/// </summary>
/// <param name="command"></param>
void AddSpeechCommand(SpeechCommands command);
I have implemented these in the class WindowsSpeechInputProvider: MixedRealitySpeechSystem. But there are two problems.
First: I can't get to the WindowsSpeechInputProvider. I thought I could get it by trying this:
private IMixedRealitySpeechSystem SpeechSystem
{
get
{
if(_speechSystem is null)
{
MixedRealityServiceRegistry.TryGetService(out _speechSystem);
}
return _speechSystem;
}
}
public void SomeMethod()
{
SpeechCommands command = new SpeechCommands("TestCommand", default, default, null);
SpeechSystem.AddSpeechCommand(command);
SpeechSystem.RefreshRecognition();
}
But the problem is that MixedRealityServiceRegistry does not contain an instance of that service or to be precise, it is not even a service.
Second: Even if this would work, it is not a good way to go. Because with this I change the MRTK and with another upgrade to a new version, these lines are overwritten.
My Question:
So how can I access and add commands on runtime?
There's an open feature request to allow adding dynamic speech commands in Github: Add keywords dynamically to MRTK speech commands #6369. It is not currently possible.
This thread has some suggestions for alternative ways to approach the overall scenario. In summary, it is recommended that you use a Grammar Recognizer and use an SRGS XML file to define your speech recognition rules. Voice input in Unity and Hologram 212 has an example showing how to use it.

Unity's IPreprocessBuildWithReport.OnPreprocessBuild throws an error

I got a tiny script that creates a text file in Resources folder before building that needs to be included in the build. So I wrote this script using Unity's IPreprocessBuildWithReport:
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using System.IO;
class MyCustomBuildProcessor : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
File.WriteAllText(
Application.dataPath + "/Resources/version.txt",
string.Format("{0}", PlayerSettings.Android.bundleVersionCode));
AssetDatabase.Refresh();
}
}
Note the AssetDatabase.Refresh(); statement at the end. It makes sure Unity becomes aware of the change and includes the updated file in the build. Now, whenever time I change the bundleVersionCode and press build, Unity updates the file as expected but the build fails with this pretty generic error:
However, if I try to build the second time with the same bundleVersionCode than no file changes occur and the build succeeds.
So I guess the AssetDatabase.Refresh() doesn't really work in OnPreprocessBuild(), or am I doing something obviously stupid again? Can anyone suggest a workaround?
EDIT:
Please ignore the first two errors, just Unity doesn't like me when I'm excluding files from build by the ~ postfix.