A "Create UI Button" editor script [Obsolete]

This is the place to post add-on scripts you have written that use EZ GUI. Please remember only to post your add-on content and not the original EZ GUI scripts.
Forum rules
Only post your own add-on content and not any of the original EZ GUI scripts.

A "Create UI Button" editor script [Obsolete]

Postby Little Angel » Fri Oct 08, 2010 10:28 am

This is an editor script to simplify the creation of UIButtons.
Updated 13-10-2010
Please see this thread for the combined package of all the scripts:
viewtopic.php?f=9&t=242

Create UI Button is a way to speed up the creation process, primarily through by-passing the Create GameObject, Assign Component and Set Parent in Hierarchy stages. This script also simplifies assigning single textures per state to the buttons, as they are all on one page.

Please note: This is not a replacement for any of the EZGui controls or control windows (eg: Control Editor, Panel Editor), but is an addon to Brady's AWESOME software package.

Little Angel wrote:Images now propagate through the button.
"Main Texture" will be assigned to the "Normal" state, and any state with an unassigned slot.
- Any slot with an image will have this image assigned to that control state (Over, Disabled, etc.).
- Any slot without an image will have the "Main Texture" assigned to that control state.
- If you need a simple button with only one image, you need only assign the "Main Texture" and leave the rest of the slots empty.


Little Angel wrote:Text is now created when you build the button.
When you include any text in the "Text" slot, it will be added to the UIButton and EZGui will create the appropriate child SpriteText object.


Little Angel wrote:You can choose a parent transform for your new UIButton.
You can choose a parent transform and the newly created button will be placed under that transform as a child and it's local position will be reset to (0,0,0).
You can choose a parent transform either by dragging a transform into the "Parent" slot of the wizard window, or you can click on any GameObject that has NO UIButton.
- If you drag a parent transform into the "Parent" slot, that object can be a UIButton and it's settings will not be over-written by this wizard (as long as it's not selected as well).
- If you select a GameObject in the hierarchy with a UIButton Component, this wizard is designed to over-write the settings, and it will not use this GameObject as a parent, but will apply new settings.


Little Angel wrote:You can select GameObjects in the hierarchy window.
If you select nothing, this wizard assumes that you want to create a new button at the root of the hierarchy.
If you select a GameObject with NO UIButton, this wizard assumes that you want this GameObject to be the new parent object for the button you are going to create.*
If you select a GameObject with a UIButton, this wizard assumes that you want to update its settings.

- If you select a GameObject with NO UIButton, this wizard assumes that you want this GameObject to be the new parent object and will assign this GameObject to the Parent slot and this GameObject will be the parent to your new UIButtons.

- If you select the UIButton before you open the wizard, the wizard will load the current setting into the wizard and you will be able to update these settings and apply them.
- If you pick a new object once the wizard is open, the setting will not update automatically. The way wizards are built, they don't look for anything new until the user interacts with the wizard (there is no Update() loop). I have added a "Refresh Button To Edit" check box, so if you select a new button to edit or any changes have been made the the selected UIButton outside of this wizard, touch this "Refresh Button To Edit" check box and it will update the details in the wizard window if that GameObject still contains a UIButton.

- CURRENTLY: If you select a UIButton this wizard will not update the sprite material variable with the current material from the existing button.
Warning: If you have set a sprite material in this wizard, this sprite material will overwrite the existing material on the current button, but only if "Update Sprite Material" is checked, otherwise, this material will be ignored when the UIButton is created or updated.



Little Angel wrote:You can now select a sprite material that will be assigned to the UIButton.
There is now the option to set a sprite materials variable, which will be assigned to new buttons or be applied as a change to existing buttons.

- CURRENTLY: If you select a UIButton this wizard will not update the sprite material variable with the current material from the existing button.
Warning: If you have set a sprite material in this wizard, this sprite material will overwrite the existing material on the current button, but only if "Update Sprite Material" is checked, otherwise, this material will be ignored when the UIButton is created or updated.



Little Angel wrote:There is a "hot key" for this window.
Option B will now act as a hot key for this window.


Let me know how this is working for you: What's good, what's not, what's confusing... and I'll see what I can do to set things right.

I will be perfecting this, as it doesn't do what I want just yet.

This wizard will replace the existing textures contained in an existing button if you apply changes with new textures - including null values.
Empty slots will over-write existing images.

