Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class ModifierPanel : MonoBehaviour {
- public bool PanelOpen;
- public bool ForceClose;
- public bool FinishedClosing;
- public AnimationCurve PositionCurve;
- public AnimationCurve AlphaCurve;
- float Transition;
- public GameObject Orienation;
- public GameObject Direction;
- public GameObject Spin;
- public GameObject Power;
- public bool UsesOrientation;
- public bool UsesDirection;
- public bool UsesSpin;
- public bool UsesPower;
- int NumberUsed;
- int PreviousUsed;
- float start;
- float end;
- public float ButtonSize;
- RectTransform RT;
- void Start ()
- {
- RT = gameObject.GetComponent<RectTransform>();
- // Start position is the initial location subtracted by its width.
- start = RT.anchoredPosition.x - RT.rect.width;
- PositionCurve.MoveKey(0, new Keyframe(0, start));
- }
- void Update ()
- {
- // See if any of the buttons are in use.
- NumberUsed = 0;
- if (UsesOrientation)
- NumberUsed++;
- if (UsesDirection)
- NumberUsed++;
- if (UsesSpin)
- NumberUsed++;
- if (UsesPower)
- NumberUsed++;
- // If the number of buttons in use changes, close the menu.
- if (NumberUsed != PreviousUsed)
- {
- ForceClose = true;
- PreviousUsed = NumberUsed;
- }
- // If the menu is NOT forced closed and at least one button is in use, open it.
- if (NumberUsed > 0 && !ForceClose)
- PanelOpen = true;
- // PanelOpen is what controls the panel animation.
- // Disable it so the panel actually closes.
- if (ForceClose)
- PanelOpen = false;
- if (PanelOpen)
- {
- // We only update these variables while the panel is open so they don't change while it's closing to be re-opened.
- Orienation.SetActive(UsesOrientation);
- Direction.SetActive(UsesDirection);
- Spin.SetActive(UsesSpin);
- Power.SetActive(UsesPower);
- HorizontalLayoutGroup LayoutGroup = gameObject.GetComponent<HorizontalLayoutGroup>();
- end = start + (((ButtonSize + LayoutGroup.spacing) * NumberUsed) + LayoutGroup.padding.right);
- PositionCurve.MoveKey(1, new Keyframe(1, end));
- }
- // Control the animation of the panel.
- if (PanelOpen && Transition < 1)
- Transition += Time.deltaTime * 3;
- if (!PanelOpen && Transition > 0)
- Transition -= Time.deltaTime * 3;
- // If the panel was forced closed but is still valid to open, re-open it.
- if (NumberUsed > 0 && Transition <= 0)
- ForceClose = false;
- // Apply animation to the panel itself.
- RT.anchoredPosition = new Vector2(PositionCurve.Evaluate(Transition), RT.anchoredPosition.y);
- CanvasGroup Canvas = gameObject.GetComponent<CanvasGroup>();
- Canvas.alpha = AlphaCurve.Evaluate(Transition);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement