作为行为组件的脚本

Tutorial

Beginner

+0XP

5 mins

1042

Unity Technologies

作为行为组件的脚本

Unity 中的脚本是什么?了解作为 Unity 脚本的行为组件,以及如何创建这些脚本并将它们附加到对象。

本教程包含在“初级编程”项目中。

下一教程:变量和函数

想了解与此主题相关的更多信息?请与 Unity 开发专家联系以获取一对一的直播课程。

Languages available:

1. 作为行为组件的脚本

using UnityEngine;
using System.Collections;

public class ExampleBehaviourScript : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            GetComponent<Renderer> ().material.color = Color.red;
        }
        if (Input.GetKeyDown(KeyCode.G))
        {
            GetComponent<Renderer>().material.color = Color.green;
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            GetComponent<Renderer>().material.color = Color.blue;
        }
    }
}

Complete this Tutorial