【问题标题】:Upgrading old Unity code to Unity 5将旧的 Unity 代码升级到 Unity 5
【发布时间】:2017-01-21 22:04:31
【问题描述】:

在触发按钮上播放动画的代码似乎不起作用。我在 Youtube 上看到了一个视频,并用一个简单的animation.Play(); 处理了那个视频,但是我无法让它在我的电脑上工作。我做错了什么还是团结改变了它?请帮助我在网上找不到解决方案。所有“解决方案也不起作用”。

这是我得到的错误:

类型UnityEngine.Component 不包含播放定义 并且没有 Play 类型的扩展方法 UnityEngine.component 可以 被发现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class animationtrigger : MonoBehaviour {


    void Start()
    {

    }
    int speed = 10;
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown("N"))
        {
            animation.Play("Cube|moving side");
            //transform.Translate(1 * Vector3.forward * Time.deltaTime * speed);
            //Animator anim = GetComponent<Animator>();
            //if (null != anim)
        //  {
        //      anim.Play("Cube|moving side");
        //  }
        }
    }
}

【问题讨论】:

  • 不起作用是什么意思?你有什么错误吗?解释您遇到的问题。
  • 我在问题中添加了我遇到的错误
  • 检查我的答案。如果有错误消息,请在下次包含您的错误消息。您正在寻找 #1。阅读其余部分,否则您最终会再次提出类似问题

标签: c# unity3d


【解决方案1】:

我做错了什么还是统一改变了它?

团结改变了。在过去的几周里,我看到了similar 的问题。虽然我不认为它们是重复的,但在这里回答所有这些以备将来的问题是有意义的。

animation 变量Component 下定义为public 变量,MonoBehaviour 继承自该变量。然后,您的代码继承自 MonoBehaviour,并且您可以访问 animation

这些是 Component 类中已弃用的变量的完整列表:

public Component animation { get; }
public Component audio { get; }
public Component camera { get; }
public Component collider { get; }
public Component collider2D { get; }
public Component constantForce { get; }
public Component guiElement { get; }
public Component guiText { get; }
public Component guiTexture { get; }
public Component hingeJoint { get; }
public Component light { get; }
public Component networkView { get; }
public Component particleEmitter { get; 
public Component particleSystem { get; }
public Component renderer { get; }
public Component rigidbody { get; }
public Component rigidbody2D { get; }

访问附加到同一脚本的组件的新方法

使用GetComponent&lt;ComponentName&gt;()。将该变量的第一个字母大写,使其成为组件类。一个例外是音频,它变成 AudioSource 而不是音频。

1.animation.Play("Cube|moving side"); 变为 GetComponent&lt;Animation&gt;().Play("Cube|moving side");

2.rigidbody2D.velocity变成GetComponent&lt;Rigidbody2D&gt;().velocity

3.rigidbody.velocity 变为 GetComponent&lt;Rigidbody&gt;().velocity

4.renderer.material 变为 GetComponent&lt;Renderer&gt;().material

5.particleSystem.Play() 变为 GetComponent&lt;ParticleSystem&gt;().Play()

6.collider2D.bounds变成GetComponent&lt;Collider2D&gt;().bounds

7.collider.bounds 变为 GetComponent&lt;Collider&gt;().bounds

8.audio.Play("shoot"); 变为 GetComponent&lt;AudioSource&gt;().Play("shoot");

9.camera.depth 变为 GetComponent&lt;Camera&gt;().depth

我不必将所有这些都列出,因为下面的示例应该可以指导任何人这样做。这些是 SO 上被问到最多和被问到的组件。

缓存组件

你可以缓存组件,这样你就不必每次都是GetComponent。

Animation myAnimation;
void Start()
{
    //Cache animation component
    myAnimation = GetComponent<Animation>();
}

void Update()
{
    if(somethingHappens)
    {
        //No GetComponent call required again
        myAnimation.Play("Cube|moving side");
    }
}

【讨论】:

  • 在我更改代码后,monodevelop 不再给出代码错误,但由于某种原因,即使我已经上传了动画,unity 仍然找不到。
  • 确保动画被检查为“遗留”,因为Animation 组件是遗留的东西。查看this 帖子,了解您的新问题的完整解决方案。
  • nvm 我发现答案只需要进行调整(对象)-> Rig(选项卡)-> 将动画类型更改为 LEGACY
  • 哈哈。我们几乎同时打字。感谢程序员所做的一切。非常感谢您的帮助:)
【解决方案2】:

如果您正在使用动画师并且想要播放特定状态,那么您可以这样做:

animator.Play("stateName");

我在下面的注释代码中看到了这一点。那应该工作。如果没有发生任何事情,请确保“Cu​​be|moving side”是一个状态名称而不是动画名称。还要确保状态实际上包含动画,并且动画实际上具有帧数据。

(下面,我有 2 个状态(移动和空闲)。状态名称是“移动”,而动画名称是“测试”。)

如果我想切换到移动状态,我会调用 animator.Play("Move")

另外,我不建议明确播放状态。如果可以管理,通常最好制作状态树并使用参数触发动画。您可以在上图中看到我如何拥有两种状态以及它们之间的转换。您可以分配参数,使我的动画师从一个切换到另一个。

在这里,我使用“示例动画触发器”使动画师从空闲状态切换到移动状态。我可以在代码中做到这一点:

animator.SetTrigger("ExampleAnimTrigger");

这是使用 Unity 动画的更现代的方式,因此我强烈建议您选择它。

Here's the documentation!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多