// ------------------------------------------------------------------ /// \param _name the name of the animation to play /// \param _index the frame index /// Play the animation by _name, start from the _index of frame // ------------------------------------------------------------------ public void Play(string _name, int _index) { curAnimation = GetAnimation(_name); if (curAnimation != null) { if (_index == 0) { curAnimation.time = 0.0f; } else if (_index > 0 && _index < curAnimation.frameTimes.Count) { curAnimation.time = curAnimation.frameTimes[_index]; } playing = true; if (curAnimation.speed >= 0.0f) { lastEventInfoIndex = -1; } else { lastEventInfoIndex = curAnimation.clip.eventInfos.Count; } exSpriteAnimClip.FrameInfo fi = curAnimation.clip.frameInfos[_index]; if (fi != null) { sprite.SetSprite(fi.atlas, fi.index); } enabled = true; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Step(exSpriteAnimState _animState) { if (_animState == null) { curWrappedTime = 0.0f; curIndex = -1; return; } curWrappedTime = _animState.clip.WrapSeconds(_animState.time, _animState.wrapMode); #if UNITY_FLASH curIndex = _animState.frameTimes.Count - 1; for (int i = 0; i < _animState.frameTimes.Count; ++i) { if (_animState.frameTimes[i] > curWrappedTime) { curIndex = i; break; } } #else curIndex = _animState.frameTimes.BinarySearch(curWrappedTime); if (curIndex < 0) { curIndex = ~curIndex; } #endif }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Init() { if (nameToState == null) { initialized = false; } if (initialized == false) { initialized = true; sprite = GetComponent <exSprite>(); defaultAtlas = sprite.atlas; defaultIndex = sprite.index; nameToState = new Dictionary <string, exSpriteAnimState> (); for (int i = 0; i < animations.Count; ++i) { exSpriteAnimState state = new exSpriteAnimState(animations[i]); nameToState[state.name] = state; } if (defaultAnimation != null) { curAnimation = nameToState[defaultAnimation.name]; } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Play(exSpriteAnimState _animState, float _time) { curAnimation = _animState; if (curAnimation != null) { curAnimation.time = _time; playing = true; if (curAnimation.speed >= 0.0f) { lastEventInfoIndex = curAnimation.clip.GetForwardEventIndex(curAnimation.time); } else { lastEventInfoIndex = curAnimation.clip.GetBackwardEventIndex(curAnimation.time); } Step(curAnimation); exSpriteAnimClip.FrameInfo fi = curAnimation.clip.frameInfos[curIndex]; if (fi != null) { sprite.SetSprite(fi.atlas, fi.index); } enabled = true; } }
// ------------------------------------------------------------------ /// Stop the playing animation, take the action that setup in the /// exSpriteAnimState.stopAction // ------------------------------------------------------------------ public void Stop() { if (curAnimation != null) { // exSpriteAnimClip.StopAction stopAction = curAnimation.stopAction; // curAnimation.time = 0.0f; curAnimation = null; playing = false; // switch (stopAction) { case exSpriteAnimClip.StopAction.DoNothing: // Nothing todo; break; case exSpriteAnimClip.StopAction.DefaultSprite: sprite.SetSprite(defaultAtlas, defaultIndex); break; case exSpriteAnimClip.StopAction.Hide: sprite.enabled = false; break; case exSpriteAnimClip.StopAction.Destroy: GameObject.Destroy(gameObject); break; } } enabled = false; }
// ------------------------------------------------------------------ /// \param _name the name of the animation to play /// \param _time the tiem to play /// Play the animation by _name, start from the _index of frame // ------------------------------------------------------------------ public void Play(string _name, float _time) { curAnimation = GetAnimation(_name); if (curAnimation != null) { Play(curAnimation, _time); } }
// ------------------------------------------------------------------ /// \param _name the name of the animation to play /// \param _frame the frame count /// Play the animation by _name, start from the _frame // ------------------------------------------------------------------ public void Play(string _name, int _frame) { curAnimation = GetAnimation(_name); if (curAnimation != null) { float unitSeconds = 1.0f / curAnimation.clip.sampleRate; float time = _frame * unitSeconds; Play(curAnimation, time); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Update() { if (playing && (curAnimation != null)) { // advance the time and check if we trigger any animation events float delta = Time.deltaTime * curAnimation.speed; float curTime = curAnimation.time; // advance the time curAnimation.time += delta; Step(curAnimation); // save the last state float lastAnimTime = curAnimation.time; exSpriteAnimState lastAnimation = curAnimation; // int newIdx = curAnimation.clip.TriggerEvents(this, lastAnimation, lastEventInfoIndex, curTime, delta, curAnimation.wrapMode); // NOTE: it is possible in the events, user destroy this component. In this case, // the curAnimation will be null. if (curAnimation == null || curAnimation != lastAnimation || lastAnimTime != curAnimation.time /*NOTE: it is possible in the event we reply the same animation*/) { return; } lastEventInfoIndex = newIdx; // set sprite to current time exSpriteAnimClip.FrameInfo fi = GetCurFrameInfo(); if (fi != null) { sprite.SetSprite(fi.atlas, fi.index); } // check if stop if (curAnimation.wrapMode == WrapMode.Once || curAnimation.wrapMode == WrapMode.Default) { if ((curAnimation.speed > 0.0f && curAnimation.time >= curAnimation.length) || (curAnimation.speed < 0.0f && curAnimation.time <= 0.0f)) { Stop(); } } } }
// ------------------------------------------------------------------ /// \param _name the name of the animation /// \param _index the frame index /// Get the texture used in the _index of frames in the animation by _name, /// and set it as the default picture of the sprite // ------------------------------------------------------------------ public void SetFrame(string _name, int _index) { exSpriteAnimState animState = GetAnimation(_name); if (animState != null && _index >= 0 && _index < animState.clip.frameInfos.Count) { exSpriteAnimClip.FrameInfo fi = animState.clip.frameInfos[_index]; sprite.SetSprite(fi.atlas, fi.index); } }
// ------------------------------------------------------------------ /// \param _animClip the sprite animation clip wants to add /// \return the instantiate animation state of the added _animClip /// Add a sprite animation clip, create a new animation state and saves /// it to the lookup table by the name of the clip /// /// \note if the animation already in the exSpriteAnimation.animations, /// it will do nothing // ------------------------------------------------------------------ public exSpriteAnimState AddAnimation( exSpriteAnimClip _animClip ) { // if we already have the animation, just return the animation state if ( animations.IndexOf(_animClip) != -1 ) { return nameToState[_animClip.name]; } // animations.Add (_animClip); exSpriteAnimState state = new exSpriteAnimState(_animClip); nameToState[state.name] = state; return state; }
// ------------------------------------------------------------------ /// \param _animClip the sprite animation clip wants to add /// \return the instantiate animation state of the added _animClip /// Add a sprite animation clip, create a new animation state and saves /// it to the lookup table by the name of the clip /// /// \note if the animation already in the exSpriteAnimation.animations, /// it will do nothing // ------------------------------------------------------------------ public exSpriteAnimState AddAnimation(exSpriteAnimClip _animClip) { // if we already have the animation, just return the animation state if (animations.IndexOf(_animClip) != -1) { return(nameToState[_animClip.name]); } // animations.Add(_animClip); exSpriteAnimState state = new exSpriteAnimState(_animClip); nameToState[state.name] = state; return(state); }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Init() { if (initialized == false) { initialized = true; sprite = GetComponent <exSprite>(); defaultAtlas = sprite.atlas; defaultIndex = sprite.index; nameToState = new Dictionary <string, exSpriteAnimState> (); foreach (exSpriteAnimClip clip in animations) { exSpriteAnimState state = new exSpriteAnimState(clip); nameToState[state.name] = state; } if (defaultAnimation != null) { curAnimation = nameToState[defaultAnimation.name]; } } }
// ------------------------------------------------------------------ /// \param _name the name of animation state you want to add /// \param _animClip the sprite animation clip wants to add /// \return the instantiate animation state of the added _animClip /// Add a sprite animation clip, create a new animation state and saves /// it to the lookup table by the name of the clip /// /// \note if the animation already in the exSpriteAnimation.animations, /// it will override the old clip and return a new animation state. // ------------------------------------------------------------------ public exSpriteAnimState AddAnimation(string _name, exSpriteAnimClip _animClip) { Init(); exSpriteAnimState state = null; // if we already have the animation, just return the animation state if (animations.IndexOf(_animClip) != -1) { state = nameToState[_name]; if (state.clip != _animClip) { state = new exSpriteAnimState(_name, _animClip); nameToState[_name] = state; } return(state); } // animations.Add(_animClip); state = new exSpriteAnimState(_name, _animClip); nameToState[_name] = state; return(state); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ int BackwardTriggerEvents(exSpriteAnimation _spAnim, exSpriteAnimState _lastAnim, int _index, float _start, float _end, bool _includeStart) { int idx = _index; exSpriteAnimState curAnim = _spAnim.GetCurrentAnimation(); for (int i = _index - 1; i >= 0; --i) { EventInfo ei = eventInfos[i]; if (ei.time == _start && _includeStart == false) { idx = i; continue; } if (ei.time >= _end) { Trigger(_spAnim, ei); if (curAnim == null || _lastAnim != curAnim) { return(-1); } idx = i; } else { break; } } return(idx); }
// ------------------------------------------------------------------ /// \param _name the name of animation state you want to add /// \param _animClip the sprite animation clip wants to add /// \return the instantiate animation state of the added _animClip /// Add a sprite animation clip, create a new animation state and saves /// it to the lookup table by the name of the clip /// /// \note if the animation already in the exSpriteAnimation.animations, /// it will override the old clip and return a new animation state. // ------------------------------------------------------------------ public exSpriteAnimState AddAnimation ( string _name, exSpriteAnimClip _animClip ) { exSpriteAnimState state = null; // if we already have the animation, just return the animation state if ( animations.IndexOf(_animClip) != -1 ) { state = nameToState[_name]; if ( state.clip != _animClip ) { state = new exSpriteAnimState( _name, _animClip ); nameToState[_name] = state; } return state; } // animations.Add (_animClip); state = new exSpriteAnimState( _name, _animClip ); nameToState[_name] = state; return state; }
// ------------------------------------------------------------------ /// Stop the playing animation, take the action that setup in the /// exSpriteAnimState.stopAction // ------------------------------------------------------------------ public void Stop () { if ( curAnimation != null ) { // exSpriteAnimClip.StopAction stopAction = curAnimation.stopAction; // curAnimation.time = 0.0f; curAnimation = null; playing = false; // switch ( stopAction ) { case exSpriteAnimClip.StopAction.DoNothing: // Nothing todo; break; case exSpriteAnimClip.StopAction.DefaultSprite: sprite.SetSprite( defaultAtlas, defaultIndex ); break; case exSpriteAnimClip.StopAction.Hide: sprite.enabled = false; break; case exSpriteAnimClip.StopAction.Destroy: GameObject.Destroy(gameObject); break; } } enabled = false; }
// ------------------------------------------------------------------ /// \param _name the name of the animation to play /// \param _index the frame index /// Play the animation by _name, start from the _index of frame // ------------------------------------------------------------------ public void Play ( string _name, int _index ) { curAnimation = GetAnimation(_name); if ( curAnimation != null ) { if ( _index == 0 ) curAnimation.time = 0.0f; else if ( _index > 0 && _index < curAnimation.frameTimes.Count ) curAnimation.time = curAnimation.frameTimes[_index-1]; playing = true; if ( curAnimation.speed >= 0.0f ) lastEventInfoIndex = -1; else lastEventInfoIndex = curAnimation.clip.eventInfos.Count; exSpriteAnimClip.FrameInfo fi = curAnimation.clip.frameInfos[_index]; if ( fi != null ) sprite.SetSprite ( fi.atlas, fi.index ); enabled = true; } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Init () { if ( initialized == false ) { initialized = true; sprite = GetComponent<exSprite>(); defaultAtlas = sprite.atlas; defaultIndex = sprite.index; nameToState = new Dictionary<string,exSpriteAnimState> (); foreach ( exSpriteAnimClip clip in animations ) { exSpriteAnimState state = new exSpriteAnimState(clip); nameToState[state.name] = state; } if ( defaultAnimation != null ) curAnimation = nameToState[defaultAnimation.name]; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Step( exSpriteAnimState _animState ) { if ( _animState == null ) { curWrappedTime = 0.0f; curIndex = -1; return; } curWrappedTime = _animState.clip.WrapSeconds(_animState.time, _animState.wrapMode); #if UNITY_FLASH curIndex = _animState.frameTimes.Count - 1; for ( int i = 0; i < _animState.frameTimes.Count; ++i ) { if ( _animState.frameTimes[i] > curWrappedTime ) { curIndex = i; break; } } #else curIndex = _animState.frameTimes.BinarySearch(curWrappedTime); if ( curIndex < 0 ) { curIndex = ~curIndex; } #endif }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Play( exSpriteAnimState _animState, float _time ) { curAnimation = _animState; if ( curAnimation != null ) { curAnimation.time = _time; playing = true; if ( curAnimation.speed >= 0.0f ) { lastEventInfoIndex = curAnimation.clip.GetForwardEventIndex(curAnimation.time); } else { lastEventInfoIndex = curAnimation.clip.GetBackwardEventIndex(curAnimation.time); } Step (curAnimation); exSpriteAnimClip.FrameInfo fi = curAnimation.clip.frameInfos[curIndex]; if ( fi != null ) sprite.SetSprite ( fi.atlas, fi.index ); enabled = true; } }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Init() { if ( nameToState == null ) { initialized = false; } if ( initialized == false ) { initialized = true; sprite = GetComponent<exSprite>(); defaultAtlas = sprite.atlas; defaultIndex = sprite.index; nameToState = new Dictionary<string,exSpriteAnimState> (); for ( int i = 0; i < animations.Count; ++i ) { exSpriteAnimState state = new exSpriteAnimState(animations[i]); nameToState[state.name] = state; } if ( defaultAnimation != null ) curAnimation = nameToState[defaultAnimation.name]; } }
// ------------------------------------------------------------------ /// \param _name the name of the animation to play /// \param _time the tiem to play /// Play the animation by _name, start from the _index of frame // ------------------------------------------------------------------ public void Play( string _name, float _time ) { curAnimation = GetAnimation(_name); if ( curAnimation != null ) { Play ( curAnimation, _time ); } }
// ------------------------------------------------------------------ /// \param _name the name of the animation to play /// \param _frame the frame count /// Play the animation by _name, start from the _frame // ------------------------------------------------------------------ public void Play( string _name, int _frame ) { curAnimation = GetAnimation(_name); if ( curAnimation != null ) { float unitSeconds = 1.0f/curAnimation.clip.sampleRate; float time = _frame * unitSeconds; Play ( curAnimation, time ); } }
// ------------------------------------------------------------------ /// \param _spAnim send message to target _spAnim.gameObject /// \param _lastAnim last animation state /// \param _lastIndex last triggered event info index (-1 means from start) /// \param _start the start time in seconds /// \param _delta the delta time in seconds /// \param _wrapMode the wrap mode /// \return return the last triggered event index /// Trigger events locate between the start and start+_delta time span // ------------------------------------------------------------------ public int TriggerEvents(exSpriteAnimation _spAnim, exSpriteAnimState _lastAnim, int _lastIndex, float _start, float _delta, WrapMode _wrapMode) { if (eventInfos.Count == 0) { return(-1); } if (_delta == 0.0f) { return(-1); } // WrapSeconds float t = WrapSeconds(_start, _wrapMode); // if we are the just start playing if (_lastIndex == -1) { return(ForwardTriggerEvents(_spAnim, _lastAnim, -1, t, t + _delta, true)); } // if (_wrapMode == WrapMode.PingPong) { int cnt = (int)(_start / length); if (cnt % 2 == 1) { _delta = -_delta; } } // if we are play forward if (_delta > 0.0f) { if (t + _delta > length) { if (_wrapMode == WrapMode.Loop) { float rest = t + _delta - length; ForwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, length, false); exSpriteAnimState curAnim = _spAnim.GetCurrentAnimation(); if (curAnim == null || _lastAnim != curAnim) { return(-1); } return(ForwardTriggerEvents(_spAnim, _lastAnim, -1, 0.0f, rest, true)); } else if (_wrapMode == WrapMode.PingPong) { float rest = t + _delta - length; ForwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, length, false); exSpriteAnimState curAnim = _spAnim.GetCurrentAnimation(); if (curAnim == null || _lastAnim != curAnim) { return(-1); } return(BackwardTriggerEvents(_spAnim, _lastAnim, eventInfos.Count, length, length - rest, false)); } else { return(ForwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, t + _delta, false)); } } else { return(ForwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, t + _delta, false)); } } else { if (t + _delta < 0.0f) { if (_wrapMode == WrapMode.Loop) { float rest = 0.0f - (t + _delta); BackwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, 0.0f, false); exSpriteAnimState curAnim = _spAnim.GetCurrentAnimation(); if (curAnim == null || _lastAnim != curAnim) { return(-1); } return(BackwardTriggerEvents(_spAnim, _lastAnim, eventInfos.Count, length, length - rest, true)); } else if (_wrapMode == WrapMode.PingPong) { float rest = 0.0f - (t + _delta); BackwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, 0.0f, false); exSpriteAnimState curAnim = _spAnim.GetCurrentAnimation(); if (curAnim == null || _lastAnim != curAnim) { return(-1); } return(ForwardTriggerEvents(_spAnim, _lastAnim, -1, 0.0f, rest, false)); } else { return(BackwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, t + _delta, false)); } } else { return(BackwardTriggerEvents(_spAnim, _lastAnim, _lastIndex, t, t + _delta, false)); } } }
// ------------------------------------------------------------------ /// \param _name the name of the animation to play /// \param _index the frame index /// Play the animation by _name, start from the _index of frame // ------------------------------------------------------------------ public void Play( string _name, int _index ) { curAnimation = GetAnimation(_name); if ( curAnimation != null ) { if ( _index == 0 ) curAnimation.time = 0.0f; else if ( _index > 0 && _index < curAnimation.frameTimes.Count ) curAnimation.time = curAnimation.frameTimes[_index]; playing = true; paused = false; } }
// ------------------------------------------------------------------ /// \param _name the name of the animation /// \param _index the frame index /// Get the texture used in the _index of frames in the animation by _name, /// and set it as the default picture of the sprite // ------------------------------------------------------------------ public void SetFrame( string _name, int _index ) { curAnimation = GetAnimation(_name); if ( curAnimation != null && _index >= 0 && _index < curAnimation.clip.frameInfos.Count ) { exSpriteAnimClip.FrameInfo fi = curAnimation.clip.frameInfos[_index]; sprite.SetSprite ( fi.atlas, fi.index ); } }