public void PlayAnim( string animName ) { if ( animations.Length == 0 ) return; foreach ( SpriteAnimation anim in animations ) { if ( anim.name == animName ) { currentAnimation = anim; headState = PlayHeadState.PLAY; int frameCount = anim.endFrame - anim.startFrame + 1; switch ( anim.playMode ) { case PlayMode.DEFAULT: frameSequence = new int[ frameCount ]; for ( int i = 0; i < frameSequence.Length; i++ ) frameSequence[ i ] = anim.startFrame + i; break; case PlayMode.LOOP: frameSequence = new int[(frameCount) + 1]; for ( int i = 0; i < frameSequence.Length - 1; i++ ) { frameSequence[ i ] = anim.startFrame + i; } frameSequence[ frameSequence.Length - 1 ] = -1; // loop order break; case PlayMode.PINGPONG: frameSequence = new int[((frameCount) * 2) - 2 + 1]; for ( int i = 0; i < frameSequence.Length - 1; i++ ) frameSequence[ i ] = (int)Mathf.PingPong( anim.startFrame + i, (frameCount) - 1 ); frameSequence[ frameSequence.Length - 1 ] = -1; // loop order break; case PlayMode.RANDOM: frameSequence = new int[frameCount]; for ( int i = 0; i < frameSequence.Length; i++ ) frameSequence[ i ] = anim.startFrame + i; for ( int i = 0; i < frameSequence.Length - 1; i++ ) { int swap = frameSequence[ i ]; int randIndex = Random.Range( i + 1, frameSequence.Length - 1 ); frameSequence[ i ] = frameSequence[ randIndex ]; frameSequence[ randIndex ] = swap; } break; case PlayMode.CUSTOM: frameSequence = new int[ anim.customFrameSequence.Length ]; for ( int i = 0; i < frameSequence.Length; i++ ) frameSequence[ i ] = anim.customFrameSequence[ i ]; break; } return; } } }
void Update() { if ( Application.isEditor && !Application.isPlaying ) { EditorUpdate(); return; } if ( isBillboard ) { if ( billboardCamera ) { transform.rotation = Quaternion.LookRotation( transform.position - billboardCamera.transform.position ); transform.rotation = Quaternion.Euler ( transform.rotation.eulerAngles.x, 0, 0 ); } else FindBillboardCamera(); } //print ("animatinos.Lenght = " + animations.Length ); if ( currentAnimation == null || animations.Length == 0 ) return; float xScale = 1.0f / xcells; float yScale = 1.0f / ycells; // Prevent out of bounds. //frameIndex = Mathf.Min ( frameList.Length - 1, frameIndex ); if ( headState == PlayHeadState.PLAY ) timer += Time.deltaTime; float frameTime = 1f / currentAnimation.frameRate; timer = Mathf.Clamp( timer, 0, frameTime * (frameSequence.Length) ); if ( timer >= (frameTime * (frameSequence.Length)) - 0.01f ) { if ( nextAnim != "" ) { StopAnim(); PlayAnim ( nextAnim ); nextAnim = ""; } else { headState = PlayHeadState.STOP; } } frameIndex = (int)(timer / frameTime); frameIndex = Mathf.Clamp ( frameIndex, 0, (frameSequence.Length - 1) ); //print ( " frame = " + frameIndex + " ... " + currentAnimation.frameRate ); if ( frameSequence[ frameIndex ] == -1 ) // Loop! { frameIndex = 0; timer = 0; } for ( int i = 0; i < renderer.materials.Length; i++ ) { Material m = renderer.materials[i]; m.mainTextureScale = new Vector2( xScale * ( currentAnimation.horizontalFlip ? -1 : 1 ), yScale ); m.mainTextureOffset = new Vector2( xScale * ((frameSequence[frameIndex] % xcells) - ( currentAnimation.horizontalFlip ? -1 : 0 )), (1.0f - yScale) - (yScale * (frameSequence[frameIndex] / xcells)) ); } }
public void PauseAnim() { headState = PlayHeadState.STOP; }
public void StopAnim() { headState = PlayHeadState.STOP; timer = 0; frameIndex = 0; }