Created
December 10, 2021 18:44
-
-
Save Anthelmed/1028db281dfcf7578d513abf3e418c70 to your computer and use it in GitHub Desktop.
Using Unity UIToolkit transition animation system to animate a GameObject
This file contains hidden or 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.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