Unity-Animator深入系列---API詳解


回到 Animator深入系列總目錄

測試Unity版本為5.2.1

  • 人形動畫的接口都有標注
  • 本列表不包含所有標注為過時的方法

1.Vector3 angularVelocity { get; }

[人形動畫]獲得Avatar相對於最后一幀的角速率

2.bool applyRootMotion { get; set; }

是否打開根運動

3.Avatar avatar { get; set; }

[人形動畫]人形動畫Avatar,但是avatar結構里的字段非常少,應該是用於自定義類型轉換。

官方說在運行時切換Avatar並不完全支持,需要保險起見 LINK

4.Vector3 bodyPosition { get; set; }

[人形動畫]獲得身體坐標,根據測試應該是平均值,比spine還低一些

5.Quaternion bodyRotation { get; set; }

[人形動畫]獲得身體旋轉

6.AnimatorCullingMode cullingMode { get; set; }

默認是CullUpdateTransforms,會剔除一部分內容,包括一些消息還會觸發

CullCompletely是不可見時完全禁用,AlwaysAnimate是完全不剔除

具體參考官方文檔 LINK

7.Vector3 deltaPosition { get; }

對於有根位移的動畫,相對於上一幀的位移本地空間坐標差。

具體參考這篇:LINK

8.Quaternion deltaRotation { get; }

對於有根位移的動畫,相對於上一幀的位移本地空間旋轉坐標差。

具體參考這篇:LINK

9.float feetPivotActive { get; set; }

資料很少,修改該值並沒有影響到pivotPosition。

Blends pivot point between body center of mass and feet pivot. At 0%, the blending point is body center of mass. At 100%, the blending point is feet pivot.

混合身體質量中心與腳質量中心的軸心點。0%表示混合軸心點在身體的質量中心;100%表示混合軸心點在腳的軸心點;

10.bool fireEvents { get; set; }

官方新增的不明接口,包括官方文檔也沒有描述。

11.float gravityWeight { get; }

使用Animator的時候經常會發現動畫沒有重力,而gravityWeight就是這個問題的關鍵

默認重力權重設置為1,即可應用剛體的重力。如果剛體不打開重力,則沒有重力

使用時在FBX動畫片段的曲線中設置即可

重力權重越大即重力越大,重力權重范圍對應時間軸上包含位移的范圍

12.bool hasRootMotion { get; }

剪輯本身是否有根運動

13.bool hasTransformHierarchy { get; }

[人形動畫]只對人形動畫有效,當勾選優化游戲對象之后。返回值為Flase,否則為True

優化游戲對象具體參閱文檔 LINK

大意就是把層級信息存到內置的數據結構里,不需要在運行時進行匹配了。從而提升性能

14.float humanScale { get; }

返回當前人形綁定Avatar的縮放,(如果綁定是generic,默認為1)

測了下似乎和FBX設置的縮放因子有關,縮放比例是根據Unity默認的Avatar

15.bool isHuman { get; }

是否是人形動畫

16.bool isInitialized { get; }

檢測動畫是否初始化成功

當沒有控制器時會提示Not initialized,此時調用isInitialized返回False,反之返回True

17.bool isMatchingTarget { get; }

[人形動畫]當前是否正在匹配目標

關於匹配具體請看這篇LINK

18.bool isOptimizable { get; }

始終返回為True,暫時沒搞明白。官方文檔鏈接 LINK

AnimatorUtility.OptimizeTransformHierarchy(gameObject, new string[0]); 

將其優化后,並且Animator也出現優化后的提示,依舊打印True。。。實在無解

19.int layerCount { get; }

動畫層數

20.bool layersAffectMassCenter { get; set; }

資料很少,暫時不清楚這個接口,官方的描述:

Additional layers affects the center of mass.

官方的鏈接: LINK

21.float leftFeetBottomHeight { get; } 和 float rightFeetBottomHeight { get; }

[人形動畫]獲得左腳腳底高度和右腳腳底高度

測試代碼:

View Code

左邊的圖是獲得腳部IK坐標,並賦予給綠色Cube和紅色Cube

右邊的圖是加上腳底距離高度,並設置坐標

22.bool linearVelocityBlending { get; set; }

沒有太多資料,嘗試設置為True,測試了一下變化不是很大

官方描述

When linearVelocityBlending is set to true, the root motion velocity and angular velocity will be blended linearly.

當linearVelocityBlending 設置為true,根運動速度和角速度將線性混合

23.bool logWarnings { get; set; }

新加入的接口,文檔沒有相關信息,應該是是否打印警告日志

24.int parameterCount { get; }

擁有的參數數量,用於參數遍歷

25.AnimatorControllerParameter[] parameters { get; }

所有的參數數組

26.Vector3 pivotPosition { get; }

[人形動畫]相對於兩腳之間的中間點,非人形動畫返回Vector3.zero

27.float pivotWeight { get; }

[人形動畫]該軸心點是avatar的左右腳之間最穩定的軸心點。值為0時,左腳是最穩定的軸心點;值為1時,右腳是最穩定的軸心點

28.float playbackTime { get; set; }

動畫回放時間,錄制與回放具體請看這篇LINK

29.AnimatorRecorderMode recorderMode { get; }

當前的錄制模式,Offline,Playback,Record

回放或者錄制,或者兩種狀態都不在。

錄制與回放具體請看這篇LINK

30.float recorderStartTime { get; set; }

錄制開始時間,錄制與回放具體請看這篇LINK

31.float recorderStopTime { get; set; }

錄制結束時間,錄制與回放具體請看這篇LINK

32.Vector3 rootPosition { get; set; }

返回根骨骼的世界坐標

33.Quaternion rootRotation { get; set; }

返回根骨骼的世界旋轉

34.RuntimeAnimatorController runtimeAnimatorController { get; set; }

可以進行強類型轉換,也可以直接取到原始動畫剪輯,映像中還有一個Editor狀態下可以轉換的Controller,能改一些底層信息

35.float speed { get; set; }

動畫的整體速度,影響其中的每一個動畫剪輯

36.bool stabilizeFeet { get; set; }

過渡和混合時,腳自動穩定。

37.Vector3 targetPosition { get; }

返回由SetTarget(AvatarTarget targetIndex, float targetNormalizedTime))指定的目標的位置

關於SetTarget具體請看這篇LINK

38.Quaternion targetRotation { get; }

返回由SetTarget(AvatarTarget targetIndex, float targetNormalizedTime))指定的目標的旋轉角度。

關於SetTarget具體請看這篇LINK

39.AnimatorUpdateMode updateMode { get; set; }

Normal Normal update of the animator.

標准更新。

AnimatePhysics Updates the animator during the physic loop in order to have the animation system synchronized with the physics engine.

在物理循環期間更新Animator為了使動畫系統與物理引擎同步。

UnscaledTime Animator updates independently of Time.timeScale.

Time.timeScale外的Animator獨立更新。

默認值是Normal

40.Vector3 velocity { get; }

返回根骨骼相對上一幀結果計算的速率,對於沒有根骨骼的動畫無效

41.static int StringToHash(string name)

注意這個是靜態方法,通過字符串取得參數的hash值

42.void ApplyBuiltinRootMotion()

該方法顧名思義,會調用內建的根骨骼移動

也可以用在StateMachineBehaviour的OnStateMove里

比如下面這樣,運行游戲后角色直接飛掉了

public class Test1 : MonoBehaviour { public Animator animator; void OnAnimatorMove() { for (int i = 0; i < 100; i++) { animator.ApplyBuiltinRootMotion(); } } } 

43.CrossFade(...)系列接口

和Animation的CrossFade差不多,混合過渡並且播放

第四個參數normalizedTime要說一下,是0-1的范圍,表示目標動畫剪輯從哪里開始播放

animator.CrossFade("Jump", 0.2f, 0, 0.5f); 

可以看見,調用后從0.5開始播放

44.AnimatorTransitionInfo GetAnimatorTransitionInfo(int layerIndex)

獲得當前動畫的過渡信息,關於過渡可以看這篇LINK

45.GetBehaviour<T>() 和 GetBehaviours<T>()

返回掛載在AnimatorController上的行為腳本StateMachineBehaviour

通常StateMachineBehaviour之間的交互會經常用到。

46.Transform GetBoneTransform(HumanBodyBones humanBoneId)

[人形動畫]獲取骨骼的Transform對象,參數是枚舉形。

如果被臨時刪除了或者沒有綁定這個關節,會返回空

Debug.Log("GetBoneTransform: " + animator.GetBoneTransform(HumanBodyBones.LeftFoot));
//print GetBoneTransform: joint_FootLT (UnityEngine.Transform) 

47.public AnimatorClipInfo[] GetCurrentAnimatorClipInfo(int layerIndex);

獲得當前層 正在播放 的剪輯數組,並且獲取到的時間長度(clip.length)不受Animator速度影響。

如果想獲取全部原始剪輯,可以用runtimeAnimatorController.animationClip

48.public AnimatorStateInfo GetCurrentAnimatorStateInfo(int layerIndex);

獲得當前所處的動畫狀態

注意取到的length屬性會受到Animator速度縮放影響

