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
Related
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!
So I have an animation that fades into the menu screen but after the animation ends none of my buttons work. I have figured out that it is because the GameObject that holds the black image that fades to clear is always in the front, blocking me from using any of the buttons. I tried to write a script, that's attached to the game object, that disables the GameObject after it completes the animation, but it isn't working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelChanger : MonoBehaviour
{
public Animation anim;
public void SetTrigger()
{
this.StartCoroutine(this.PerformAnimRoutine());
}
private IEnumerator PerformAnimRoutine()
{
var state = anim.PlayQueued("Fade_In", QueueMode.PlayNow, PlayMode.StopSameLayer);
yield return new WaitForSeconds(state.length);
this.gameObject.SetActive(false);
}
}
Is there anything wrong with the code or is there an easier way to accomplish this? I am extremely new to unity so I am very stuck.
If all you are doing is fading a sprite to clear and you seem to know about coroutines, I might start by suggesting you do the fade within a coroutine instead.
Have that decrease the alpha by some fraction each frame and when it's 0 disable the object.
That's just if that sounds more fluid, nothing wrong with the animation way.
Doing it with animations though :
I'm not confident that you can disable the object the animation is on in that animation. If it is available on the dope sheet try that. Otherwise we can use state behaviours or animation events.
Animation Events
These can be used to trigger a function at a certain point of an animation. You can create them similar to keyframes. Here is a link to Unity's guide on this topic.
All you'd need to do is create an event and place it at the end of the animation. Then you need to in a script of that object make a public function that simply disables the object. Call that with the event.
State Behaviours
State Machine Behaviours allow you to define a script to run on a given animation state. It has many functions to hook onto such as OnStateEnter and OnStateExit.
You'd want to click on the state that fades in the animator. In the inspector you should be able to click "Add Behaviour". This will create a script that you can open and edit. Here is the reference for that class.
From there is should be very simple to disable the object through OnStateExit.
hello as shown in the picture when the game started in the unity project healtUI I need to show the path Raw_Hp. How can I do th
Did you try dragging and dropping the Raw_HP element into your Healt UI place in your Hp Bar script?
The Raw_HP is not instantiated so you can just simply drag it there.
AN EDIT TO YOUR COMMENT
So I see you instantiate/spawn your character object that has the Hp Bar script
(Althought I can see many better ways to handle this situation)
But if you want to continue with this thing, just add a GameTag to your hpbar like "GUI_HpBar" and then in your Hp Bar script do
void Start() {
HealtUI = GameObject.FindGameObjectWithTag("GUI_HpBar")
}
This will automatically find the desired HpBar and link it.
I am a beginner with Unity3D.
Until now I have learned a few things.
For example, create a simple 2D menu, switch from one scene to another with an appropriate C# script and associate the scripts with the objects.
The problem I pose here is about the behavior (strange to me) of the white panel behind the menu when I change the scene.
I have two scenes, the first which is the menu and a second one is the actual game.
I used UI elements to create the first one, while I used 3D objects to make the second one.
So, what happens?
It happens that when you step into the second scene by clicking on the green button the background becomes brown. If, on the other hand, I run the second scene individually without going through the first one (launching the scene directly), the background remains white.
See this short video for further details.
Why?
Listed below are some images, hoping they can help me understand my problem.
The first scene (dev mode):
The first scene (running):
The second scene (dev mode):
The second scene (running directly):
The second scene (running after clicking the green button "Comincia"):
The script I use to change the scene (linked to the green button):
using UnityEngine.SceneManagement;
using UnityEngine;
public class GameScene : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayNow () {
SceneManager.LoadScene("Game", LoadSceneMode.Single);
}
}
Update: I have updated the information on the application. I added a new screenshot where you can see the camera settings. Meanwhile, I changed some things, but the problem still remains. I also noticed that this problem only happens when I switch to the Game scene, when I switch to other scenes instead it does not.
Added a new screenshot showing the inspector of the Main Camera object:
I was able to understand where the problem was: I made an improper use of the Plane object (highlighted in the screenshot). By removing it, now it shows correctly the solid color white background as set on the Main Camera.
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.