Pour Moons (sisi
![Dan.San :mrgreen:](./images/smilies/14.gif)
Exemple d'Animator:
![Image](https://img15.hostingpics.net/pics/588067mooncontroller.jpg)
Les délais entre animations étant un passage obligé, j'ai commencé par utiliser l'Invoke d'Unity, malgré sa mauvaise réputation du au GC.
Après moult tests, j'en suis maintenant a préférer une solution via l'API standard de Mono/C# : le System.Timers.Timer
Un exemple valant mieux que de longs discours
Les deux codes activent une succession de timers avant de boucler le processus. Jetez un œil au Profiler (et à la fenêtre de Stats) . C'est édifiant surtout avec les Debug.Log "activés"
Version MonoBehaviour.Invoke
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Timers;
public class CInvoke : MonoBehaviour {
void Start () {
Invoke("OnIdleMove", 1.0f);
}
private void OnIdleMove() {
Debug.Log("OnIdleMove");
Invoke("OnIdleRandom", 0.010f);
}
private void OnIdleRandom() {
Debug.Log("OnIdleRandom");
Invoke("OnInitSpeed", 0.010f);
}
private void OnInitSpeed() {
Debug.Log("OnInitSpeed");
Invoke("OnNextMove", 0.010f);
}
private void OnNextMove() {
Debug.Log("OnNextMove");
Invoke("OnStopVitesse", 0.010f);
}
private void OnStopVitesse() {
Debug.Log("OnStopVitesse");
Invoke("OnIdleMove", 0.010f);
}
void Update(){
if (Input.GetKeyDown("q")){
if(EditorApplication.isPlaying)
{
UnityEditor.EditorApplication.isPlaying = false;
}
enabled = false; // déactivation du script
}
}
}
Version System.Timers.Time
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Timers;
public class CTimer : MonoBehaviour {
private System.Timers.Timer Timer_IdleMove;
private System.Timers.Timer Timer_IdleRandom;
private System.Timers.Timer Timer_InitSpeed;
private System.Timers.Timer Timer_NextMove;
private System.Timers.Timer Timer_StopVitesse;
private const int _OnIdleMove = 0;
private const int _OnIdleRandom = 1;
private const int _OnInitSpeed = 2;
private const int _OnNextMove = 3;
private const int _OnStopVitesse = 4;
private void Start () {
Timer_IdleMove = new System.Timers.Timer();
// Timer_IdleMove.Elapsed += new ElapsedEventHandler(OnIdleMove);
Timer_IdleMove.Elapsed += OnIdleMove; //ou
Timer_IdleRandom = new System.Timers.Timer();
Timer_IdleRandom.Elapsed += OnIdleRandom;
Timer_InitSpeed = new System.Timers.Timer();
Timer_InitSpeed.Elapsed += OnInitSpeed;
Timer_NextMove = new System.Timers.Timer();
Timer_NextMove.Elapsed += OnNextMove;
Timer_StopVitesse = new System.Timers.Timer();
Timer_StopVitesse.Elapsed += OnStopVitesse;
_Invoke(_OnIdleMove, 1000);
}
private void _Invoke(int func, float delais){
switch (func) {
case _OnIdleMove:
Timer_IdleMove.Interval = delais;
Timer_IdleMove.Start();
break;
case _OnIdleRandom:
Timer_IdleRandom.Interval = delais;
Timer_IdleRandom.Start();
break;
case _OnInitSpeed:
Timer_InitSpeed.Interval = delais;
Timer_InitSpeed.Start();
break;
case _OnNextMove:
Timer_NextMove.Interval = delais;
Timer_NextMove.Start();
break;
case _OnStopVitesse:
Timer_StopVitesse.Interval = delais;
Timer_StopVitesse.Start();
break;
}
}
private void OnIdleMove(object source, ElapsedEventArgs e) {
Timer_IdleMove.Stop();
Debug.Log("OnIdleMove");
_Invoke(_OnIdleRandom, 10);
}
private void OnIdleRandom(object source, ElapsedEventArgs e) {
Timer_IdleRandom.Stop();
Debug.Log("OnIdleRandom");
_Invoke(_OnInitSpeed, 10);
}
private void OnInitSpeed(object source, ElapsedEventArgs e) {
Timer_InitSpeed.Stop();
Debug.Log("OnInitSpeed");
_Invoke(_OnNextMove, 10);
}
private void OnNextMove(object source, ElapsedEventArgs e) {
Timer_NextMove.Stop();
Debug.Log("OnNextMove");
_Invoke(_OnStopVitesse, 10);
}
private void OnStopVitesse(object source, ElapsedEventArgs e) {
Timer_StopVitesse.Stop();
Debug.Log("OnStopVitesse");
_Invoke(_OnIdleMove, 10);
}
private void OnDisable() {
Timer_IdleMove.Close();
Timer_IdleRandom.Close();
Timer_InitSpeed.Close();
Timer_NextMove.Close();
Timer_StopVitesse.Close();
}
private void Update(){
if (Input.GetKeyDown("q")){
OnDisable();
if(EditorApplication.isPlaying)
{
UnityEditor.EditorApplication.isPlaying = false;
}
enabled = false; // déactivation du script
}
}
}