Author Topic: Button state change by condition  (Read 5862 times)

iwishash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Button state change by condition
« on: May 01, 2012, 11:58:51 AM »
Hi
I bought NGUI just 3 days ago, so I'm not familiar with this.
I'm trying to make menu UI system with NGUI.
I made main panel, start panel, option panel....and so on.
The way of making UI with NGUI seems super easy!

But I got stucked at "start panel" menu.
In start panel, there is input field which allows you to type name.
After fill this, you can click "start" button which will launch the game scene.
Thus, if you don't fill the input field, the "start" button should be disabled state. (that means, change the button color with grey, and there is no response of click or touch)
I made input field and start button but I don't know how to change button state.
And if I go to main panel by "back" button, then go back to start panel again, the input field should reset.

Than's all I want.
Is there any suggestion how to do or any hint?
It would be appreciated.
Thank you.


« Last Edit: May 01, 2012, 12:02:36 PM by iwishash »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button state change by condition
« Reply #1 on: May 01, 2012, 12:39:38 PM »
Modify UIButtonColor line 50. Before:
  1. void Start () { mStarted = true; }
After:
  1. void Start () { mStarted = true; Init(); }

Then use this class instead of UIButtonColor:

  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Similar to UIButtonColor, but adds a 'disabled' state.
  5. /// </summary>
  6.  
  7. [AddComponentMenu("NGUI/Interaction/Button")]
  8. public class UIButton : UIButtonColor
  9. {
  10.         /// <summary>
  11.         /// Whether the button should be enabled.
  12.         /// </summary>
  13.  
  14.         public bool isEnabled = true;
  15.  
  16.         bool mEnabled = true;
  17.  
  18.         void Update ()
  19.         {
  20.                 if (mEnabled != isEnabled)
  21.                 {
  22.                         mEnabled = isEnabled;
  23.  
  24.                         if (tweenTarget)
  25.                         {
  26.                                 Color c = defaultColor;
  27.  
  28.                                 if (!mEnabled)
  29.                                 {
  30.                                         c.r *= 0.75f;
  31.                                         c.g *= 0.75f;
  32.                                         c.b *= 0.75f;
  33.                                 }
  34.                                 TweenColor.Begin(tweenTarget, 0.15f, c);
  35.                         }
  36.                         Collider col = collider;
  37.                         if (col) col.enabled = mEnabled;
  38.                 }
  39.         }
  40. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button state change by condition
« Reply #2 on: May 01, 2012, 12:54:18 PM »
P.S. I'll have a more robust version of this script in the next release.