Hi, I;m tryin to make a clickable scene button, but it wont work
void OnDrawGizmos () {
Handles.BeginGUI();
if (GUI.Button(new Rect(10, 10, 100, 50), "Button"))
{
Debug.Log("Pressed");
}
Handles.EndGUI();
}
Hi, I;m tryin to make a clickable scene button, but it wont work
void OnDrawGizmos () {
Handles.BeginGUI();
if (GUI.Button(new Rect(10, 10, 100, 50), "Button"))
{
Debug.Log("Pressed");
}
Handles.EndGUI();
}
Because your trying to call it on draw gizmos
Does it show up? Because I think you can only draw Gizmos in the OnDrawGizmos.
You need to place the code in a window on your OnSceneGUI function, something like this:
GUILayout.Window(0, new Rect(10, 10, 100, 100), (id)=> {
GUILayout.Button("A Button");
},
I dont need any windows, I just need a scene view button, and yes it draws with OnDrawGizmos…
I’ve tryied this way:
void OnDrawGizmos () {
Handles.BeginGUI();
Rect rect = new Rect(10, 10, 100, 50);
GUI.Button(rect, "Button");
if(Event.current.type == EventType.MouseDown)
{
Debug.Log("press");
}
Handles.EndGUI();
}
And it works everywhere except the button’s rect… wtf? How can I do the opposite?
I can make this work this way, but without button… looks like button blocking click event…
void OnDrawGizmos () {
Handles.BeginGUI();
Rect rect = new Rect(10, 10, 100, 50);
if(Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition))
{
Debug.Log("press");
}
Handles.EndGUI();
}
Help, I need this to work with a button, not just with rect!
try it on OnSceneGUI () { }
see if that makes a difference. It really hurts my brain that you’re doing it in gizmos space ha
It wont work inside OnSceneGUI at all …
I just tested in OnSceneGUI and it worked…
Exact same code I used. Except I drew a box to visualise it.
Might be a stupid question but are you sure you’re clicking in the right area?
Hey, It works with GUI.Box but not with a Button… Is there a way to draw a GUI.Box like a button, GUIStyle or something?
yeah use GUI Style, although I’m curious as to why it’s not working for you. Even without a box it works for me. Although I do have other stuff going on.
Try adding SceneView.RepaintAll (); somewhere in your on scene gui function. That’s the only thing my brain can think of that might be causing the issue
Can you post your version pls?
I can’t post my whole script, but here’s what I used to test your issue:
void OnSceneGUI () {
Handles.BeginGUI();
Rect rect = new Rect(10, 10, 100, 50);
if(Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition))
Debug.Log("press");
Handles.EndGUI();
SceneView.RepaintAll ();
}
Just nothing…
using UnityEngine;
using UnityEditor;
using System.Collections;
public class Test : Editor {
void OnSceneGUI () {
Handles.BeginGUI();
Rect rect = new Rect(10, 10, 100, 50);
GUI.Box(rect, "Button");
if(Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition))
Debug.Log("press");
Handles.EndGUI();
SceneView.RepaintAll ();
}
}
No box, no click detection…
hmm I’m stumped bro. I’m not sure why it’s not working for you. It must be something obvious…
You have hooked up the editor script to your base script right? the editor class is in the editor folder?
Ok, here is what I have for now:
Script:
using UnityEngine;
using System.Collections;
public class testScript : MonoBehaviour {
public GameObject selected;
}
Editor Script:
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(testScript))]
public class Test : Editor {
testScript myScript;
void OnEnable()
{
myScript = (testScript) target;
}
void OnSceneGUI () {
Handles.BeginGUI();
Rect rect = new Rect(10, 10, 100, 50);
GUI.Box(rect, "Button");
if(Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition))
Selection.activeGameObject = myScript.selected;
Handles.EndGUI();
SceneView.RepaintAll ();
}
}
The problem is GUI.Box is drawing only if the object with Script is selected(I need it to be drawn allways)… The other problem when I click this box, selection object goes to the myScript.selected gameobject, and immediately goes null.
Ok so I’ve tested this out. And it’s working now. The issue was that the event.current wasn’t being used. Fixed it with event.current.Use ();
I’ve cleaned it up a little:
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(testScript))]
public class Test : Editor {
testScript TestScript {
get { return (testScript) target;}
}
void OnSceneGUI () {
Event e = Event.current;
Handles.BeginGUI();
Rect rect = new Rect(10, 10, 100, 50);
GUI.Box(rect, "Button");
if(e.type == EventType.MouseDown && rect.Contains(e.mousePosition)) {
Selection.activeGameObject = TestScript.selected;
Debug.Log("work whore!!");
}
Handles.EndGUI();
e.Use ();
}
}
It goes null because you’re only setting the object when the mouse is down and is within that rect
Set it to EventType.MouseUp