添加组件或脚本的方法

1.在Inspctor面板上直接添加

2.使用AddComponent添加组件

public SphereCollider sc;
public void Awake() {
//给游戏物体添加名为FoobarScript的脚本
gameObject.AddComponent("FoobarScript");
//给游戏物体添加球体碰撞器
sc = gameObject.AddComponent("SphereCollider") as SphereCollider;}
3.RequireComponent需要组件

将需要添加的组件放到命名空间下面类上面,将脚本放到需要添加的物体上,组件就会自动添加

using UnityEngine;
using System.Collections;
   [RequireComponent(typeof(Rigidbody))]
public class qw : MonoBehaviour {


}



版权声明:本文为zdmqweasdzxc原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。