【问题标题】:Load Animation From Assets Unity从 Unity 资源加载动画
【发布时间】:2016-06-14 04:57:31
【问题描述】:

我是使用 Unity 引擎编写游戏的新手。我已经搜索了“如何从统一的资产中加载动画”,但没有找到解决方案。

我正在处理对撞机的触发器

void OnTriggerCollisionEnter2D(Collision2D coll){

     if(coll.gameObject.tag = "Player"){
        //try to load animation

     }

}

如果你知道,请帮助我!谢谢

【问题讨论】:

  • 动画是如何保存的?作为 AssetBundle 还是只是预制件?你有资产的截图吗?
  • 首先,感谢您的回复:)。我保存到文件夹 Assets/Animations 的动画,如文件 (.anim)

标签: c# unity3d


【解决方案1】:

首先,我认为您将很难使我的解决方案发挥作用,因为您似乎缺乏 Unity 基本知识。

OnTriggerCollisionEnter2D 不是检测Trigger 的有效回调函数。

OnTriggerEnter2D(Collider2D coll) 就是你要找的东西。

其次,在 onTrigger 期间不要做任何事情。在Start()函数中加载动画,然后在OnTriggerEnter2D函数中播放。

if(coll.gameObject.tag = "Player") 也应该是 if(coll.gameObject.tag == "Player")。注意双'='。您与双 '=' 比较不是一个。但效率不高。如果可能,请使用coll.gameObject.CompareTag 而不是coll.gameObject.tag ==

将动画放在Assets/Resources/Animation文件夹中

Animation animation;
AnimationClip animanClip;
string animName = "walk";

// Use this for initialization
void Start()
{
    //Load Animation
    loadAnimation();
}

void loadAnimation()
{
    GameObject tempObj = Resources.Load("Animations/" + animName, typeof(GameObject)) as GameObject;
    if (!tempObj == null)
    {
        Debug.LogError("Animation NOT found");
    }
    else
    {
        animation = tempObj.GetComponent<Animation>();
        animanClip = animation.clip;

        animation.AddClip(animanClip, animName);
    }
}

public void OnTriggerEnter2D(Collider2D coll)
{
    if (coll.gameObject.CompareTag("Player"))
    {
        animation.Play(animName);
    }
}

最后,学习这些教程。

Unity Scripting Tutorial

Unity Physics Tutorial

Other Unity Tutorials

【讨论】:

  • 为什么 GameObject 返回 null ?我正在输入比赛名称和方向。
猜你喜欢
  • 1970-01-01
  • 2011-03-30
  • 2011-07-24
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多