Advertisement
CassataGames

ButtonControl

Jan 5th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class T_MainMenu : MonoBehaviour {
  6.  
  7.     public Color Active;
  8.     public Color Inactive;
  9.     public AnimationCurve Curve;
  10.  
  11.     public T_Button[] Buttons;
  12.     public bool NoneHovering;
  13.  
  14.     void Update()
  15.     {
  16.         // Reset NoneHovering state.
  17.         NoneHovering = true;
  18.         foreach (T_Button I in Buttons)
  19.         {
  20.             #region BrightnessControl
  21.             if (I.Highlight && I.Transition < 1)
  22.                 I.Transition += Time.deltaTime * 2;
  23.             if (!I.Highlight && I.Transition > 0)
  24.                 I.Transition -= Time.deltaTime * 2;
  25.             I.ImageAccess.color = Color.Lerp(Inactive, Active, Curve.Evaluate(I.Transition));
  26.             #endregion
  27.  
  28.             // Button found hovering.
  29.             if (I.Hovering)
  30.                 NoneHovering = false;
  31.         }
  32.         // If no buttons are hovered over, set all highlights true.
  33.         // If one button is hovered over, check each button and set its highlight to its hover state.
  34.         if (NoneHovering)
  35.             foreach (T_Button I in Buttons)
  36.             {
  37.                 I.Highlight = true;
  38.             }
  39.         else
  40.             foreach (T_Button I in Buttons)
  41.             {
  42.                 I.Highlight = I.Hovering;
  43.             }
  44.  
  45.     }
  46.  
  47.     // Called from the button when the cursor hovers over it.
  48.     public void HoverOn(string Name)
  49.     {
  50.         foreach (T_Button I in Buttons)
  51.         {
  52.             I.Hovering = (I.Name == Name);
  53.         }
  54.     }
  55.     // Called from the button when the cursor is no longer hovering over it.
  56.     public void HoverOff(string Name)
  57.     {
  58.         foreach (T_Button I in Buttons)
  59.         {
  60.             I.Hovering = false;
  61.         }
  62.     }
  63. }
  64.  
  65. [System.Serializable]
  66. public class T_Button
  67. {
  68.     public string Name;
  69.     public Image ImageAccess;
  70.     public bool Hovering;
  71.     public bool Highlight;
  72.     public float Transition;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement