Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created October 12, 2021 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtdekker/f8d77ae0837106f356f4884ab80f4575 to your computer and use it in GitHub Desktop.
Save kurtdekker/f8d77ae0837106f356f4884ab80f4575 to your computer and use it in GitHub Desktop.
Simple focused-button animator
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;
}
}
@kurtdekker
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment