The Mixed Reality Forums here are no longer being used or maintained.
There are a few other places we would like to direct you to for support, both from Microsoft and from the community.
The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.
For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.
If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.
And always feel free to hit us up on Twitter @MxdRealityDev.
Origami Tutorial Gaze Gesture Manager : help deconstructing Start() script
Hello all (and hopefully @Alex ),
I have the Origami tutorial working but I'm not able to wrap my head around whats happening in the Start() function of the Gaze Gesture Manager script.
I have a few questions that I feel will help portray what I'm struggling with.
Appreciate your help.
Q: How is public GameObject FocusedObject being used
Q:How did Unity manage to Divine if FocusedObject was Null or not (it is not being explicitly set afaik)
Q:What happens when the script runs (via Start() ) the first time
Q: How is instance = This being used
Thank you so much for any help.
Cheers,
` using UnityEngine;
using UnityEngine.VR.WSA.Input;
public class GazeGestureManager : MonoBehaviour { public static GazeGestureManager Instance { get; private set; } // Represents the hologram that is currently being gazed at. public GameObject FocusedObject { get; private set; } GestureRecognizer recognizer; // Use this for initialization void Start() { Instance = this; // Set up a GestureRecognizer to detect Select gestures. recognizer = new GestureRecognizer(); recognizer.TappedEvent += (source, tapCount, ray) => { // Send an OnSelect message to the focused object and its ancestors. if (FocusedObject != null) { FocusedObject.SendMessageUpwards("OnSelect"); } }; recognizer.StartCapturingGestures(); } // Update is called once per frame void Update() { // I think I understand this part } }
`
Best Answers
-
Options
neerajwadhwa mod
Q: How is public GameObject FocusedObject being used
A: This FocusedObject gets updated in the Update() function.
When the user is gazing at a hologram and the Raycast hit's it, we save it FocusedObject = hitInfo.collider.gameObject;This way your gesture will always happen on the object being gazed at.
Update gets called immediately after Start(). You can read more about these on the Unity docs and get more familiar with the sequence of calls. Awake, then Start then Update.Q:How did Unity manage to Divine if FocusedObject was Null or not (it is not being explicitly set afaik)
A: It is being set in the Update() function as explained above. We just sign up for the TappedEvent handler like an anonymous delegate / lambda function so when the tap is detected that code inside the braces gets executed.Q:What happens when the script runs (via Start() ) the first time
A: Hopefully this should be clear now?Q: How is instance = This being used
A: This is a typical singleton style pattern. It makes it easier for other code classes to access public properties or variables of a particular class.Please let us know if you have any other doubts. Hope this helps!
6 -
Options
neerajwadhwa mod
4.Since this is the first time the script is being run FocusedObject is indeed Null ?
5.Therefore SendMessageUpwards("OnSelect") is never executed !Correction here is, since an Air Tap was not detected so that code never got executed during Start. It will get executed only when an Air Tap is detected and then if the focused object is not null we will SendMessageUpwards.
5
Answers
Q: How is public GameObject FocusedObject being used
A: This FocusedObject gets updated in the Update() function.
When the user is gazing at a hologram and the Raycast hit's it, we save it FocusedObject = hitInfo.collider.gameObject;
This way your gesture will always happen on the object being gazed at.
Update gets called immediately after Start(). You can read more about these on the Unity docs and get more familiar with the sequence of calls. Awake, then Start then Update.
Q:How did Unity manage to Divine if FocusedObject was Null or not (it is not being explicitly set afaik)
A: It is being set in the Update() function as explained above. We just sign up for the TappedEvent handler like an anonymous delegate / lambda function so when the tap is detected that code inside the braces gets executed.
Q:What happens when the script runs (via Start() ) the first time
A: Hopefully this should be clear now?
Q: How is instance = This being used
A: This is a typical singleton style pattern. It makes it easier for other code classes to access public properties or variables of a particular class.
Please let us know if you have any other doubts. Hope this helps!
Update_1:
Ok so my misunderstanding was that anything setup in the Start function happens once and only once. Wrong !
The TappedEvent Event Handler is only set in motion in the Start function and persists even after the Start function.Which is what @neerajwadhwa was trying to get across above.
The penny dropped when the folks at Unity chimed in.
http://forum.unity3d.com/threads/origami-tutorial-gaze-gesture-manager-help-deconstructing-void-start-script.412459/
End Update_1
Thanks for the reply Neeraj,
I will write my understanding of what I think is happening and hopefully you can correct any misconception / ignorance on my part. I think my understanding of Events and how unity handles them is a bit lacking.
1.When the game begins and Unity runs the Gaze Gesture Manager script
2. public GameObject FocusedObject variable is declared (which defaults to Null ?)
3. Since there is no Awake function, the Start function is executed once and "only" once during the lifetime of the Program.
3. in the event handler we check for FocusedObject to have a valid value (i.e not Null)
4. Since this is the first time the script is being run FocusedObject is indeed Null ?
5. Therefore SendMessageUpwards("OnSelect") is never executed !
That is what I'm having trouble understanding.
Can we try to deconstruct the process once more ?
Thanks,
b
4.Since this is the first time the script is being run FocusedObject is indeed Null ?
5.Therefore SendMessageUpwards("OnSelect") is never executed !
Correction here is, since an Air Tap was not detected so that code never got executed during Start. It will get executed only when an Air Tap is detected and then if the focused object is not null we will SendMessageUpwards.
Hi guys,
I'm also having a bit of trouble figuring out the start script. This post has been very helpful, thank you for the answers @neerajwadhwa . I have more of my own however... mostly I just don't understand why recognizer.StartCapturingGestures() is after the recognizer.TappedEvent. Obviously it works this way, I know it's correct, but thinking it through with my understanding as it is now, I would think that the GestureRecognizer would need to "start capturing gestures" before it can be aware of a tapped event happening... if that makes sense...
Does the line " recognizer.TappedEvent += (source, tapCount, ray) =>" just set up what will happen WHEN the GestureRecognizer captures a tapped event, and then the line "recognizer.StartCapturingGestures();" actually tells the GestureRecognizer to begin to watch out for gestures? Or am I completely off base here?
Thanks so much for any help!!
@ckesterson I think about it this way:
Why start capturing the gestures without having something that actually handles that gesture. Hence I think the order of register and capture makes sense to me.
That makes a lot of sense when you put it that way. Thank you so much for your help!