Created
October 12, 2021 18:19
-
-
Save kurtdekker/f8d77ae0837106f356f4884ab80f4575 to your computer and use it in GitHub Desktop.
Simple focused-button animator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.EventSystems; | |
// @kurtdekker | |
// cheap and cheerful "increase the size of the focused button" | |
public class ScaleOnFocus : MonoBehaviour | |
{ | |
[Header( "Percent increase +10%, etc.")] | |
public float PercentIncrease = 10; | |
[Header("How quickly it changes")] | |
public float Snappiness = 1.0f; | |
float currentScale; | |
float desiredScale; | |
private void Start() | |
{ | |
currentScale = transform.localScale.x; | |
} | |
private void Update() | |
{ | |
desiredScale = 1.0f; | |
if (EventSystem.current) | |
{ | |
if (EventSystem.current.currentSelectedGameObject == gameObject) | |
{ | |
desiredScale = 1.0f + PercentIncrease / 100.0f; | |
} | |
} | |
currentScale = Mathf.MoveTowards(currentScale, desiredScale, Snappiness * Time.deltaTime); | |
transform.localScale = Vector3.one * currentScale; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To see it in action, make a UI with a handful of buttons and navigation set up between them.
Drop this script onto each one of the buttons.
Press play and go interact with your buttons.
Yay simplicity!