将旧的 Unity 代码升级到 Unity 5 [英] Upgrading old Unity code to Unity 5

查看:30
本文介绍了将旧的 Unity 代码升级到 Unity 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在触发按钮上播放动画的代码似乎不起作用.我在 Youtube 上看到了一个视频,并带有一个简单的 animation.Play(); 它可以处理该视频,但是,我无法在我的计算机上运行它.我做错了什么还是团结改变了它?请帮助我在网上找不到解决方案.所有解决方案也不起作用".

The code to play animation on trigger button does not seem to work. I saw a video on Youtube and with a simple animation.Play(); it worked on that video but yet, I couldn't get it to work on my computer. What did I do wrong or did unity change it? please help I cant find solution on the net. All the "solution not working either".

这是我得到的错误:

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

Type UnityEngine.Component does not contain a definition for play and no extension method Play of type UnityEngine.component could be found

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");
        //  }
        }
    }
}

推荐答案

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

what did i do wrong or did unity change it?

统一改变了.我见过类似的 过去几周的问题.虽然我不认为它们是重复的,但在这里回答所有这些以备将来的问题是有意义的.

Unity changed. I've seen similar questions for the past few weeks. Although I don't think they are duplicates but it would make sense to answer all these here for future questions.

animation variableComponent 下定义为一个 public 变量,MonoBehaviour 继承自.然后您的代码继承自 MonoBehaviour,并且您可以访问 animation.

The animation variable is defined under Component as a public variable which MonoBehaviour inherits from. Your code then inherits from MonoBehaviour and you have access to animation.

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

These are the complete list of variables from the Component class that are deprecated:

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().将该变量的第一个字母大写,使其成为组件类.一个例外是音频,它变成AudioSource 而不是音频.

Use GetComponent<ComponentName>(). Capitalize the first letter of that variable to make it its component class. One exception is audio which becomes AudioSource instead of Audio.

1.animation.Play("Cube|moving side"); 变成 GetComponent().Play("Cube|moving side");

2.rigidbody2D.velocity 变成 GetComponent().velocity

3.rigidbody.velocity 变成 GetComponent().velocity

4.renderer.material 变成 GetComponent().material

5.particleSystem.Play() 变成 GetComponent().Play()

6.collider2D.bounds 变成 GetComponent().bounds

7.collider.bounds 变成 GetComponent().bounds

8.audio.Play("shoot"); 变成 GetComponent().Play("shoot");

9.camera.depth 变成 GetComponent().depth

我不必将所有这些都列出来,因为下面的示例应该可以指导任何人执行此操作.这些是 SO 上最常被问到和被问到的组件.

I don't have to put all of them because the example below should guide anyone do this. These are the most asked and asked components on SO.

缓存组件:

您可以缓存组件,这样您就不必每次都使用 GetComponent.

You can cache component so that you don't have be GetComponent everytime.

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

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

这篇关于将旧的 Unity 代码升级到 Unity 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