GUIHeader2As a follow-up to «Choosing GUI framework for your Unity3D project: EZGUI vs NGUI, Part I» here is another post comparing these both frameworks by workflows. They are quite different, probably when you realize it with this small example how to create a simple button, you will make your decision more confidently which framework to choose in your project.

Creating simple button with EZGUI

First of all let’s create an orthographic camera in UI layer that will render UI only:

GameObject -> CreateOther -> Camera and set it up as shown below:

You see I’ve attached UIManager component.

Create an empty GameObject called “Button” and attach Button component to it (“Component/EZ GUI/Controls/Button”).  The script needs to be set up as well. Assign UI Camera in Render Camera slot, and check Pixel Perfect checkbox (Auto Resize will be checked automatically). And here is one of the most annoying disadvantages of EZGUI – when instantiating UI element as a prefab, there’s no guarantee Render Camera will be assigned correctly.

We will also need several button images for all button states: Normal, Over, Active, Disabled

part2-2

Next, you will need to create atlas for button states. Just press this little gear and select “Build Atlas“

part2-3

And here is the texture:

Note that atlas can contain not only states for single button – but for all the elements in your entire UI. But I would highly recommend sorting them by screens, like start screen elements in one atlas, settings screen in another.  I like this way, because you won’t load memory with redundant atlases.

Now let’s make it working. It’s quite simple with EZGUI: just specify GameObject that has a script with a method that will be called when button is pressed.

Here’s the script that we have to attach to a “controller” GameObject.

using UnityEngine;
public class TestScript : MonoBehaviour
{
    public void ProcessTestButton()
    {
        Debug.LogWarning("Hello world");
    }
}

part2-6Then configure the button: set controller GameObject in “Script With Method To Invoke” slot, specify method name in “Method To Invoke” field and select “When To Invoke” option.

part2-7
Now if you click “Play” you will get fully functional button. Here we go…

part2-8

And here is just a little tip. Attach EZ Screen Placement component to your button and it will be placed on screen just the way you need. For example you can stick your button with the top right corner of screen, or stick with some object, and it will maintain constant pixel offset in screen coordinates. Again be very careful with Render Camera. This is the first point you need to check if you see that your elements are moved to incorrect position or have a wrong scale. Also this component stops working in editor mode when you have it on prefab instance and click “apply” button – just click “Play” and then “Stop” to fix this.

Creating simple button with NGUI

Basically, both EZGUI and NGUI work with similar principle – generating meshes, generating UV coordinates and applying textures automatically. However NGUI’s “Button” doesn’t actually mean what we used to think. “Button” is everything in the scene that has a collider attached and is visible by camera having UICamera component, so this pseudo-button object can receive events, generated by UICamera. Here’s the list of all events.

When you see UIButton* (e.g. UIButtonColor) component, it does not mean that it has to be attached to something like a button (well, you won’t even find UIButton component in the list of scripts). Instead, it can be attached to ANYTHING that has a collider, for example – UICheckbox, and your own script, attached to this checkbox, will receive all the events from the list. Well, UICheckbox is just an example, but even a sphere with a collider can receive events! (It can be seen in one of examples provided by Tasharen Entertainment)

Now let’s do the same job using NGUI and start with some preparations. You need to create atlas for UI. In comparison with EZGUI – NGUI atlas is not just a texture – it’s a prefab that contains all information about sprites. It can be easily created using Atlas Maker.

part2-8

By clicking “Create” button, wizard creates new material and prefab that will store all required information about atlas and sprites.

part2-9

Great advantage of NGUI atlas is a possibility of adding and deleting images from atlas without rebuilding it totally. As you can see below, all you need to do is just select textures you want to add/update. In this case you see four new textures will be added to the atlas TestAtlas.

part2-10

After adding images to atlas, you will be able to add, delete, and modify them at any time which is cool I think. The only thing I miss here is forcing creation of square atlases, which is required for PVRTC compression on mobile devices.

part2-11

Atlas prefab can be edited from its inspector, where you can add, delete and setup sprites, with real-time preview of what you are doing.

part2-12So, when you’re finished with sprites, you can go ahead to creating your UI.

Create a base for the future UI. NGUI -> Create new UI. UI Tool opens up.
Just set a layer where UI will be rendered and a camera you’re going to use.

part2-13You will see the following hierarchy:

part2-14

  • “UI Root” is responsible for scaling the entire UI, to maintain its screen size when changing resolution.
  • “Camera” renders UI geometry (btw, this geometry is handled by UIDrawcall script). It also sends events to objects.
  • “Anchor” is used for placing widgets in correct positions, add half-pixel offset to all UI (you can read about half-pixel and half-texel offsets here), and it also can be used to stretch sprites to fill entire screen on different resolutions, e.g. to be used as tiling background.
  • “Panel” groups UI objects (widgets) together and shows some debug information about widgets it contains. Also it has clipping ability to be used in scrollable lists.

Next step – creating widgets. “NGUI -> Create a Widget” opens Widget Tool. In order to create a widget of any type you need to specify an atlas where required sprites will be taken from, widget template – in our case it would be “Image Button”, that works quite similar to EZGUI’s button, setup images needed for three button states – normal, hover and pressed (I’m not sure why, but there’s no disabled state), and press “Add To” button.

part2-15

That’s it, you already have you button! Just hit play and check it out!

part2-16

And the last step – the button interaction with scripts. Attach “UI Button Message” component to the button. It allows calling methods in the script attached either to a button GameObject or any custom GameObject, specified in “Target” slot. If target GameObject isn’t set, the button GameObject itself will be assigned as Target automatically.

part2-17You can see UIButtonMessage component on the screenshot, with Function Name specified – the name of method that will be called on a specific event (check “Trigger” variable).

using UnityEngine;
public class TestScript : MonoBehaviour
{
    public void ProcessTestButton()
    {
        Debug.LogWarning("Hello world");
    }
}

Then, we press our button and here’s it:

part2-8

Finally EZGUI or NGUI?

Personally I stick with NGUI that attracts with its minimalism, short, clean and easy-understandable code, stability and optimization. I really hope these posts were helpful for you. Please put comments, ratings and suggest what to add\improve, I’ll be able update this post later.

Author Andrei Chaiko

Senior Unity Developer with Heyworks Unity Studio