GameObj

public class GameObj : MonoBehaviour
{
    //准备用来克隆的对象
    //可以是场景上的对象,也可以是预设体对象
    public GameObject obj4;
    // Start is called before the first frame update
    void Start()
    {
        #region 成员变量
        //name
        print(gameObject.name);
        //rename
        gameObject.name = "robot";
        print(gameObject.name);
        //是否激活
        print(gameObject.activeSelf);
        //是否为静态
        print(gameObject.isStatic);
        //层级
        print(gameObject.layer);
        //标签
        print(gameObject.tag);
        //transform
        print(gameObject.transform.position);

        #endregion
        #region 静态方法
        //创建自带几何体
        //得到了一个GameObject对象,就可以得到它身上挂载的所有脚本信息
        GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        obj.name = "myCube";

        //查找对象
        //通过对象名查找,无法找到失活对象
        //效率低,会所有对象中去查找,未找到会返回null,名字必须完全匹配
        GameObject obj2 = GameObject.Find("Cube");
        if (obj2 != null)
        {
            print(obj2.name);
        }
        else
        {
            print("未找到相应对象");
        }
        //通过tag查找,无法找到失活对象
        GameObject obj3 = GameObject.FindWithTag("Player");
        //GameObject obj3 = GameObject.FindGameObjectWithTag("Player");同上
        if (obj3 != null)
        {
            print(obj3.name);
        }
        else
        {
            print("未找到相应对象");
        }
        //得到某个单个对象有两种方式
        //1.通过public创建成员变量后从Inspector界面进行关联
        //2.通过GameObject自带的API去查找

        //查找多个对象
        //查找多个对象只能通过tag,只能找到激活对象
        GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
        print(objs.Length);
        //Unity中的Object并不是指C#的object
        //Unity中的Object也继承了C#的object
        //寻找场景中挂载的某一个脚本对象
        GameObj o = GameObject.FindObjectOfType<GameObj>();
        print(o.gameObject.name);
        //实例化对象(克隆对象)的方法
        //根据一个GameObject对象,创建出来一个一模一样的对象
        GameObject obj5 = GameObject.Instantiate(obj4);
        //如果继承了MonoBehavior可以直接省去GameObject=>Instantiate(obj4);
        //删除对象的方法,第二个参数为时间
        GameObject.Destroy(obj5,5);
        //Destroy还可以删除脚本
        //GameObject.Destroy(this);
        //这个Destroy方法不会马上移除对象,会在下一帧把这个对象移除并从内存上移除
        //立即移除=> GameObject.DestroyImmediate(obj5); 一般不用

        //过场景不移除,默认情况下切场景时,场景中对象都会被自动删除
        GameObject.DontDestroyOnLoad(this.gameObject);

        #endregion
        #region 成员方法
        //创建空物体
        GameObject obj6 = new GameObject();
        //创建空物体时顺便改名
        GameObject obj7 = new GameObject("new Empty");
        GameObject obj8 = new GameObject("创建时加脚本的空物体",typeof(GameObj));
        //为对象添加脚本
        //在某个对象上动态添加继承MonoBehavior的脚本
        obj6.AddComponent(typeof(GameObj));
        //得到这个脚本
        GameObj gobj = obj6.AddComponent(typeof(GameObj)) as GameObj;
        //泛型添加
        obj7.AddComponent<GameObj>();
        //得到这个脚本
        GameObj gobj2 = obj7.AddComponent<GameObj>();
        //得到脚本同继承Mono的类的方法

        //标签比较
        if(gameObject.CompareTag("Player"))
        {
            print("对象标签是player");
        }

        //激活失活
        obj6.SetActive(false);//失活
        obj6.SetActive(true);//激活
        #endregion
    }

}
posted @ 2025-03-15 16:51  cannedmint  阅读(9)  评论(0)    收藏  举报