示例#1
0
    /// <summary>
    /// Sets the current frame of the current animation immediately.
    /// </summary>
    /// <param name="index">Zero-based index of the desired frame.</param>
    public void SetCurFrame(int index)
    {
        if (curAnim == null)
        {
            return;
        }

        curAnim.SetCurrentFrame(index - curAnim.StepDirection);
        timeSinceLastFrame = timeBetweenAnimFrames;
        StepAnim(0);
    }
示例#2
0
    /// <summary>
    /// Starts playing the specified animation
    /// Note: this doesn't resume from a pause,
    /// it completely restarts the animation. To
    /// unpause, use <see cref="UnpauseAnim"/>.
    /// </summary>
    /// <param name="anim">A reference to the animation to play.</param>
    /// <param name="frame">The zero-based index of the frame at which to start playing.</param>
    public void PlayAnim(UVAnimation anim, int frame)
    {
        if (deleted || !gameObject.active)
        {
            return;
        }

        curAnim      = anim;
        curAnimIndex = curAnim.index;
        curAnim.Reset();
        curAnim.SetCurrentFrame(frame - 1);             // curFrame inside UVAnimation will be incremented before it is used, so anticipate this by decrementing by 1

        // Ensure the framerate is not 0 so we don't
        // divide by zero:
        if (anim.framerate != 0.0f)
        {
            timeBetweenAnimFrames = 1f / anim.framerate;
        }
        else
        {
            timeBetweenAnimFrames = 1f;             // Just some dummy value since it won't be used
        }

        timeSinceLastFrame = timeBetweenAnimFrames;

        // Only add to the animated list if
        // the animation has more than 1 frame
        // or the framerate is non-zero:
        if ((anim.GetFrameCount() > 1 || anim.onAnimEnd != UVAnimation.ANIM_END_ACTION.Do_Nothing) && anim.framerate != 0.0f)
        {
            StepAnim(0);
            // Start coroutine
            if (!animating)
            {
                //animating = true;
                AddToAnimatedList();
                //StartCoroutine(AnimationPump());
            }
        }
        else
        {
            // Since this is a single-frame anim,
            // call our delegate before setting
            // the frame so that our behavior is
            // consistent with multi-frame anims:
            if (animCompleteDelegate != null)
            {
                animCompleteDelegate(this);
            }

            StepAnim(0);
        }
    }
示例#3
0
    /// <summary>
    /// Like PlayAnim, but plays the animation in reverse.
    /// See <see cref="PlayAnim"/>.
    /// </summary>
    /// <param name="anim">Reference to the animation to play in reverse.</param>
    /// <param name="frame">The zero-based index of the frame at which to start playing.</param>
    public void PlayAnimInReverse(UVAnimation anim, int frame)
    {
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
        if (deleted || !gameObject.activeInHierarchy)
#else
        if (deleted || !gameObject.active)
#endif
        { return; }
        if (!m_started)
        {
            Start();
        }

        curAnim = anim;
        curAnim.Reset();
        curAnim.PlayInReverse();
        curAnim.SetCurrentFrame(frame + 1);             // curFrame inside UVAnimation will be decremented before it is used, so anticipate this by decrementing by 1

        // Ensure the framerate is not 0 so we don't
        // divide by zero:
        anim.framerate = Mathf.Max(0.0001f, anim.framerate);

        timeBetweenAnimFrames = 1f / anim.framerate;
        timeSinceLastFrame    = timeBetweenAnimFrames;

        // Only add to the animated list if
        // the animation has more than 1 frame:
        if (anim.GetFrameCount() > 1)
        {
            StepAnim(0);
            // Start coroutine
            if (!animating)
            {
                //animating = true;
                AddToAnimatedList();
                //StartCoroutine(AnimationPump());
            }
        }
        else
        {
            // Since this is a single-frame anim,
            // call our delegate before setting
            // the frame so that our behavior is
            // consistent with multi-frame anims:
            if (animCompleteDelegate != null)
            {
                animCompleteDelegate(this);
            }

            StepAnim(0);
        }
    }
	/// <summary>
	/// Like PlayAnim, but plays the animation in reverse.
	/// See <see cref="PlayAnim"/>.
	/// </summary>
	/// <param name="anim">Reference to the animation to play in reverse.</param>
	/// <param name="frame">The zero-based index of the frame at which to start playing.</param>
	public void PlayAnimInReverse(UVAnimation anim, int frame)
	{
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
		if (deleted || !gameObject.activeInHierarchy)
#else
		if (deleted || !gameObject.active)
#endif
			return;
		if (!m_started)
			Start();

		curAnim = anim;
		curAnim.Reset();
		curAnim.PlayInReverse();
		curAnim.SetCurrentFrame(frame + 1);	// curFrame inside UVAnimation will be decremented before it is used, so anticipate this by decrementing by 1

		// Ensure the framerate is not 0 so we don't
		// divide by zero:
		anim.framerate = Mathf.Max(0.0001f, anim.framerate);

		timeBetweenAnimFrames = 1f / anim.framerate;
		timeSinceLastFrame = timeBetweenAnimFrames;

		// Only add to the animated list if
		// the animation has more than 1 frame:
		if (anim.GetFrameCount() > 1)
		{
			StepAnim(0);
			// Start coroutine
			if (!animating)
			{
				//animating = true;
				AddToAnimatedList();
				//StartCoroutine(AnimationPump());
			}
		}
		else
		{
			// Since this is a single-frame anim,
			// call our delegate before setting
			// the frame so that our behavior is
			// consistent with multi-frame anims:
			if (animCompleteDelegate != null)
				animCompleteDelegate(this);

			StepAnim(0);
		}
	}
	/// <summary>
	/// Starts playing the specified animation
	/// Note: this doesn't resume from a pause,
	/// it completely restarts the animation. To
	/// unpause, use <see cref="UnpauseAnim"/>.
	/// </summary>
	/// <param name="anim">A reference to the animation to play.</param>
	/// <param name="frame">The zero-based index of the frame at which to start playing.</param>
	public void PlayAnim(UVAnimation anim, int frame)
	{
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
		if (deleted || !gameObject.activeInHierarchy)
#else
		if (deleted || !gameObject.active)
#endif
			return;

		if (!m_started)
			Start();

		curAnim = anim;
		curAnimIndex = curAnim.index;
		curAnim.Reset();
		curAnim.SetCurrentFrame(frame - 1);	// curFrame inside UVAnimation will be incremented before it is used, so anticipate this by decrementing by 1

		// Ensure the framerate is not 0 so we don't
		// divide by zero:
		if (anim.framerate != 0.0f)
		{
			timeBetweenAnimFrames = 1f / anim.framerate;
		}
		else
		{
			timeBetweenAnimFrames = 1f; // Just some dummy value since it won't be used
		}

		timeSinceLastFrame = timeBetweenAnimFrames;

		// Only add to the animated list if
		// the animation has more than 1 frame
		// or the framerate is non-zero:
		if ((anim.GetFrameCount() > 1 || anim.onAnimEnd != UVAnimation.ANIM_END_ACTION.Do_Nothing) && anim.framerate != 0.0f)
		{
			StepAnim(0);
			// Start coroutine
			if (!animating)
			{
				//animating = true;
				AddToAnimatedList();
				//StartCoroutine(AnimationPump());
			}
		}
		else
		{
			// Make sure we are no longer in the animation pump:
			PauseAnim();

			// Since this is a single-frame anim,
			// call our delegate before setting
			// the frame so that our behavior is
			// consistent with multi-frame anims:
			if (animCompleteDelegate != null)
			{
				// See if we need to delay calling
				// our delegate to be consistent
				// with our framerate:
				if (anim.framerate != 0)
				{
					Invoke("CallAnimCompleteDelegate", 1f / anim.framerate);
				}
				else
					animCompleteDelegate(this);
			}

			StepAnim(0);
		}
	}
示例#6
0
    /// <summary>
    /// Starts playing the specified animation
    /// Note: this doesn't resume from a pause,
    /// it completely restarts the animation. To
    /// unpause, use <see cref="UnpauseAnim"/>.
    /// </summary>
    /// <param name="anim">A reference to the animation to play.</param>
    /// <param name="frame">The zero-based index of the frame at which to start playing.</param>
    public void PlayAnim(UVAnimation anim, int frame)
    {
        if (deleted || !gameObject.active)
            return;

        curAnim = anim;
        curAnimIndex = curAnim.index;
        curAnim.Reset();
        curAnim.SetCurrentFrame(frame - 1);	// curFrame inside UVAnimation will be incremented before it is used, so anticipate this by decrementing by 1

        // Ensure the framerate is not 0 so we don't
        // divide by zero:
        if(anim.framerate != 0.0f)
        {
            timeBetweenAnimFrames = 1f / anim.framerate;
        }
        else
        {
            timeBetweenAnimFrames = 1f; // Just some dummy value since it won't be used
        }

        timeSinceLastFrame = timeBetweenAnimFrames;

        // Only add to the animated list if
        // the animation has more than 1 frame
        // or the framerate is non-zero:
        if ( (anim.GetFrameCount() > 1 || anim.onAnimEnd != UVAnimation.ANIM_END_ACTION.Do_Nothing) && anim.framerate != 0.0f)
        {
            StepAnim(0);
            // Start coroutine
            if (!animating)
            {
                //animating = true;
                AddToAnimatedList();
                //StartCoroutine(AnimationPump());
            }
        }
        else
        {
            // Since this is a single-frame anim,
            // call our delegate before setting
            // the frame so that our behavior is
            // consistent with multi-frame anims:
            if (animCompleteDelegate != null)
                animCompleteDelegate(this);

            StepAnim(0);
        }
    }
