Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Karsten · May 11, 2013 at 07:03 AM · spriteinspectorngui

How to change NGUI Imagebutton amount of sprite states

NGUI's UIImageButton appears to only have three states, not including one for disabled, how could I add the disabled state to the image button?

please look that video and tell me what im thinking or doing wrong please.

http://www.youtube.com/watch?v=xb_RcrPohYU

 using UnityEngine;
 
 /// <summary>
 /// Sample script showing how easy it is to implement a standard button that swaps sprites.
 /// </summary>
 
 [ExecuteInEditMode]
 [AddComponentMenu("NGUI/UI/Image Button")]
 public class UIImageButton : MonoBehaviour
 {
     public UISprite target;
     public string normalSprite;
     public string hoverSprite;
     public string pressedSprite;
 
     void OnEnable ()
     {
         if (target != null)
         {
             target.spriteName = UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite;
         }
     }
 
     void Start ()
     {
         if (target == null) target = GetComponentInChildren<UISprite>();
     }
 
     void OnHover (bool isOver)
     {
         if (enabled && target != null)
         {
             target.spriteName = isOver ? hoverSprite : normalSprite;
             target.MakePixelPerfect();
         }
     }
 
     void OnPress (bool pressed)
     {
         if (enabled && target != null)
         {
             target.spriteName = pressed ? pressedSprite : normalSprite;
             target.MakePixelPerfect();
         }
     }
 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 11, 2013 at 07:33 AM 0
Share

Your video doesn't work and in any case this should be a question that is contained on this site with an explanation of the problem. Please fix the video link and describe the problem.

avatar image Karsten · May 11, 2013 at 10:07 AM 0
Share

The videolink works like charm i dont know whats wrong, i have proof from 2 skype contacts.

Why do you do it ?

avatar image whydoidoit · May 11, 2013 at 10:27 AM 0
Share

$$anonymous$$ust have been my computer. It does work on my phone...

avatar image Karsten · May 11, 2013 at 10:37 AM 0
Share

ok, so can you please open the question again? i do videos because its easier for me also i think a video shows things 7845,8 times better than 30 lines of text (with wrong grammar here and there if the writer isnt native english speaker).

or is it forbidden to link videos/external sources?

avatar image whydoidoit · May 12, 2013 at 07:00 AM 0
Share

Hey sorry, it wasn't me that closed this question. I have reinstated it and reopened it for you.

It is not good practice on UA to have a question that is just a video. It's fine to have a video that shows what you mean but the question should reflect actually what you want (so people can search for it).

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by whydoidoit · May 12, 2013 at 07:15 AM

So NGUI works by finding a collider and sending messages to it. All of the NGUI logic for button presses etc are in the UICamera script.

Enabled is an interesting state, if you look at UIButton you will see it implements it as "isEnabled" and in fact UIButton just manages it all internally. To be clear, you do not need a UIButton in order to have a button in NGUI!

So if you want to enable some other state you do so in response to the messages you receive in the script, but in the case of Enabled you script it yourself. Perhaps like this:

 using UnityEngine;
 
 [ExecuteInEditMode]
 [AddComponentMenu("NGUI/UI/Image Button")]
 public class UIImageButtonEx : MonoBehaviour
 {
     public UISprite target;
     public string normalSprite;
     public string hoverSprite;
     public string pressedSprite;
     public string disabledSprite;
     public string clickedSprite;
     public bool isEnabled;
     bool _lastEnabled;

     void OnEnable ()
     {
         StartCoroutine(CheckForEnabled());
     }

     void OnDisable()
     {
           if(!string.IsNullOrEmpty(disabledSprite))
               target.spriteName = disabledSprite;
     }

     IEnumerator CheckForEnabled()
     {
           if(target)
                 target.spriteName = isEnabled ?  (UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite) : disabledSprite ;
           while(true)
           {
              
                if(target && _lastEnabled != isEnabled)
                {
                    _lastEnabled = isEnabled;
                    target.spriteName = isEnabled ?  (UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite) : disabledSprite ;                      
                }
                yield return new WaitForSeconds(0.1f);
           }
     }
 
     void Awake ()
     {
         if (target == null) target = GetComponentInChildren<UISprite>();
             disabledSprite = !string.IsNullOrEmpty(disabledSprite) ? disabledSprite : normalSprite;
     }

     void OnClick()
     {
            if(target && !string.IsNullOrEmpty(clickedSprite))
            {
               StartCoroutine(ClickSprite());
            }
     }

     IEnumerator ClickSprite()
     {
          var former = target.spriteName;
          target.spriteName = clickedSprite;
          yield return new WaitForSeconds(0.6f);
          if(target.spriteName == clickedSprite)
                target.spriteName = former;
 
     }
 
     void OnHover (bool isOver)
     {
         if (enabled && target != null)
         {
             target.spriteName = isOver ? hoverSprite : normalSprite;
             target.MakePixelPerfect();
         }
     }
 
     void OnPress (bool pressed)
     {
         if (enabled && target != null)
         {
             target.spriteName = pressed ? pressedSprite : normalSprite;
             target.MakePixelPerfect();
         }
     }
 }


I should point out that UICamera will still send the messages to the other scripts which would need to check the isEnabled property of this script to see if they should react. You could edit UICamera to make that not true.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 13, 2013 at 06:07 AM 0
Share

You probably also want to write a custom inspector for it, so that you get the buttons that choose sprites. Basically that ends up being just an:

       NGUIEditorTools.SpriteField("Whatever", currentAtlas, currentSprite, functionToSetNewOne);

$$anonymous$$g.

    NGUIEditorTools.SpriteField("Hover", mSprite.atlas, mButton.hoverSprite, OnHover);

    ...

 void OnHover (string spriteName)
 {
     NGUIEditorTools.RegisterUndo("Image Button Change", mButton, mButton.gameObject, mSprite);
     mButton.hoverSprite = spriteName;
     Repaint();
 }
avatar image whydoidoit · May 13, 2013 at 06:07 AM 0
Share

Check out UIImageButtonInspector

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to change sound option sprite when clicked 0 Answers

How to add sprites from a sprite sheet to an NGUI atlas 1 Answer

[Uni2D + Unity Inspector] Unity extremely slow, when a Sprite is selected 4 Answers

NGUI Texture atlas help ! 1 Answer

NGUI Slider foreground color change 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges