I am currently working on some Unity VR project and I have a problem with gaze functions. As an example I will use Google VR DemoScene object named Cube. There is a Teleport script attached with some gaze code at the end:
public void OnGazeEnter() {
SetGazedAt(true);
}
public void OnGazeExit() {
SetGazedAt(false);
}
public void OnGazeTrigger() {
TeleportRandomly();
}
However this fragment seems to be useless and all gaze events are handled by EventTrigger component attached to the Cube object.
My question is - how do you handle gaze events (OnGazeEnter, OnGazeExit, OnGazeTrigger) with this code only? It would be way simpler not to attach EventTrigger component all the time.
They are not useless, just not very well documented how to use it. It tooks me a while for me to figure out how to.
If you want to use them, what you need to do is remove the EvenTrigger component, then select the Main Camera and Add the component called "GVRGaze" and now these events are fired by code (OnGazeEnter, OnGazeExit, OnGazeTrigger)
You can use the mask to filter layers if you want, this is very helpful.
Hope this helps you.
Related
Is there a way to override how Vectors are displayed in the inspector ?
Let's say I want to add a button next to the vector fields, can I override the "Property Drawer" of Vector3 or should I create a new class/struct and apply a CustomPropertyDrawer on it ?
I've tried this, but it doesn't work.
[CustomPropertyDrawer(typeof(Vector2))]
public class Vector2Drawer : PropertyDrawer {
// overrides functions, but does nothing
}
I'd use the public Vector2 and a public bool for the button status for the sake of simplicity.
If you really want to achieve your editor custom behaviour then you need to check the unity Editor class.
With that you can use that class that is within unity itself and you can set up your own custom editor UI that communicate with the rest of the code of your application.
You can check regarding editor scripting: https://learn.unity.com/tutorial/editor-scripting#.
To add a button specifically check: Add button to Unity3d editor
As you can see the editor scripting is a disciplice of its own. I think its should be used in teams when a complex component will be used by a lots of people repeatedly soas to simplify its use.
I can understand that can be juicy to dive in if you are interested in learning unity deeply, but if you are building a game or have a launch of a product in mind, I would suggest to move on and simplify your editor setttings as much as possible and move on with your game. Take into account that it takes quite an effort to elaborate this cool UI component, and you might give up on launching your product by the time you got them as you'd like.
Hope that helps :)
Basically, In my scene, underneath a GameObject called 'GameOverUI' I have some buttons and a Panel that covers the screen. I have made a simple animation where the panel's opacity (The alpha channel) increases. Do any of you know how to make it so that when the player dies, it enables GameOverUI and plays the animation for the panel once?
Edit: Forgot to mention, I know how to make it so that 'GameOverUI' is enabled, I just don't know how to make the animation play
If you are looking for a solution with animations, then here you go:
Create a script for your GameOverUI gameObject. Really simple:
public class UIHandler : MonoBehaviour
{
private void OnEnable()
{
//play "dead screen" animation
}
}
Method OnEnable() is a MonoBehavior function, and will be called when you enable a GameObject by calling myGameObject.SetActive(true);.
However, I would recommend thinking about another solution. I usually leave GameOverUI always active and use it to manage its children via script. So I think it would be more elegant to write something like this:
public class UIHandler : MonoBehaviour
{
public void PlayerDied()
{
//play "dead screen" animation
}
}
The difference is, that instead of enabling and disabling the GameObject, you call a method. This way, you will be able to pass data (as function parameters) if you need to do so. For example, you can write to the screen, what caused the death. And further on, the GameObject will be able to manage its UI components for other purposes.
I hope it makes sense and I could be of your help!
Currently when I am trying to detect whether an object has been clicked / interacted with by the mouse, I create a script on that GameObject and then define void OnMouseDown() and apply functionality within. If I then want to trigger a function in another script that is not attached to the GameObject I call it through a GetComponent<>() reference.
Is there a way to add a callback to the OnMouseDown event from a script on another GameObject? Potentially in the same way delegates / events are allocated and invoked? Something such as: gameObjectReference.GetComponent<BoxCollider>().onmousedownevent += MyFunction; So that it automatically calls this function on mouse down?
Note: I know that you can use raycasting to achieve something similar, but my question is about attaching callbacks to the existing Unity event.
1) I understand the sense of what you mean by this
pseudocode:
gameObjectReference.GetComponent<BoxCollider>().onmousedownevent += MyFunction
and there is no way to do that.
2) please do understand: it is incredibly easy to achieve your aim (elegantly). Add a UnityEvent in script "b", Invoke it in the OnMouseDown. drag anything you want there. For anyone reading who is new to Unity, pls try UnityEvent in general as it is used constantly
3) A I mention you can kind of do what you want by fooling around with the OnPointerDown(PointerEventData eventData) systems, but this just emphasises you can't do what you ask, do it with OnMouseDown.
Sorry for the grim news.
I made an animation using Animation panel, which swaps images from time to time. Read from the Internet, this is not a legacy animation.
Here is the Animation panel screenshot:
Then, I add the Animation and Animator components to the Game Object and assign the animation, which is called Animation01, to it. Here is the screenshot from Inspector of the Game Object:
I try to use the following C# script to stop the animation :
using UnityEngine;
using System.Collections;
public class Scene1 : MonoBehaviour {
public GameObject ball;
// Use this for initialization
void Start () {
ball.animation.Stop();
}
// Update is called once per frame
void Update () {
}
}
but the animation didn't stop. It prompts a notice in Console:
Default clip could not be found in attached animations list.
What did I miss?
UPDATE: By disabling/ removing Animator component, the animation is stopped and cannot be controlled by codes. I need to control the animation by codes.
Unity had implemented two animation systems throughout it's history. Animation component belongs to the legacy animation system, while Animator component belongs to the new animation system. So, adding both components makes no sense: you either use the old system or the new.
The main difference between the legacy and new animation system is that the new animation system is much more advanced in way it's controlled. However, it also means that it's more complicated. It adds another level of abstraction: instead of launching animation yourself, you control variables that influence the behaviour of a special state machine, animation controller.
So, if you want to use animations for something really, really simple, where you want just to launch animations manually, it may be better to use legacy animation system instead. But the components are not the only thing that is different: the animation files themselves are marked to determine if they are "legacy" or not. By default, when you create an animation in the new unity version, it belongs to the new animation system. If you want to use it with a legacy animation, you have to mark it as a legacy animation. Unfortunately, you'll have to do a little hack to achieve that.
is there anyway to draw stuff on scene view without calling OnSceneGUI?
or is there a way to get OnSceneGUI to get called even if the object with the script attached is not selected?
Edit: Looks like I wasn't explicit enough on what I was asking for so here's a little update:
I have a whole bunch of controls shown as GUI objects on my Game Scene that are used by the game designers to more easily test the game. Since these tools are for development use rather than deployment, I wanted to move these to the scene window instead.
Unfortunately I am only able to display these GUI tools if the object that contains the script with the "OnSceneGUI" method is selected. Instead I want them to be displayed whenever the game is running and regardless of the selected object.
I have found the solution to my issue.
If you want your GUI stuff to always be displayed on the scene window you can do the following:
public class MyClass{
static MyStaticConstructor() {
SceneView.onSceneGUIDelegate += OnScene;
}
static void OnScene(SceneView sceneView) {
// Draw GUI stuff here for Scene window display
}
}
This code will run as soon as you press play and will draw whatever you wish on your scene window.
Hope it will help others!
There are many ways to use GUI,
The easiest but least efficient way is using the GUI class on OnGUI()
GUI.Label(new Rect(10,10,10,10), "This is a label");
GUI.Label(new Rect(10,10,10,10), "Your Texture2D here");
Any active monobehaviour will run OnGUI() if its defined. So it can be attached to any gameObject. You can create an empty gameObject in the scene and call it "GuiGameObject" for example and attach the script there. That way it wont be mixed in with your gameplay script.
There are also GUI textures --> More Info on GUITexture
Also I recommend checking out nGUI
Edit:
For OnSceneGUI You can try Editor.Repaint, you can use it to make sure that the inspector updates changes made inside of OnSceneGUI