(如果要獲取不受縮放影響的原始剪輯,可以用runtimeAnimatorController.animationClip或者GetCurrentAnimatorClipInfo,區別在於一個能取到全部,一個只能取到當前播放的)

49.public bool HasState(int layerIndex, int stateID);

配合Animator.StringToHash可以直接轉換成stateID

Debug.Log(animator.HasState(0, Animator.StringToHash("Idle"))); //print True 

50.public bool IsInTransition(int layerIndex);

是否正處於過渡,很常用的一個接口

如果忽視了過渡的判斷,就會有意想不到的bug

if(!animator.IsInTransition(0) && animator.GetCurrentAnimatorStateInfo(0).IsName("Idle")) { //Do something. } 

51.public bool IsParameterControlledByCurve(...);

參數是否被曲線控制

Debug.Log("IsParameterControlledByCurve: " + animator.IsParameterControlledByCurve("Speed")); //print True 

注意,參數一旦被曲線控制,就不要在代碼里修改它,否則會有警告

52.MatchTarget(Vector3 matchPosition, Quaternion matchRotation, AvatarTarget targetBodyPart, MatchTargetWeightMask weightMask, float startNormalizedTime, float targetNormalizedTime)

[人形動畫]匹配目標,關於匹配具體請看這篇LINK

53.Play(...)系列接口

沒什么好說的,需要注意normalizedTime參數,區間在0-1之間,直接影響到目標動畫剪輯從哪個時間點開始播放

54.void Rebind();

重新綁定動畫器的所有動畫的屬性和網格數據。

有時根骨骼坐標會被鎖定,重置坐標后,調用該方法即可解決。

55.public void ResetTrigger(...);

重設該觸發參數為False

如果是當前狀態播放結束再跳轉的話,會用到這個方法,在結束之前可以重置回False

56.void SetBoneLocalRotation(...);

[人形動畫]設置某個骨骼的本地旋轉

57.void Vector3 GetIKHintPosition(AvatarIKHint hint)  &   SetIKHintPosition(AvatarIKHint hint, Vector3 hintPosition)

[人形動畫]獲取/設置IK Hint位置,IK相關操作請參考這篇:LINK

58.float GetIKHintPositionWeight(AvatarIKHint hint)   &   void SetIKHintPositionWeight(AvatarIKHint hint, float value)

[人形動畫]獲取/設置IK Hint位置權重,IK相關操作請參考這篇:LINK

59.Vector3 GetIKPosition(AvatarIKGoal goal)   &    void SetIKPosition(AvatarIKGoal goal, Vector3 goalPosition)

[人形動畫]獲取/設置IK位置,IK相關操作請參考這篇:LINK

60.float GetIKPositionWeight(AvatarIKGoal goal)     &    void SetIKPositionWeight(AvatarIKGoal goal, float value)

[人形動畫]獲取/設置和獲取IK位置權重,IK相關操作請參考這篇:LINK

61.Quaternion GetIKRotation(AvatarIKGoal goal)    &   void SetIKRotation(AvatarIKGoal goal, Quaternion goalRotation)

[人形動畫]獲取/設置IK旋轉,IK相關操作請參考這篇:LINK

62.float GetIKRotationWeight(AvatarIKGoal goal)  &  void SetIKRotationWeight(AvatarIKGoal goal, float value)

[人形動畫]獲取/設置IK旋轉權重,IK相關操作請參考這篇:LINK

63.void SetLayerWeight(int layerIndex, float weight)

設置層權重

64.void SetLookAtPosition(Vector3 lookAtPosition) 和 void SetLookAtWeight(...)

[人形動畫]測試腳本:

animator.SetLookAtPosition(reference.position);
animator.SetLookAtWeight(1f);

使用后玩家會看向reference的對象

IK相關操作請參考這篇:LINK

65.void SetTarget(AvatarTarget targetIndex, float targetNormalizedTime)

[人形動畫]在動畫播放前得到播放后的坐標和旋轉。

關於SetTarget具體請看這篇LINK

66.void StartPlayback()

開始回放,錄制與回放具體請看這篇LINK

67.void StartRecording(int frameCount)

開始錄制

注意參數中的frameCount是需要錄制長度的幀數

如果參數小於1,則不會限制錄制時間

錄制與回放具體請看這篇LINK

68.void StopPlayback()

停止回放,錄制與回放具體請看這篇LINK

69.void StopRecording()

停止錄制,錄制與回放具體請看這篇LINK

70.void Update(float deltaTime)

更新動畫,一般用於手動更新修改過的參數,或者在編輯器中運行Animator

71.GetSet各類型參數系列(SetFloat,SetBool等等)

最常用的接口

但需要注意下float類型數據,可以單獨設置阻尼,具體看這篇LINK


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM