- Home /
Editor Window , Is There no Callback on GUI.Button??
I studied Android for long time. In Android. u can add callback (like onClick) on view simply. like,
Button button1 = new Button(context); button1.setOnClickListener(new OnClickListener(){ public void onClick(int id) { // Contents... } });
but WindowEditor in Unity,
There is no callback. and just handle events with 'if'.
Like,
if (GUI.Button(new Rect(100,100 , 100, 100), "Sample")) { Debug.Log("OnClickButton"); }
but this case, It seems to impossible to identify "mouse down" and "mouse up". u can just know it is clicked or not. (just quick mouse down and up, If you drag the button or make slow down and up, unity doesn't see it as a click so 'Debug.Log is not executed')
so these are my question.
Is there more elaborate way to handle Event on Button? I want to drag buttons.
I want to manage GUI things more OOP-likely. for example, I want to store rect information on instances. (Just Image dragging some buttons, It will be more easier if I can make Button objects) not like code that I showed you. Is there a way to do. 3.If you have some nice document for EditorWIndow please let me know.
thank you :)
Answer by srivello · Jan 05, 2017 at 09:27 AM
Unity has 2 GUI systems.
Immediate GUI - Available in Editor (By 'Editor' I mean Custom Editors and Inspectors and stuff like that). Also available at Runtime but its deprecated in favor of uGUI (since Unity 4.6)
Retained GUI - Available only at runtime as uGUI (since Unity 4.6)
Coming from 'Android' you are probably used to a retained GUI. One that you declare once and then can safely assume the 'OOP-like' object for a Button or other control continues to exists until you destroy it. This allows for state during its lifetime which allows for callbacks.
In Immediate GUI, you declare a control like a button and that declaration is RECALLED MANY TIMES per second. Not just once per render, but much more. Some info on it is here. https://docs.unity3d.com/Manual/gui-Basics.html. Due to how the system works if it was not recalled it would dissapear. It has no lifetime of its own and thus no callbacks.
I enjoy the uGUI system for runtime use. Its great. It would be nice to have that same level of control within the Editor for use in Custom Inspectors, however I'm not sure that is possible nor will ever happen. So for now, good luck with the immediate GUI :)
O$$anonymous$$G... So you mean, If you want to use Retained GUI System, You have to make it all by yourself... (all by myself...) I hope there is some library or something. I would better check it...
Your answer
Follow this Question
Related Questions
Editor GUI ScrollView Actual Scrollbar Not Showing Up?? 1 Answer
Reordable List in a window 1 Answer
Editor GUI Foldout header style customization 0 Answers
Is it possible to detect drag and drop in hierachy window? 0 Answers
Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer