Search Unity

Modal Dialog Help

Discussion in 'Immediate Mode GUI (IMGUI)' started by steveg2357, Oct 14, 2014.

  1. steveg2357

    steveg2357

    Joined:
    Aug 21, 2014
    Posts:
    34
    I'm considering moving from another development environment to Unity and I'd appreciate some assistance on doing the following in UnityScript (NOT C#). Are there any related examples, tutorials, assets, etc?
    1. MyClass conditionally instantiates and shows MyModalDialogClass, a modal dialog window, which has various controls, including buttons such as "Done" and "Cancel," checkboxes, textfields, etc.
    2. MyClass receives a callback with user information (such as checkboxes selected, textfield entries, etc.) if "Done" is selected and null if "Cancel" is selected. MyClass then also hides (or destroys?) the instance of MyModalDialogClass.
     
  2. ShuuGames

    ShuuGames

    Joined:
    Jun 25, 2013
    Posts:
    188
    First you have to decide what UI system you want to use in Unity. It's a hard choice if you have no experience with any of them. I'd say that that go with UI system introduced in Unity 4.6 beta (not available in 4.5 release version), where you lay out the UI elements into a Canvas and then attach callbacks to your scripts and add other magic. Another officially supported choice is to create UI from code using OnGUI method (just google it). All other choices are third party ones which you may have to purchase. Most popular one that you can get is NGUI, which is rather similar to UI found in Unity 4.6 beta. NGUI is more powerful, but a bit more tedious to use.

    With all those options around there's no definite answer to your questions. With Unity 4.6 beta UI system the workflow for what you want would be something like this:
    * build the UI you want under Canvas gameobject with builtin WYSIWYG editor
    * write the script that contain code you want to execute on UI events like button clicks. Put it into a class that extends MonoBehaviour and add that component to some gameobject in the scene. Maybe even directly to the Canvas gameobject.
    * link UI element events to methods in your script. It can be done from script, but better way is to set the event receivers using UI element component inspector.
    * if you don't want to UI to be visible all the time, write a script that disables/enables Canvas gameobject when needed, or if you partially want to disable it, group your UI elements under a gameobject
    * last thing is the trickiest part. You have to make sure that UI stuff and what goes on in your game don't interfere. Most common thing is preventing "UI click through", but there are other cases. Also maybe your setup requires that some world items can come in front of the UI and then what.

    I see that you're thinking in terms of code, but in Unity that's the hard way of doing things. In Unity it's easier to think in terms of data. The scripts you write will work on data and are very loosely coupled to each other. It applies mostly to game world, but in many ways to UI also. Not UI related, but as a good illustrative example you may have an enemy character in the world which has all its stats, which is data. Then you write a few behaviour scripts that can be attached to that enemy. These use the data, but are indipendant from each other and really the world doesn't care and all code compiles and runs error free indipendant from what behaviours the enemy has.

    Maybe the best place to start learning Unity 4.6 UI system is from this overview video http://blogs.unity3d.com/2014/05/28/overview-of-the-new-ui-system/
     
    Last edited: Oct 20, 2014