REMEMBER: You must build your atlas like normal after creating your new buttons. If you are using the standard "scan project folder" and need to have all of your buttons built into prefabs, please look at the "make prefab" script to simplify this step. (Eventually I will add a 'create/update prefab' check box, but that is in the future.)

Little Angel wrote:The direct paste of the code has been deleted. The code is now saved as an asset/unity package. Please use the asset package above...
Last edited by Little Angel on Wed Oct 13, 2010 12:31 am, edited 23 times in total.
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Re: A "Create UI Button" editor script for you

Postby Little Angel » Fri Oct 08, 2010 12:31 pm

Code updated
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Re: A "Create UI Button" editor script

Postby MadMac » Fri Oct 08, 2010 3:47 pm

cool !
this Scripts should go in a special Section to find them better later after the clutter starts..
thank's man
Mad
MadMac
 
Posts: 47
Joined: Tue Sep 28, 2010 11:03 am

Re: A "Create UI Button" editor script

Postby Brady » Sat Oct 09, 2010 1:29 am

Cool! I had planned to do something like this eventually, so kudos to you! I might integrate it in a future version if that's okay.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: A "Create UI Button" editor script

Postby Little Angel » Sat Oct 09, 2010 11:15 am

No problem using this at all! Any improvement is great.

You wouldn't happen to know the best way to assign the texture2d images would you?

My feeling is the I'll need some sort of 2 step script that creates the UIButton GameObject, adds the UIButton script , and names it, then waits a frame before asking if there needs to be text and perhaps images, as a lot ofthe UIButton seems to be created as it's instantiated rathe than before (require component? Have not delved that deep...)
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Re: A "Create UI Button" editor script

Postby Brady » Sun Oct 10, 2010 5:27 am

You should be able to go ahead and add textures after creating it. You should be able to assign a texture to a state by doing something like this (untested):

Code: Select all
// 0 is the first state ("Normal" for UIButton)
control.States[0].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(normalTexture));

// 1 is the second state ("Over" for UIButton)
control.States[1].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(overTexture));

// etc...


You'll need to build your atlas(es) after that, of course.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: A "Create UI Button" editor script

Postby ibyte » Sun Oct 10, 2010 1:50 pm

Guys is it possible to create a GameObject as a child of an exising object vs @ top of the hieracrchy and then have to move and adjust the postions? That would be handy. Even handierif you could run a script like this with a right click.
ibyte
 
Posts: 258
Joined: Tue Sep 07, 2010 8:23 pm

Re: A "Create UI Button" editor script

Postby Little Angel » Sun Oct 10, 2010 2:18 pm

Well, ultimately we'll see where automation ends... like after building the button an auto-build prefab and auto-build atlas... but for right now, auto-build button that can do text and images will be fine.

I'll give that code a go, and let you know how it works.
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Re: A "Create UI Button" editor script

Postby Little Angel » Sun Oct 10, 2010 3:34 pm

Please don't use this code as it is NOT working at the moment...

Brady, I've used the suggestion from above as best as possible, but I'm getting an error:
Console wrote:error CS0029: Cannot implicitly convert type `string' to `string[]'

... from lines like this:
thisButton.States[0].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(mainTexture));

... which was my attempt at using:
control.States[0].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(normalTexture));

Feel free to say "I'm a bit busy to write your code for you right now". If this is an easy "point me to it", let me know, otherwise, I'll put off the image part of this wizard for now, as this part of EZGui's guts is a bit beyond me!


Code: Select all
//   CreateUIButton.cs
//   An editor script to simplify the creation of an EZGui UIButton.
//   More to come...

using UnityEditor;
using UnityEngine;

class CreateUIButton : ScriptableWizard {
   public string buttonName = "New Button";
   private int layerMask;
   public string layerName = "UILayer";
    public float height = 0;
    public float width = 0;
   public string text = "";
   public Texture2D mainTexture;
   public Texture2D overTexture;
   public Texture2D activeTexture;
   public Texture2D disabledTexture;
   private bool hasStarted = false;
   private UIButton thisButton;
   private GameObject thisGameObject;
   
   
    [MenuItem ("EZG-Wiz/Button")]
    static void CreateWizard () {
        ScriptableWizard.DisplayWizard<CreateUIButton>("EZ Button", "Create & Close", "Create or Apply");
        //   If you don't want to use the secondary button simply leave it out:
//        ScriptableWizard.DisplayWizard<CreateButton>("Create Button");
    }
   
