您的位置:首页 > 移动开发 > Unity3D

Unity Avatar动画系统

2014-05-13 17:01 344 查看
Unity Avatar动画系统使用的一些基础属性和方法。以方便日后查找和使用。

using UnityEngine;
using System.Collections;

public class AnimationTest : MonoBehaviour {
private Animator MasterAnim;
public GameObject GMaster;
public bool ikActive;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
MasterAnim = GMaster.GetComponent<Animator> ();
AnimatorStateInfo stateInfo=MasterAnim.GetCurrentAnimatorStateInfo(0);//获取当前状态机

float Statelength=stateInfo.length;//动作的总共时间
Debug.Log ("StateLength" + Statelength);

float normalizedTime=stateInfo.normalizedTime;//返回了当前动作的时间,整数表示已经循环了几次,小数点后的数字表示目前动作执行的进度
//stateInfo.normalizedTime > 0.6f当前状态运行了0.6正交化时间(即动作时长的60%)
Debug.Log("stateNorTime"+normalizedTime);

int nameHash=stateInfo.nameHash;

if (stateInfo.nameHash == Animator.StringToHash ("Base Layer.idle") && !MasterAnim.IsInTransition (0))
{
Debug.Log ("the state is idle");
}
else
{
Debug.Log("the state is other");
}

if (stateInfo.loop == true) //判断动画是否是循环播放
{
Debug.Log ("The animation loop");
} else
{
Debug.Log("Tshe animation only one");
}

if (stateInfo.IsName ("Base Layer.idle"))
{
Debug.Log ("动画相等");
}
else
{
Debug.Log("动画不等");
}

MasterAnim.avatar.name="monkeyAvatar";//更换Avatar

string name = MasterAnim.name;//动作所赋的物体名字
Debug.Log("name"+name);

//获取当前Clip
AnimationClip Animaclip = MasterAnim.GetCurrentAnimationClipState(0)[0].clip;

string Clipname=Animaclip.name;//clip名字
Debug.Log ("Clipname"+Clipname);

float ClipLength=Animaclip.length;
Debug.Log ("ClipLength"+ClipLength);

//设置IK动画
/*IK(Inverse Kinematics)反向动力学的使用只能在PRO版中使用

if(ikActive)

{
//设置骨骼的权重,1表示完整的骨骼,如果是0.5哪么骨骼权重就是一半呢,可移动或旋转的就是一半,大家可以修改一下参数试试。

MasterAnim.SetIKPositionWeight(AvatarIKGoal.RightHand,1f);

MasterAnim.SetIKRotationWeight(AvatarIKGoal.RightHand,1f);

//set the position and the rotation of the right hand where the external object is

if(rightHandObj != null)
{
//设置右手根据目标点而旋转移动父骨骼节点

MasterAnim.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);

MasterAnim.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);

}
}
//如果取消IK动画,哪么重置骨骼的坐标。
else
{
MasterAnim.SetIKPositionWeight(AvatarIKGoal.RightHand,0);

MasterAnim.SetIKRotationWeight(AvatarIKGoal.RightHand,0);
}
*/

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: