8,303
社区成员
发帖
与我相关
我的任务
分享
using UnityEngine;
using System.Collections;
public class AnimTriggers : MonoBehaviour
{
// Create a reference to the animator component
private Animator animator;
void Start ()
{
// initialise the reference to the animator component
animator = gameObject.GetComponent<Animator>();
}
// check for colliders with a Trigger collider
// if we are entering something called JumpTrigger, set a bool parameter called JumpDown to true..
void OnTriggerEnter(Collider col)
{
if(col.gameObject.name == "JumpTrigger")
{
animator.SetBool("JumpDown", true);
}
}
// ..and when leaving the trigger, reset it to false
void OnTriggerExit(Collider col)
{
if(col.gameObject.name == "JumpTrigger")
{
animator.SetBool("JumpDown", false);
}
}
}
AnimatorStateInfo currentState = animator.GetCurrentAnimatorStateInfo(0);
if (currentState.nameHash == Animator.StringToHash("Base Layer.Idle"))
{
if (!hasSet) {
Action act = new Action();
act.Att = this.transform;
act.Def = this.transform;
AnimationEvent evt = new AnimationEvent();
evt.time = 0;
evt.functionName = "Test";
evt.objectReferenceParameter = act as Object;
AnimationInfo[] info = animator.GetCurrentAnimationClipState(0);
foreach (AnimationInfo i in info) {
i.clip.AddEvent(evt);
hasSet = true;
}
}
}
但是感觉也不好。我觉得你这种动态在代码里设置参数的需求是要得到Animation Clip的吧,但是用新的动画系统这种要求本身就比较麻烦。。。感觉这个系统还有很多API没有开放,不是很灵活。
我尽力了。。。
using UnityEngine;
using System.Collections;
public class Try : MonoBehaviour {
public class Action : ScriptableObject
{
public Transform Att;
public Transform Def;
}
// Use this for initialization
void Start () {
Action act = new Action();
act.Att = this.transform;
act.Def = this.transform;
AnimationEvent evt = new AnimationEvent();
evt.time = 0;
evt.functionName = "Test";
evt.objectReferenceParameter = act as Object;
animation.GetClip("ani").AddEvent(evt);
Debug.Log(act.Att.gameObject.name);
}
public void Test(Action obj) {
Action act = obj as Action;
Debug.Log(act.Att.gameObject.name);
}
}