Skip to content

Instantly share code, notes, and snippets.

@Anthelmed
Created December 10, 2021 18:44
Show Gist options
  • Save Anthelmed/1028db281dfcf7578d513abf3e418c70 to your computer and use it in GitHub Desktop.
Save Anthelmed/1028db281dfcf7578d513abf3e418c70 to your computer and use it in GitHub Desktop.
Using Unity UIToolkit transition animation system to animate a GameObject
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.UIElements.Experimental;
public class Animation : MonoBehaviour
{
[SerializeField] private Transform transform;
[SerializeField] private UIDocument uiDocument;
[SerializeField] private int durationMs = 3000;
private void Start()
{
PlayRotateAnimation();
}
private void PlayRotateAnimation()
{
var visualElement = uiDocument.rootVisualElement.Q<VisualElement>("Animator");
var currentRotation = transform.rotation.eulerAngles.y;
visualElement.experimental.animation.Start(currentRotation, currentRotation + 360f, durationMs,
(element, value) =>
{
//element.style.height = value;
var rotation = transform.rotation.eulerAngles;
rotation.y = value;
transform.rotation = Quaternion.Euler(rotation);
})
.Ease(Easing.Linear)
.OnCompleted(PlayRotateAnimation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment