1. 程式人生 > >Unity 精簡緩動動畫指令碼

Unity 精簡緩動動畫指令碼

實現了一個Dotween超精簡版的指令碼,核心均基於Unity MonoNehaviour.StartCoroutine方法實現,添加了Unity.Transform新增擴充套件方法,方便呼叫。包含動畫過程中常用的基本的位移、縮放、旋轉、淡入淡出、以及數值的動態變化。使用也非常簡單。直接將Plugins下的Prefabs拖動到場景中,然後再指令碼中呼叫即可。

Demo截圖預覽

部分程式碼如下


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace
SunME { public static class Extensionmethod { /// <summary> /// 移動物件到目標位置 /// </summary> /// <param name="targetPos">目標座標</param> /// <param name="duration">過程的持續時間</param> /// <param name="ignoreTimeScale">是否忽視時間縮放</param> /// <param name="OnComplete">執行完成的回撥函式</param>
public static void DoMove (this Transform t, Vector3 targetPos, float duration = 1, bool ignoreTimeScale = false, Action OnComplete = null) { Tween.Instance.MoveAtion (t, targetPos, duration, ignoreTimeScale, OnComplete); } /// <summary> /// 旋轉物件到目標尤拉角 /// </summary> /// <param name="targetRotate">目標尤拉角</param>
/// <param name="duration">過程的持續時間</param> /// <param name="ignoreTimeScale">是否忽視時間縮放</param> /// <param name="OnComplete">執行完成的回撥函式</param> public static void DoRotate (this Transform t, Vector3 targetRotate, float duration = 1, bool ignoreTimeScale = false, Action OnComplete = null) { Tween.Instance.RoateAction (t, targetRotate, duration, ignoreTimeScale, OnComplete); } /// <summary> /// 縮放物件到目標值 /// </summary> /// <param name="targetScale">目標縮放值</param> /// <param name="duration">過程的持續時間</param> /// <param name="ignoreTimeScale"></param> /// <param name="OnComplete">執行完成的回撥函式</param> public static void DoScale (this Transform t, Vector3 targetScale, float duration = 1, bool ignoreTimeScale = false, Action OnComplete = null) { Tween.Instance.ScaleAction (t, targetScale, duration, ignoreTimeScale, OnComplete); } /// <summary> /// 改變物件的Alpha為目標值 /// </summary> /// <param name="isfadeIn">是否是淡入,否則為淡出</param> /// <param name="duration">過程的持續時間</param> /// <param name="ignoreTimeScale">是否忽視時間縮放</param> /// <param name="OnComplete">執行完成的回撥函式</param> public static void DoFade (this Transform t, bool isfadeIn, float duration = 1, bool ignoreTimeScale = false, Action OnComplete = null) { Tween.Instance.FadeAction (t, isfadeIn, duration, ignoreTimeScale, OnComplete); } /// <summary> /// 在給定時間內改變一個浮點值 /// </summary> /// <param name="targetValue">目標值</param> /// <param name="valueChangeAction">值發生改變時的回撥函式</param> /// <param name="duration">持續時間</param> /// <param name="ignoreTimeScale">是否忽視時間縮放</param> /// <param name="OnComplete">值更改完成時回撥</param> public static void DoValueChange (this float t, float targetValue, Action<float> valueChangeAction, float duration = 1, bool ignoreTimeScale = false, Action OnComplete = null) { Tween.Instance.ValueChangeAction (t, targetValue, valueChangeAction, duration, ignoreTimeScale, OnComplete); } } }

呼叫方式如下

t1.DoMove (new Vector3 (3, -2, 0), 2, true, ()=>{ Debug.Log ("移動完成"); { Debug.Log ("移動完成"); });
		t2.DoRotate (new Vector3 (0, 60, 0), 2, false, () => { Debug.Log ("旋轉完成"); });
		t3.DoScale (new Vector3 (3, 3, 3), 3);
		spriteRenderer.transform.DoFade (false, 3);
		image.transform.DoFade (false, 2);
		kk.DoValueChange (80, value => { _camera.fieldOfView = value; }, 2, false, () => {
				Debug.Log ("值改變完成");
			});

Demo和工程地質地址:掃碼—>當前文章—>文章末尾