Author Topic: UICheckboxPlus (UITexture instead of UISprite)  (Read 1856 times)

schellenberghq

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
  • Game Programmer
    • View Profile
    • Blog
UICheckboxPlus (UITexture instead of UISprite)
« on: September 09, 2013, 03:53:09 PM »
I modified the code of the UICheckbox to handle a UITexture instead of a UISprite. This came in handy for me, so I figure perhaps there is someone else out there who could benefit from it. So here you are.


  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2013 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using AnimationOrTween;
  8.  
  9. /// <summary>
  10. /// Simple checkbox functionality. If 'option' is enabled, checking this checkbox will uncheck all other checkboxes with the same parent.
  11. /// </summary>
  12.  
  13. [AddComponentMenu("NGUI/Interaction/Checkbox")]
  14. public class UICheckboxPlus : MonoBehaviour
  15. {
  16.         static public UICheckboxPlus current;
  17.         public delegate void OnStateChange (bool state);
  18.  
  19.         /// <summary>
  20.         /// Sprite that's visible when the 'isChecked' status is 'true'.
  21.         /// </summary>
  22.  
  23.         public UITexture checkSprite;
  24.  
  25.         /// <summary>
  26.         /// Animation to play on the checkmark sprite, if any.
  27.         /// </summary>
  28.  
  29.         public Animation checkAnimation;
  30.  
  31.         /// <summary>
  32.         /// If checked, tween-based transition will be instant instead.
  33.         /// </summary>
  34.  
  35.         public bool instantTween = false;
  36.  
  37.         /// <summary>
  38.         /// Whether the checkbox starts checked.
  39.         /// </summary>
  40.  
  41.         public bool startsChecked = true;
  42.  
  43.         /// <summary>
  44.         /// If the checkbox is part of a radio button group, specify the root object to use that all checkboxes are parented to.
  45.         /// </summary>
  46.  
  47.         public Transform radioButtonRoot;
  48.  
  49.         /// <summary>
  50.         /// Can the radio button option be 'none'?
  51.         /// </summary>
  52.  
  53.         public bool optionCanBeNone = false;
  54.  
  55.         /// <summary>
  56.         /// Generic event receiver that will be notified when the state changes.
  57.         /// </summary>
  58.  
  59.         public GameObject eventReceiver;
  60.  
  61.         /// <summary>
  62.         /// Function that will be called on the event receiver when the state changes.
  63.         /// </summary>
  64.  
  65.         public string functionName = "OnActivate";
  66.  
  67.         /// <summary>
  68.         /// Delegate that will be called when the checkbox's state changes. Faster than using 'eventReceiver'.
  69.         /// </summary>
  70.  
  71.         public OnStateChange onStateChange;
  72.  
  73.         // Prior to 1.90 'option' was used to toggle the radio button group functionality
  74.         [HideInInspector][SerializeField] bool option = false;
  75.  
  76.         bool mChecked = true;
  77.         bool mStarted = false;
  78.         Transform mTrans;
  79.  
  80.         /// <summary>
  81.         /// Whether the checkbox is checked.
  82.         /// </summary>
  83.  
  84.         public bool isChecked
  85.         {
  86.                 get { return mChecked; }
  87.                 set { if (radioButtonRoot == null || value || optionCanBeNone || !mStarted) Set(value); }
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Legacy functionality support -- set the radio button root if the 'option' value was 'true'.
  92.         /// </summary>
  93.  
  94.         void Awake ()
  95.         {
  96.                 mTrans = transform;
  97.  
  98.                 if (checkSprite != null) checkSprite.alpha = startsChecked ? 1f : 0f;
  99.  
  100.                 if (option)
  101.                 {
  102.                         option = false;
  103.                         if (radioButtonRoot == null) radioButtonRoot = mTrans.parent;
  104.                 }
  105.         }
  106.  
  107.         /// <summary>
  108.         /// Activate the initial state.
  109.         /// </summary>
  110.  
  111.         void Start ()
  112.         {
  113.                 if (eventReceiver == null) eventReceiver = gameObject;
  114.                 mChecked = !startsChecked;
  115.                 mStarted = true;
  116.                 Set(startsChecked);
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Check or uncheck on click.
  121.         /// </summary>
  122.  
  123.         void OnClick () { if (enabled) isChecked = !isChecked; }
  124.  
  125.         /// <summary>
  126.         /// Fade out or fade in the checkmark and notify the target of OnChecked event.
  127.         /// </summary>
  128.  
  129.         void Set (bool state)
  130.         {
  131.                 if (!mStarted)
  132.                 {
  133.                         mChecked = state;
  134.                         startsChecked = state;
  135.                         if (checkSprite != null) checkSprite.alpha = state ? 1f : 0f;
  136.                 }
  137.                 else if (mChecked != state)
  138.                 {
  139.                         // Uncheck all other checkboxes
  140.                         if (radioButtonRoot != null && state)
  141.                         {
  142.                                 UICheckboxPlus[] cbs = radioButtonRoot.GetComponentsInChildren<UICheckboxPlus>(true);
  143.  
  144.                                 for (int i = 0, imax = cbs.Length; i < imax; ++i)
  145.                                 {
  146.                                         UICheckboxPlus cb = cbs[i];
  147.                                         if (cb != this && cb.radioButtonRoot == radioButtonRoot) cb.Set(false);
  148.                                 }
  149.                         }
  150.  
  151.                         // Remember the state
  152.                         mChecked = state;
  153.  
  154.                         // Tween the color of the checkmark
  155.                         if (checkSprite != null)
  156.                         {
  157.                                 if (instantTween)
  158.                                 {
  159.                                         checkSprite.alpha = mChecked ? 1f : 0f;
  160.                                 }
  161.                                 else
  162.                                 {
  163.                                         TweenAlpha.Begin(checkSprite.gameObject, 0.15f, mChecked ? 1f : 0f);
  164.                                 }
  165.                         }
  166.  
  167.                         current = this;
  168.  
  169.                         // Notify the delegate
  170.                         if (onStateChange != null) onStateChange(mChecked);
  171.  
  172.                         // Send out the event notification
  173.                         if (eventReceiver != null && !string.IsNullOrEmpty(functionName))
  174.                         {
  175.                                 eventReceiver.SendMessage(functionName, mChecked, SendMessageOptions.DontRequireReceiver);
  176.                         }
  177.                         current = null;
  178.  
  179.                         // Play the checkmark animation
  180.                         if (checkAnimation != null)
  181.                         {
  182.                                 ActiveAnimation.Play(checkAnimation, state ? Direction.Forward : Direction.Reverse);
  183.                         }
  184.                 }
  185.         }
  186. }
  187.  
---------------------------
Jacob S.
Game Programmer
---------------------------

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICheckboxPlus (UITexture instead of UISprite)
« Reply #1 on: September 10, 2013, 07:07:55 AM »
Change it to "UIWidget" and you will handle both in the same script.