1. 创建一个Scene,并在里面创建一个GameObject。
    Unity脚本篇 - 创建和使用脚本

  2. 编写一个脚本,并作为组件加载到GameObject上。
    Unity脚本篇 - 创建和使用脚本

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

namespace CreateAndUsingScripts {

    public class MainPlayer : MonoBehaviour {

        // Start is called before the first frame update
        private void Start() {
            Debug.Log("I am alive!");
        }

        // Update is called once per frame
        private void Update() {
        }
    }
}
  1. 最后的文件结构是这样。
    Unity脚本篇 - 创建和使用脚本

  2. 在控制台上看到log输出。
    Unity脚本篇 - 创建和使用脚本

参考:https://docs.unity3d.com/Manual/CreatingAndUsingScripts.html

相关文章: