Je trouve que les animations d'unity s'adaptent assez mal aux interfaces, il m'arrive d'avoir des grosses baisse de frames juste pour déplacer un bouton ou un menu De plus ça prends je trouve trop de temps à implémenter
Donc je me suis créer un petit système qui me facilite la vie et je me suis dit que ça pourrait peut être vous plaire
Je tiens à préciser que le script n'est pas incroyablement travaillé et qu'il peut être très facilement amélioré
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class InterfaceAnimator : MonoBehaviour {
public Animation[] animations;
// C'est ici qu'est initialisé les animations ex : faire en sorte que les interfaces aparaissent
void Awake () {
foreach (Animation animation in animations) {
animation.StartInitialization ();
if (animation.isOpen) {
if (animation.mainCoroutine != null)
StopCoroutine (animation.mainCoroutine);
animation.mainCoroutine = Animate (animation, true);
StartCoroutine (animation.mainCoroutine);
}
}
}
// La coroutine qui va venir animer une action
public IEnumerator Action (Action action, bool isOpen) {
yield return new WaitForSeconds (action.startDelay);
Vector2 to = (isOpen == action.state) ? action.openPos : action.closePos;
float percent = 0;
while (percent < 1) {
percent += Time.deltaTime / action.duration;
action.graphics.anchoredPosition = Vector2.Lerp (action.graphics.anchoredPosition, to, percent);
yield return null;
}
}
// La coroutine qui va venir animer chaque ations de l'animation
public IEnumerator Animate (Animation animation, bool awake) {
if (awake)
yield return new WaitForSeconds (animation.awakeDelay);
foreach (Action action in animation.actions) {
if (action.coroutine != null)
StopCoroutine (action.coroutine);
action.coroutine = Action (action, animation.isOpen);
StartCoroutine (action.coroutine);
}
}
// C'est cet fonction qui est nécessaire d'appeler pour engendrer une animation
public void CallAnimation (string name) {
Animation animation = GetAnimations (name);
animation.isOpen = !animation.isOpen;
if (animation.mainCoroutine != null)
StopCoroutine (animation.mainCoroutine);
animation.mainCoroutine = Animate (animation, false);
StartCoroutine (animation.mainCoroutine);
foreach (CallBack callback in animation.callbacks) {
Animation callbackAnimation = GetAnimations (callback.name);
callbackAnimation.isOpen = callback.state;
if (callback.followBaseState)
callbackAnimation.isOpen = (callback.state == animation.isOpen);
if (callbackAnimation.mainCoroutine != null)
StopCoroutine (callbackAnimation.mainCoroutine);
callbackAnimation.mainCoroutine = Animate (callbackAnimation, false);
StartCoroutine (callbackAnimation.mainCoroutine);
}
}
// Ce constructeur retourne l'animation souhaitée
public Animation GetAnimations (string animationName) {
Animation tryAnimation = animations [0];
foreach (Animation menu in animations) {
if (menu.name == animationName)
tryAnimation = menu;
}
return tryAnimation;
}
}
// Une action est le déplacement d'un élement d'interface
[System.Serializable]
public class Action {
public RectTransform graphics;
public float duration;
public bool state;
public float startDelay;
public bool startInitialisation;
public bool openOrCloseInitialization;
public Vector2 openPos;
public Vector2 closePos;
public IEnumerator coroutine;
public void StartInitialization () {
graphics.anchoredPosition = (openOrCloseInitialization) ? openPos : closePos;
}
}
// Une animation c'est ensemble d'actions
[System.Serializable]
public class Animation {
public string name;
public float awakeDelay;
public bool isOpen;
public Action[] actions;
public IEnumerator mainCoroutine;
public void StartInitialization () {
foreach (Action action in actions) {
if (action.startInitialisation)
action.StartInitialization ();
}
}
public CallBack[] callbacks;
}
// Une callback c'est une animation appelée par une autre animation
[System.Serializable]
public class CallBack {
public string name;
public bool followBaseState;
public bool state;
}