基本组件操作

using System.Collections;
using UnityEngine;

class Review_01 : MonoBehaviour
{
    public GameObject obj;
    public Transform trans;
    public Vector3 vec;
    void Start()
    {
        //基本组件操作
        //gameObject.activeSelf//是否活动
        obj.tag = "Cube";//标签
        obj.gameObject.layer = 1;//层 
        obj.name = "Cube";//名字

        //transform.position;(位置)
        //transform.Rotate;(旋转)
        //transform.Scale;(缩放)

        //创建游戏对象
        obj = GameObject.CreatePrimitive(PrimitiveType.Cube);

        //克隆游戏对象
        GameObject.Instantiate(obj);

        //查找对象
        //1.名字查找
        GameObject.Find("Cube");
        //2.标签查找
        GameObject.FindGameObjectWithTag("");
        //3.以组建查找
        //GameObject.FindObjectOfType<>();

        //销毁游戏对象,是一个静态的方法
        GameObject.Destroy(obj);

        //对象添加组件
        //obj.AddComponent<>();

        //获取对象的组件
        //1.获取自身组件
        //public Component GetComponent<XXX>(bool includeInactive)
        //public Component[] GetComponents<XXX>(bool includeInactive)

        //2.获取子节点组件
        //public Component GetComponentInChildren<XXX>(bool includeInactive)
        //public Component[] GetComponenstInChildren<XXX>(bool includeInactive)

        //3.获取父节点组件
        //public Component GetComponentInParent<XXX>(bool includeInactive)
        //public Component[] GetComponentsInParent<XXX>(bool includeInactive)

        //删除对象组件
        //1.删除游戏组件
        //Component = obj.GetComponent<xxx>();
        //Destroy(obj);

        //脚本操作Transform
        //查找子节点
        //获取子节点数量
        int child = trans.childCount;

        //按名字(路径)查找
        transform.Find("xxx");
        transform.FindChild("xxx");

        //按索引查找
        trans.GetChild(2);

        //分离子节点
        trans.DetachChildren();

        //设置父节点
        //获取根节点
        Transform not = trans.root;

        //获取父节点
        not = transform.parent;

        //设置父节点
        not.SetParent(transform);

        //物体位移
        transform.Translate(Vector3.up);

        //物体旋转
        //自转
        transform.Rotate(Vector3.up, 3);
        //公转
        transform.RotateAround(Vector3.down, Vector3.forward, 1.5f);

        //看向目标
        transform.LookAt(not);

        //转换坐标系
        //变换位置从物体坐标到世界坐标
        transform.TransformPoint(vec);
        //变换位置从世界坐标到自身坐标
        transform.InverseTransformPoint(vec);
        //将一个方向从局部坐标变换到世界坐标方向
        transform.TransformDirection(vec);
        //将一个方向从世界坐标变换到局部坐标方向
        transform.InverseTransformDirection(vec);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41996509/article/details/80923766