    void OnWizardUpdate () {
        helpString = "Please set details on this button!\n\nNOTE: Main Texture will be propagated to all states\nthat are left unassigned.\n\nNOTE: Text can be APPLIED to an existing button,\nbut NOT when CREATED.";
      if (!hasStarted) {
         hasStarted = true;
         AtStart();
      }
    }
   
 // When the user pressed the "Create & Close" button OnWizardOtherButton is called.
    void OnWizardCreate () {
      CreateButton ();
    }
   
 // When the user pressed the "Create or Apply" button OnWizardOtherButton is called.
    void OnWizardOtherButton () {
      thisGameObject = Selection.activeGameObject;
      
      if (thisGameObject == null) {
         CreateButton ();
            return;
      }
         
      thisButton = thisGameObject.GetComponent <UIButton>();
        if (thisButton == null) {
            return;
      }
      
      ApplyToButton ();
    }
   
   void CreateButton () {
        thisGameObject = new GameObject (buttonName);
        thisButton = thisGameObject.AddComponent("UIButton") as UIButton;
      layerMask = LayerMask.NameToLayer(layerName);
      thisGameObject.layer = layerMask;
      thisButton.width = width;
      thisButton.height = height;
//      thisButton.text = text;
      UpdateTextures ();                                 //   DEAD END
   }
   
   void ApplyToButton () {
      thisGameObject.name = buttonName;
      layerMask = LayerMask.NameToLayer(layerName);
      thisGameObject.layer = layerMask;
        thisButton.width = width;
        thisButton.height = height;
      thisButton.text = text;
      UpdateTextures ();                                 //   DEAD END
   }
   
   void AtStart () {
      thisGameObject = Selection.activeGameObject;
      if (thisGameObject == null)
            return;
      UIButton thisButton = thisGameObject.GetComponent <UIButton>();
        if (thisButton == null)
            return;
      
      buttonName = thisGameObject.name;
      layerMask = thisGameObject.layer;
      layerName = LayerMask.LayerToName(layerMask);
      width = thisButton. width;
      height = thisButton.height;
      text = thisButton.text;
   }
   
   void UpdateTextures () {
      if (mainTexture != null) {
         thisButton.States[0].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(mainTexture));
//         thisButton.sourceTextures[0] = mainTexture;
         if (overTexture != null)
            thisButton.States[1].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(overTexture));
//            thisButton.sourceTextures[1] = overTexture;
         else
            thisButton.States[1].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(mainTexture));
//            thisButton.sourceTextures[1] = mainTexture;
         if (overTexture != null)
            thisButton.States[2].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(overTexture));
//            thisButton.sourceTextures[2] = activeTexture;
         else
            thisButton.States[2].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(mainTexture));
//            thisButton.sourceTextures[2] = mainTexture;
         if (overTexture != null)
            thisButton.States[3].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(overTexture));
//            thisButton.sourceTextures[3] = disabledTexture;
         else
            thisButton.States[3].frameGUIDs = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(mainTexture));
//            thisButton.sourceTextures[3] = mainTexture;
      }
   }
}


// ©2010 Adam Buckner (aka: Little Angel) and theantranch.com (mailto: adam@theantranch.com)
// Not for reuse or resale without permission.
// Permission granted to reuse this script by any owner of EZGui by A&BSoft.
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Re: A "Create UI Button" editor script

Postby Little Angel » Sun Oct 10, 2010 4:17 pm

Also, without prying too deeply:

How does the .Text property work? I find that no matter what I do, SetDirty, etc., I cannot successfully apply a value to the text field and get the "control_text" object when I create the GameObject and Assign the UIButton component. I can only do this on the "apply" stage, once the button has been built, I reacquire it, and then set it's text.

Moreover, if I have set the text in the text field, if I change it or add to it, I can't get the "control_text" object to be created. It will only create when I delete the text that was put in at the create step, and then add a new text value, and it works fine and creates the child text object.

What I want to do, is in one step add text and have it create the "control_text" object... I've not figured it out yet.

I'll admit that I'm worried about actually hard coding the creation of the "control_text" child, as I want to make sure it's working properly within the context of EZGui.

-

I sort of feel I need some sort of "pause" in the editor script, but you can't use co-routines (afaict) in the editor... I've tried SetDirty... But I feel that the UIButton component must register with the UIManager or something... hum... All speculation.
Little Angel
 
Posts: 772
Joined: Thu Sep 02, 2010 10:23 pm

Next

Return to EZ GUI Add-ons

Who is online

Users browsing this forum: No registered users and 2 guests