示例#7
0
    /*
     *      // Steps to the next frame of sprite animation
     *      public bool StepAnim(float time)
     *      {
     *              if (curAnim == null)
     *                      return false;
     *
     *              timeSinceLastFrame += time;
     *
     *              framesToAdvance = (int)(timeSinceLastFrame / timeBetweenAnimFrames);
     *
     *              // If there's nothing to do, return:
     *              if (framesToAdvance < 1)
     *                      return true;
     *
     *              while (framesToAdvance > 0)
     *              {
     *                      if (curAnim.GetNextFrame(ref lowerLeftUV))
     *                              --framesToAdvance;
     *                      else
     *                      {
     *                              // We reached the end of our animation
     *                              if (animCompleteDelegate != null)
     *                                      animCompleteDelegate();
     *
     *                              // Update mesh UVs:
     *                              UpdateUVs();
     *                              PauseAnim();
     *                              animating = false;
     *
     *                              return false;
     *                      }
     *              }
     *
     *              // Update mesh UVs:
     *              UpdateUVs();
     *
     *              timeSinceLastFrame = 0;
     *
     *              return true;
     *      }
     */

    // Steps to the next frame of sprite animation
    public override bool StepAnim(float time)
    {
        if (curAnim == null)
        {
            return(false);
        }

        timeSinceLastFrame += Mathf.Max(0, time);
        //timeSinceLastFrame += time;

        framesToAdvance = (timeSinceLastFrame / timeBetweenAnimFrames);

        // If there's nothing to do, return:
        if (framesToAdvance < 1)
        {
            if (crossfadeFrames)
            {
                SetColor(new Color(1f, 1f, 1f, (1f - framesToAdvance)));
            }
            return(true);
        }

        //timeSinceLastFrame -= timeBetweenAnimFrames * (float)framesToAdvance;

        while (framesToAdvance >= 1f)
        {
            if (curAnim.GetNextFrame(ref frameInfo))
            {
#if SPRITE_FRAME_DELEGATE
                // Notify the delegate:
                if (framesToAdvance >= 1f)
                {
                    if (animFrameDelegate != null)
                    {
                        animFrameDelegate(this, curAnim.GetCurPosition());
                    }
                }
#endif
                framesToAdvance    -= 1f;
                timeSinceLastFrame -= timeBetweenAnimFrames;
            }
            else
            {
                // We reached the end of our animation
                if (crossfadeFrames)
                {
                    SetColor(Color.white);
                }

                // See if we should revert to a static appearance,
                // default anim, or do nothing, etc:
                switch (curAnim.onAnimEnd)
                {
                case UVAnimation.ANIM_END_ACTION.Do_Nothing:
                    PauseAnim();

                    // Update mesh UVs:
                    uvRect = frameInfo.uvs;
                    SetBleedCompensation();

                    // Resize if selected:
                    if (autoResize || pixelPerfect)
                    {
                        CalcSize();
                    }
                    break;

                case UVAnimation.ANIM_END_ACTION.Revert_To_Static:
                    RevertToStatic();
                    curAnim = null;
                    break;

                case UVAnimation.ANIM_END_ACTION.Play_Default_Anim:
                    // Notify the delegates:

                    /*
                     #if SPRITE_FRAME_DELEGATE
                     *                                              if (animFrameDelegate != null)
                     *                                                      animFrameDelegate(this, curAnim.GetCurPosition());
                     #endif
                     */
                    if (animCompleteDelegate != null)
                    {
                        animCompleteDelegate(this);
                    }

                    // Play the default animation:
                    PlayAnim(defaultAnim);
                    return(false);

                case UVAnimation.ANIM_END_ACTION.Hide:
                    Hide(true);
                    break;

                case UVAnimation.ANIM_END_ACTION.Deactivate:
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
                    gameObject.SetActive(false);
#else
                    gameObject.active = false;
#endif
                    break;

                case UVAnimation.ANIM_END_ACTION.Destroy:
                    // Notify the delegates:

                    /*
                     #if SPRITE_FRAME_DELEGATE
                     *                                              if (animFrameDelegate != null)
                     *                                                      animFrameDelegate(this, curAnim.GetCurPosition());
                     #endif
                     */
                    if (animCompleteDelegate != null)
                    {
                        animCompleteDelegate(this);
                    }

                    Delete();
                    Destroy(gameObject);
                    break;
                }

                // Notify the delegates:

                /*
                 #if SM2_FRAME_DELEGATE
                 *                      if (animFrameDelegate != null)
                 *                              animFrameDelegate(this, curAnim.GetCurPosition());
                 #endif
                 */
                if (animCompleteDelegate != null)
                {
                    animCompleteDelegate(this);
                }

                // Check to see if we are still animating
                // before setting the curAnim to null.
                // Animating should be turned off if
                // PauseAnim() was called above, or if we
                // reverted to static.  But it could have
                // been turned on again by the
                // animCompleteDelegate.
                if (!animating)
                {
                    curAnim = null;
                }

                return(false);
            }
        }

        // Cross-fade to the next frame:
        if (crossfadeFrames)
        {
            UVAnimation curClip      = curAnim.GetCurrentClip();
            int         curClipNum   = curAnim.GetCurClipNum();
            int         curFrame     = curClip.GetCurPosition();
            int         multiStepDir = curAnim.StepDirection;
            int         clipStepDir  = curClip.StepDirection;

            curAnim.GetNextFrame(ref nextFrameInfo);

            Vector2[] uvs2   = m_spriteMesh.uvs2;
            Rect      nextUV = nextFrameInfo.uvs;
            uvs2[0].x = nextUV.xMin; uvs2[0].y = nextUV.yMax;
            uvs2[1].x = nextUV.xMin; uvs2[1].y = nextUV.yMin;
            uvs2[2].x = nextUV.xMax; uvs2[2].y = nextUV.yMin;
            uvs2[3].x = nextUV.xMax; uvs2[3].y = nextUV.yMax;

            // Undo our advance:
            curAnim.SetCurClipNum(curClipNum);
            curClip.SetCurrentFrame(curFrame);
            curAnim.StepDirection = multiStepDir;
            curClip.StepDirection = clipStepDir;

            SetColor(new Color(1f, 1f, 1f, (1f - framesToAdvance)));
        }

        /*
         #if SM2_FRAME_DELEGATE
         *              if (animFrameDelegate != null)
         *                      animFrameDelegate(this, curAnim.GetCurPosition());
         #endif
         */
        // Update mesh UVs:
        uvRect = frameInfo.uvs;
        SetBleedCompensation();

        // Resize if selected:
        if (autoResize || pixelPerfect)
        {
            CalcSize();
        }

        //timeSinceLastFrame = 0;

        return(true);
    }
