Advertisement
CassataGames

Panel Control

Jan 10th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class ModifierPanel : MonoBehaviour {
  6.  
  7.     public bool PanelOpen;
  8.     public bool ForceClose;
  9.     public bool FinishedClosing;
  10.  
  11.     public AnimationCurve PositionCurve;
  12.     public AnimationCurve AlphaCurve;
  13.     float Transition;
  14.  
  15.     public GameObject Orienation;
  16.     public GameObject Direction;
  17.     public GameObject Spin;
  18.     public GameObject Power;
  19.  
  20.     public bool UsesOrientation;
  21.     public bool UsesDirection;
  22.     public bool UsesSpin;
  23.     public bool UsesPower;
  24.  
  25.     int NumberUsed;
  26.     int PreviousUsed;
  27.     float start;
  28.     float end;
  29.  
  30.     public float ButtonSize;
  31.     RectTransform RT;
  32.  
  33.     void Start ()
  34.     {        
  35.         RT = gameObject.GetComponent<RectTransform>();
  36.         // Start position is the initial location subtracted by its width.
  37.         start = RT.anchoredPosition.x - RT.rect.width;
  38.         PositionCurve.MoveKey(0, new Keyframe(0, start));
  39.     }
  40.    
  41.     void Update ()
  42.     {
  43.         // See if any of the buttons are in use.
  44.         NumberUsed = 0;
  45.         if (UsesOrientation)
  46.             NumberUsed++;
  47.         if (UsesDirection)
  48.             NumberUsed++;
  49.         if (UsesSpin)
  50.             NumberUsed++;
  51.         if (UsesPower)
  52.             NumberUsed++;
  53.  
  54.         // If the number of buttons in use changes, close the menu.
  55.         if (NumberUsed != PreviousUsed)
  56.         {
  57.             ForceClose = true;
  58.             PreviousUsed = NumberUsed;
  59.         }
  60.         // If the menu is NOT forced closed and at least one button is in use, open it.
  61.         if (NumberUsed > 0 && !ForceClose)
  62.             PanelOpen = true;
  63.         // PanelOpen is what controls the panel animation.
  64.         // Disable it so the panel actually closes.
  65.         if (ForceClose)
  66.             PanelOpen = false;
  67.                
  68.         if (PanelOpen)
  69.         {
  70.             // We only update these variables while the panel is open so they don't change while it's closing to be re-opened.
  71.             Orienation.SetActive(UsesOrientation);
  72.             Direction.SetActive(UsesDirection);
  73.             Spin.SetActive(UsesSpin);
  74.             Power.SetActive(UsesPower);
  75.             HorizontalLayoutGroup LayoutGroup = gameObject.GetComponent<HorizontalLayoutGroup>();
  76.             end = start + (((ButtonSize + LayoutGroup.spacing) * NumberUsed) + LayoutGroup.padding.right);
  77.             PositionCurve.MoveKey(1, new Keyframe(1, end));
  78.         }
  79.  
  80.         // Control the animation of the panel.
  81.         if (PanelOpen && Transition < 1)
  82.             Transition += Time.deltaTime * 3;
  83.         if (!PanelOpen && Transition > 0)
  84.             Transition -= Time.deltaTime * 3;
  85.  
  86.         // If the panel was forced closed but is still valid to open, re-open it.
  87.         if (NumberUsed > 0 && Transition <= 0)
  88.             ForceClose = false;
  89.  
  90.         // Apply animation to the panel itself.
  91.         RT.anchoredPosition = new Vector2(PositionCurve.Evaluate(Transition), RT.anchoredPosition.y);
  92.         CanvasGroup Canvas = gameObject.GetComponent<CanvasGroup>();
  93.         Canvas.alpha = AlphaCurve.Evaluate(Transition);
  94.     }
  95.  
  96.    
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement