您的位置:首页 > 移动开发 > Objective-C

API_Object_GameObject类

2020-06-25 04:26 204 查看
using UnityEngine;
/*
* API_Object_GameObject
* 2020/6/4
*/
public class API_Object_GameObject : MonoBehaviour
{
public GameObject go;
// Start is called before the first frame update
void Start()
{
print("返回对象的名称:" + go.ToString());
//销毁对象
GameObject.Destroy(go,3.0f);
//在新场景中保留对象
GameObject.DontDestroyOnLoad(go.GetComponent<Rigidbody>());

//用于获取工程中所有符合参数类型的对象,多用于检测工程中是否含有type类型的对象
GameObject.FindObjectsOfType(typeof(Light));   //返回含有Light类型组件的对象数组

//实例化(克隆)对象
GameObject.Instantiate(go,Vector3.zero,Quaternion.identity);

//获取组件,返回Type类型的组件,若没有返回null
go.GetComponent<Transform>();
//增加组件
go.AddComponent<Rigidbody>();

//向自己发送消息   gameObject的名字是该脚本:API_Object_GameObject
gameObject.SendMessage("Attack", gameObject.name + "接收到了发送的消息");
//向自己和子类广播消息  不一定需要接收者,没有也不报错
go.BroadcastMessage("Attack", "接收到了"+go.name +"广播的消息",SendMessageOptions.DontRequireReceiver);
//向自己和父类发送消息
gameObject.SendMessageUpwards("Attack", "接收到了" + this.gameObject + "发送的消息");

//根据给定的true或false值激活/停用GameObject
go.SetActive(false);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: