public void Play(string name, bool isFlipX, bool isBack = false) { mSpriteRenderer.flipX = isFlipX; if (mStateMap.ContainsKey(name)) { if (mCurState != null) { if (mCurState != mStateMap[name]) { //切换动画 mCurState.Stop(); mCurState = mStateMap[name]; mCurState.IsBack = isBack; mCurState.Start(); } else { if (mCurState.IsOver) { //播放完了,重新播放 mCurState.IsBack = isBack; mCurState.Start(); } } } else if (mCurState == null) { //第一次播放 mCurState = mStateMap[name]; mCurState.IsBack = isBack; mCurState.Start(); } } }
private void SetAnimationState(SpriteAnimationState state) { if (spriteAnimationState != state) { spriteAnimationState = state; switch (spriteAnimationState) { case SpriteAnimationState.Standing: { spriteAnimator.SetTrigger("Standing"); break; } case SpriteAnimationState.Running: { spriteAnimator.SetTrigger("Running"); break; } case SpriteAnimationState.Jumping: { spriteAnimator.SetTrigger("Jumping"); break; } case SpriteAnimationState.Falling: { spriteAnimator.SetTrigger("Falling"); break; } } } }
public void Init(List <SpriteAnimationClip> clipList) { for (int i = 0; i < clipList.Count; i++) { SpriteAnimationState state = new SpriteAnimationState(); state.Clip = clipList[i]; state.WrapMode = clipList[i].WrapMode; mStateMap.Add(clipList[i].Name, state); } }
static bool checkPlayingQueue(SpriteAnimationState item) { if ( ! item._animation.IsLayerPlaying(item.layer) ) { item.time = 0f; item.weight = 1f; item.enabled = true; return true; } return false; }
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (_animation == null) { _animation = animator.GetComponent <SpriteAnimation>(); } _animation.StopAll(); _animation.Play(Clip.name); _animation.CalculateData(); _startTime = Time.time; if (_state == null) { _state = _animation[Clip.name]; } _length = _animation[Clip.name].length * (_state.wrapMode == WrapMode.PingPong ? 2 : 1); _isLoop = _state.wrapMode == WrapMode.Loop || _state.wrapMode == WrapMode.PingPong; }
void StopLayerExcept(int layer, SpriteAnimationState s) { foreach (SpriteAnimationState state in allStates) { if (state.layer == layer && state != s) Stop(state); } }
static bool removeClip(SpriteAnimationState state) { bool needRemove = state.clip == _removeClip; if (needRemove) { state._animation.animationStates.Remove(state.name); SpriteAnimationState.ReleaseState(state); } return needRemove; }
internal void AddState(SpriteAnimationState state) { animationStates.Add(state.name, state); allStates.Add(state); }
void RemoveSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash) { SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash(component._fullPathHash + parentHash ); if (sprTransfrom != null) { sprTransfrom.DetachState(state, component); if (sprTransfrom.attachStateComponent.Count <= 0) { if (sprTransfrom.prve != null) sprTransfrom.prve.next = sprTransfrom.next; if (sprTransfrom.next != null) sprTransfrom.next.prve = sprTransfrom.prve; if (sprTransfrom.parent != null && sprTransfrom.parent.firstChild == sprTransfrom) sprTransfrom.parent.firstChild = sprTransfrom.next; for (SpriteTransform child = sprTransfrom.firstChild; child != null; child = child.next) child.parent = null; { sprTransfrom.parent = null; sprTransfrom.next = null; sprTransfrom.prve = null; sprTransfrom.firstChild = null; sprTransfrom.lastChild = null; } if (stateRoot.Contains(sprTransfrom)) stateRoot.Remove(sprTransfrom); spriteRenderer.DetachSprite(sprTransfrom.id); } } for (int i = 0, e = component.children.Length; i < e; i++) RemoveSprite(state, state.clip.subComponents[component.children[i]], sprTransfrom, 0); }
void OnGUI() { GUILayout.BeginArea (new Rect (25, 45, 150, 500)); GUILayout.BeginHorizontal(); pos = GUILayout.BeginScrollView(pos); for( int i = 0, e = animatinoNames.Length; i < e; i++) { //If click button, then play this clip. if ( GUILayout.Button( animatinoNames[i] ) ) { //Get clip's playing state currState = spriteAnimation[ animatinoNames[i] ]; } } GUILayout.EndScrollView(); GUILayout.EndHorizontal(); if ( currState != null ) { SpriteAnimationState clipState = currState; //Pause and resume clip if ( GUILayout.Button( clipState.enabled ? "Disable" : "Enable" ) ) { clipState.enabled = !clipState.enabled; } //Pause and resume clip if ( GUILayout.Button( clipState.isPaused ? "Resume" : "Pause" ) ) { if ( clipState.isPaused ) { spriteAnimation.Resume( clipState.clip.name ); } else { spriteAnimation.Pause( clipState.clip.name ); } } //Display current playing clip's state. GUILayout.Label( "Name:" + clipState.clip.name ); GUILayout.Label( "Length:" + clipState.length.ToString() ); GUILayout.Label( "WrapMode:" + clipState.wrapMode.ToString() ); GUILayout.Label( "Time:" + clipState.time.ToString() ); GUILayout.Label( "Speed:" + clipState.speed.ToString() ); clipState.speed = GUILayout.HorizontalSlider( clipState.speed, -5f, 5f); GUILayout.Label( "Weight:" + clipState.weight.ToString() ); float weight = GUILayout.HorizontalSlider( clipState.weight, 0f, 1f); if ( GUI.changed ) { clipState.weight = weight; } GUILayout.Label( "Layer:" + clipState.layer.ToString() ); int layer = (int)GUILayout.HorizontalSlider( clipState.layer, -100f, 100f); if ( GUI.changed ) { clipState.layer = layer; } } GUILayout.EndArea(); }
public void Play(SpriteAnimation animation) { state = SpriteAnimationState.Playing; spriteAnimationHelper.ChangeAnimation(animation); }
void AddSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash ) { SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash(component._fullPathHash + parentHash); if (sprTransfrom == null) { { sprTransfrom = spriteRenderer.AttachSpriteComponent(component); sprTransfrom.parent = parentTransform; sprTransfrom.layer = (int)component.layer + state.parentComponentLayer; sprTransfrom.visible = component.visible; } sprTransfrom.overrideHash = parentHash != 0 ? component._fullPathHash + parentHash : 0; if (parentTransform != null) { if (parentTransform.firstChild == null) { parentTransform.firstChild = sprTransfrom; parentTransform.lastChild = sprTransfrom; sprTransfrom.prve = null; sprTransfrom.next = null; } else { sprTransfrom.next = parentTransform.lastChild.next; sprTransfrom.prve = parentTransform.lastChild; parentTransform.lastChild.next = sprTransfrom; } } } sprTransfrom.AttachState(state, component); for (int i = 0, e = component.children.Length; i < e; i++) AddSprite( state, state.clip.subComponents[ component.children[i] ], sprTransfrom, 0); }
internal void AddState(SpriteAnimationState state) { animationStates.Add(state.name, state); List<SpriteAnimationState> tmp = new List<SpriteAnimationState>(allStates); tmp.Add(state); allStates = tmp.ToArray(); }
internal void RemoveState(SpriteAnimationState state) { animationStates.Remove(state.name); List<SpriteAnimationState> tmp = new List<SpriteAnimationState>(allStates); tmp.Remove(state); allStates = tmp.ToArray(); }
internal void SetStateName( SpriteAnimationState state, string oldName ) { if ( oldName != null && animationStates.ContainsKey(oldName)) { SpriteAnimationState tmpState = animationStates[oldName] as SpriteAnimationState; animationStates.Remove(oldName); animationStates.Add(state.name, tmpState); } }
internal SpriteAnimationState AddClip(SpriteAnimationClip clip, string newName, int firstFrame, int lastFrame, bool addLoopFrame) { if (clip == null) { Debug.LogError("A null clip add to animation!"); return null; } SpriteAnimationState state = animationStates[newName] as SpriteAnimationState; if (state == null) { state = new SpriteAnimationState(clip, this, newName); AddState(state); } else { state.Init(clip, this, newName); } List<SpriteAnimationClip> tmpAnis = new List<SpriteAnimationClip>(animations); if (!tmpAnis.Contains(clip)) { tmpAnis.Add(clip); animations = tmpAnis.ToArray(); } return state; }
void Stop( SpriteAnimationState state) { state.time = 0; state.weight = 0f; state.enabled = false; if (state.removeAfterStop) removeList.Add(state); }
internal void Stop( SpriteAnimationState state) { state.time = 0; state.weight = 0f; state.enabled = false; if (state.removeAfterStop && !state.isRemoving) { removeList.Add(state); state.isRemoving = true; } }
void Blend(SpriteAnimationState state, float targetWeight, float fadeLength, bool needStop) { if (state.parentTransform == null) { state.needFade = true; state.fadeBase = state.weight; state.fadeTarget = targetWeight; state.fadeLength = fadeLength; state.currFadeTime = 0f; state.needStop = needStop; } }
internal void AddSprite(SpriteAnimationState state) { AddSprite(state, state.clip.root, state.parentTransform, state.parentTransform == null ? 0 : state.parentTransform.component._fullPathHash ); SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash( state.clip.root._fullPathHash); if ( state.parentTransform == null && !stateRoot.Contains(sprTransfrom)) stateRoot.Add(sprTransfrom); }
/// <summary> /// Cross fades an animation after previous animations has finished playing.<br/> ///If queue is QueueMode.CompleteOthers this animation will only start once all other animations have stopped playing.<br/> ///If queue is QueueMode.PlayNow this animation will start playing immediately on a duplicated animation state.<br/> /// if mode is PlayMode.StopSameLayer, animations in the same layer as animation will be faded out while animation is faded in. if mode is PlayMode.StopAll, all animations will be faded out while animation is faded in.<br/> ///After the animation has finished playing it will automatically clean itself up. Using the duplicated animation state after it has finished will result in an exception. <br/> /// </summary> /// <param name="name">The clip name that you want to fade in.</param> /// <param name="fadeLength">The fade in/out length in second.</param> /// <param name="queue">How the clip start fade.</param> /// <param name="mode">How to stop the other clips.</param> /// <returns>The duplicated animation state of the clip.</returns> public SpriteAnimationState CrossFadeQueued(string name, float fadeLength, QueueMode queue, PlayMode mode) { SpriteAnimationState state = this[name]; if (state == null) return null; bool isPlaying = IsLayerPlaying(state.layer); if (queue == QueueMode.PlayNow || !isPlaying) { string newName = state.name + " - Queued Clone " + cloneID++; SpriteAnimationState tmpState = new SpriteAnimationState(state, newName); tmpState.removeAfterStop = true; AddState(tmpState); CrossFade(tmpState, fadeLength, mode); return tmpState; } else { string newName = state.name + " - Queued Clone " + cloneID++; SpriteAnimationState tmpState = new SpriteAnimationState(state, newName); tmpState.removeAfterStop = true; AddState(tmpState); SpriteAnimationQueueItem item = new SpriteAnimationQueueItem(); item.state = tmpState; item.playMode = mode; item.fadeLength = fadeLength; crossFadeQueue.Add(item); return tmpState; } return null; }
internal void RemoveSprite(SpriteAnimationState state) { RemoveSprite(state, state.clip.root, state.parentTransform, state.parentTransform == null ? 0 : state.parentTransform.component._fullPathHash ); }
void Rewind(SpriteAnimationState state) { if (state.speed < 0f) state.time = state.clip.length; else state.time = 0f; }
/// <summary> /// Plays an animation after previous animations has finished playing.<br/> /// If queue is QueueMode.CompleteOthers this animation will only start once all other animations have stopped playing.<br/> /// If queue is QueueMode.PlayNow this animation will start playing immediately on a duplicated animation state.<br/> /// if mode is PlayMode.StopSameLayer, animations in the same layer as animation will be faded out while animation is faded in. if mode is PlayMode.StopAll, all animations will be faded out while animation is faded in.<br/> /// After the animation has finished playing it will automatically clean itself up. Using the duplicated animation state after it has finished will result in an exception. /// </summary> /// <param name="name">The name of animation clip you want to play.</param> /// <param name="queue">QueueMode control how to play the clip in queue.</param> /// <param name="mode">QueueMode control how to play the clip in queue.</param> /// <returns>The duplicated animation state of the clip.</returns> public SpriteAnimationState PlayQueued(string name, QueueMode queue, PlayMode mode) { SpriteAnimationState state = this[name]; if (state == null) return null; bool isPlaying = IsLayerPlaying(state.layer); if (queue == QueueMode.PlayNow || !isPlaying ) { string newName = state.name + " - Queued Clone " + cloneID++; SpriteAnimationState tmpState = new SpriteAnimationState(state, newName); tmpState.removeAfterStop = true; AddState(tmpState); Play(newName); return tmpState; } else { string newName = state.name + " - Queued Clone " + cloneID++; SpriteAnimationState tmpState = new SpriteAnimationState(state, newName); tmpState.removeAfterStop = true; AddState(tmpState); playingQueue.Add( tmpState ); return tmpState; } return null; }
// Use this for initialization void Start() { //Get SpriteAnimation for GameObject spriteAnimation = GetComponent<SpriteAnimation>(); List<string> tmp = new List<string>(); //Get all clips assigned in SpriteAnimation SpriteAnimationClip[] clips = SpriteAnimationUtility.GetAnimationClips( spriteAnimation ); foreach( SpriteAnimationClip clip in clips) { tmp.Add( clip.name); } animatinoNames = tmp.ToArray(); if ( spriteAnimation.clip != null ) currState = spriteAnimation[ spriteAnimation.clip.name ]; }
void CrossFade(SpriteAnimationState state, float fadeLength, PlayMode mode) { if (mode == PlayMode.StopAll) { foreach (SpriteAnimationState iter in allStates) { if (iter.enabled) Blend(iter, 0f, fadeLength, true); } } else { foreach (SpriteAnimationState iter in allStates) { if (iter.enabled && iter.layer == state.layer) Blend(iter, 0f, fadeLength, true); } } if (!state.enabled) { state.time = 0; state.weight = 0f; state.enabled = true; state.lastTime = 0f; state.lastFrameIndex = -1; state.lastEvaluateTime = 0; } Blend(state, 1f, fadeLength, false); }
private void FireAnimationEvent(SpriteAnimationState state, int idx) { //计算当前时间与最后次更新的时间差 float t = state.time - state.lastTime; //计算出步进了几个tick int step = (int)(t / state._clip.tick); //保存起始的帧索引 int li = state.lastFrameIndex; //事件数组的长度 int e = state._clip.events.Length; //搜索时间内的事件 for (float st = state.lastTime; st < state.time; st += state._clip.tick) { //得到当前时间的索引 int currFrameIdx = (int)state.curve.Evaluate(st); //检查是否和上次不一致,如果是则进行一个线性事件搜索 if (li != currFrameIdx) { for (int ei = 0; ei < e; ei++) { if (state._clip.events[ei].frameIndex == currFrameIdx) { SpriteAnimationEvent evt = state._clip.events[ei]; evtList.Add(evt); break; } } } state.lastFrameIndex = li = currFrameIdx; } if (state.lastFrameIndex != idx) { for (int ei = 0; ei < e; ei++) { if (state._clip.events[ei].frameIndex == idx) { SpriteAnimationEvent evt = state._clip.events[ei]; evtList.Add(evt); break; } } } }
void Stop( SpriteAnimationState state) { state.time = 0; state.weight = 0f; state.enabled = false; }
void AddSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash ) { int searchHash = component._fullPathHash + parentHash; SpriteTransform sprTransform = spriteRenderer.GetSpriteTransformByFullPathHash(searchHash); if (sprTransform == null) { { sprTransform = spriteRenderer.AttachSpriteComponent(component); sprTransform.parent = parentTransform; sprTransform.layer = (int)component.layer + state.parentComponentLayer; sprTransform.visible = component.visible; sprTransform.searchHash = searchHash; //sprTransform.name = state.parentTransform != null ? state.parentTransform.component._fullPath + component._fullPath : component._fullPath; } if (parentTransform != null) { if (parentTransform.firstChild == null) { parentTransform.firstChild = sprTransform; parentTransform.lastChild = sprTransform; sprTransform.prve = null; sprTransform.next = null; } else { sprTransform.next = parentTransform.lastChild.next; sprTransform.prve = parentTransform.lastChild; parentTransform.lastChild.next = sprTransform; } } } sprTransform.AttachState(state, component); for (int i = 0, e = component.children.Length; i < e; i++) AddSprite(state, state.clip.subComponents[component.children[i]], sprTransform, parentHash); }
internal void RemoveSprite(SpriteAnimationState state) { RemoveSprite(state, state.clip.root, state.parentTransform, state.parentTransform == null ? 0 : state.parentTransform.searchHash); }
void RemoveSprite(SpriteAnimationState state, SpriteAnimationComponent component, SpriteTransform parentTransform, int parentHash) { int searchHash = component._fullPathHash + parentHash; SpriteTransform sprTransfrom = spriteRenderer.GetSpriteTransformByFullPathHash(searchHash); for (int i = 0, e = component.children.Length; i < e; i++) RemoveSprite(state, state.clip.subComponents[component.children[i]], sprTransfrom, parentHash); if (sprTransfrom != null) { //if (sprTransfrom.lastRefClip != null) //{ // string name = state.name + "_" + sprTransfrom.lastRefClip.name; // SpriteAnimationState _state = this[name]; // if (_state != null) // { // if (_state.parentState != null) // _state.parentState.subClipState.Remove(_state); // Stop(_state); // } // sprTransfrom.lastRefClip = null; //} sprTransfrom.DetachState(state, component); if (sprTransfrom.attachStateComponent.Count <= 0) { if (sprTransfrom.prve != null) sprTransfrom.prve.next = sprTransfrom.next; if (sprTransfrom.next != null) sprTransfrom.next.prve = sprTransfrom.prve; if (sprTransfrom.parent != null && sprTransfrom.parent.firstChild == sprTransfrom) sprTransfrom.parent.firstChild = sprTransfrom.next; for (SpriteTransform child = sprTransfrom.firstChild; child != null; child = child.next) child.parent = null; { sprTransfrom.parent = null; sprTransfrom.next = null; sprTransfrom.prve = null; sprTransfrom.firstChild = null; sprTransfrom.lastChild = null; } if (stateRoot.Contains(sprTransfrom)) stateRoot.Remove(sprTransfrom); spriteRenderer.DetachSprite(sprTransfrom.id); } } }
internal void SetStateName( SpriteAnimationState state, string oldName ) { //if ( oldName != null ) { if (animationStates.ContainsKey(oldName)) { animationStates.Remove(oldName); animationStates.Add(state.name, state); } } }
void StopAllExcept(SpriteAnimationState s) { foreach (SpriteAnimationState state in allStates) { if (state != s) Stop(state); } }
internal void RemoveState(SpriteAnimationState state) { if (state != null) { animationStates.Remove(state.name); allStates.Remove(state); SpriteAnimationState.ReleaseState(state); } }
public void Play(SpriteAnimation animation) { state = SpriteAnimationState.Playing; spriteAnimationHelper.ChangeAnimation(animation); }