示例#8
0
    /// <summary>
    /// Starts playing the specified animation
    /// Note: this doesn't resume from a pause,
    /// it completely restarts the animation. To
    /// unpause, use <see cref="UnpauseAnim"/>.
    /// </summary>
    /// <param name="anim">A reference to the animation to play.</param>
    /// <param name="frame">The zero-based index of the frame at which to start playing.</param>
    public void PlayAnim(UVAnimation anim, int frame)
    {
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
        if (deleted || !gameObject.activeInHierarchy)
#else
        if (deleted || !gameObject.active)
#endif
        { return; }

        if (!m_started)
        {
            Start();
        }

        curAnim      = anim;
        curAnimIndex = curAnim.index;
        curAnim.Reset();
        curAnim.SetCurrentFrame(frame - 1);             // curFrame inside UVAnimation will be incremented before it is used, so anticipate this by decrementing by 1

        // Ensure the framerate is not 0 so we don't
        // divide by zero:
        if (anim.framerate != 0.0f)
        {
            timeBetweenAnimFrames = 1f / anim.framerate;
        }
        else
        {
            timeBetweenAnimFrames = 1f;             // Just some dummy value since it won't be used
        }

        timeSinceLastFrame = timeBetweenAnimFrames;

        // Only add to the animated list if
        // the animation has more than 1 frame
        // or the framerate is non-zero:
        if ((anim.GetFrameCount() > 1 || anim.onAnimEnd != UVAnimation.ANIM_END_ACTION.Do_Nothing) && anim.framerate != 0.0f)
        {
            StepAnim(0);
            // Start coroutine
            if (!animating)
            {
                //animating = true;
                AddToAnimatedList();
                //StartCoroutine(AnimationPump());
            }
        }
        else
        {
            // Make sure we are no longer in the animation pump:
            PauseAnim();

            // Since this is a single-frame anim,
            // call our delegate before setting
            // the frame so that our behavior is
            // consistent with multi-frame anims:
            if (animCompleteDelegate != null)
            {
                // See if we need to delay calling
                // our delegate to be consistent
                // with our framerate:
                if (anim.framerate != 0)
                {
                    Invoke("CallAnimCompleteDelegate", 1f / anim.framerate);
                }
                else
                {
                    animCompleteDelegate(this);
                }
            }

            StepAnim(0);
        }
    }