123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using UnityEngine;
- using System.Collections.Generic;
- [System.Serializable]
- public class MegaPointCacheAnimClip
- {
- public string name;
- public float start;
- public float end;
- public MegaRepeatMode loop;
- public float speed = 1.0f;
- public MegaPointCacheAnimClip(string _name, float _start, float _end, MegaRepeatMode _loop)
- {
- name = _name;
- start = _start;
- end = _end;
- loop = _loop;
- }
- }
- [AddComponentMenu("Modifiers/Point Cache Animator")]
- [ExecuteInEditMode]
- public class MegaPointCacheAnimator : MonoBehaviour
- {
- public MegaPointCache mod;
- public MegaPointCacheRef modref;
- public List<MegaPointCacheAnimClip> clips = new List<MegaPointCacheAnimClip>();
- public int current = 0;
- public float t = -1.0f; // Current clip time
- public float at = 0.0f;
- public int sourceFPS = 30;
- public bool useFrames = true;
- [ContextMenu("Help")]
- public void Help()
- {
- Application.OpenURL("http://www.west-racing.com/mf/?page_id=1802");
- }
- public bool IsPlaying()
- {
- if ( t >= 0.0f )
- return true;
- return false;
- }
- public void SetTime(float time)
- {
- t = time;
- }
- public float GetTime()
- {
- return at;
- }
- public void PlayClipEvent(int i)
- {
- PlayClip(i);
- }
- public void PlayClipNameEvent(string name)
- {
- PlayClip(name);
- }
- public void PlayClip(int i)
- {
- if ( i < clips.Count )
- {
- current = i;
- t = 0.0f;
- }
- }
- public void PlayClip(string name)
- {
- for ( int i = 0; i < clips.Count; i++ )
- {
- if ( clips[i].name == name )
- {
- current = i;
- t = 0.0f;
- return;
- }
- }
- }
- public void Stop()
- {
- t = -1.0f;
- }
- public int AddClip(string name, float start, float end, MegaRepeatMode loop)
- {
- MegaPointCacheAnimClip clip = new MegaPointCacheAnimClip(name, start, end, loop);
- clips.Add(clip);
- return clips.Count - 1;
- }
- public string[] GetClipNames()
- {
- string[] names = new string[clips.Count];
- for ( int i = 0; i < clips.Count; i++ )
- {
- names[i] = clips[i].name;
- }
- return names;
- }
- void Start()
- {
- if ( PlayOnStart )
- {
- t = 0.0f;
- }
- else
- t = -1.0f;
- }
- void Update()
- {
- DoUpdate();
- }
- void DoUpdate()
- {
- if ( mod == null && modref == null )
- {
- mod = (MegaPointCache)GetComponent<MegaPointCache>();
- modref = (MegaPointCacheRef)GetComponent<MegaPointCacheRef>();
- }
- if ( mod != null || modref != null )
- {
- if ( LinkedUpdate )
- {
- DoLinkedUpdate();
- }
- else
- {
- if ( clips.Count > 0 && current < clips.Count )
- {
- if ( t >= 0.0f )
- {
- t += Time.deltaTime * clips[current].speed;
- float dt = clips[current].end - clips[current].start;
- switch ( clips[current].loop )
- {
- //case MegaRepeatMode.Loop: at = Mathf.Repeat(t, dt); break;
- case MegaRepeatMode.Loop:
- at = Mathf.Repeat(t, Mathf.Abs(dt));
- if ( dt < 0.0f )
- at = clips[current].start - at;
- break;
- case MegaRepeatMode.PingPong: at = Mathf.PingPong(t, dt); break;
- case MegaRepeatMode.Clamp: at = Mathf.Clamp(t, 0.0f, dt); break;
- }
- at += clips[current].start;
- if ( mod )
- mod.SetAnim(at);
- else
- modref.SetAnim(at);
- }
- }
- }
- }
- }
- public bool LinkedUpdate = false;
- public bool PlayOnStart = true;
- Animation myanim;
- void DoLinkedUpdate()
- {
- if ( myanim == null )
- myanim = GetComponent<Animation>();
- if ( myanim != null )
- {
- foreach ( AnimationState state in myanim )
- {
- if ( state.enabled )
- {
- AnimationClip clip = state.clip;
- if ( clip != null )
- {
- for ( int i = 0; i < clips.Count; i++ )
- {
- if ( clips[i].name == clip.name )
- {
- current = i;
- float ct = state.time;
- WrapMode wm = clip.wrapMode;
- if ( wm == WrapMode.Default )
- {
- wm = myanim.wrapMode;
- }
- switch ( clip.wrapMode )
- {
- case WrapMode.Loop:
- ct = Mathf.Repeat(ct, clip.length);
- break;
- case WrapMode.PingPong:
- ct = Mathf.PingPong(ct, clip.length);
- break;
- case WrapMode.ClampForever:
- ct = Mathf.Clamp(ct, 0.0f, clip.length);
- break;
- case WrapMode.Once:
- if ( ct > clip.length )
- ct = 0.0f;
- break;
- }
- ct += clips[current].start;
- if ( mod )
- mod.SetAnim(ct); //state.time + clips[current].start);
- else
- modref.SetAnim(ct);
- return;
- }
- }
- }
- }
- }
- }
- }
- }
|