• Unity3d 常用的方法


    1、创建物体

    2、加载物体

    3、寻找物体

    4、添加脚本

    1、创建物体

      GameObject go;
        // Use this for initialization
        void Start () {
            go = new GameObject("New");
        }

    find 方法查找对应的组件(找到第一个匹配的组件)

      GameObject go;
        GameObject goLight;
        Light light;
        // Use this for initialization
        void Start () {
            go = new GameObject("New");
            goLight = GameObject.Find("Directional Light");//页面组件
            light = goLight.GetComponent<Light>();
            light.color = Color.red;
        }

     两个灯光

        GameObject go;
        GameObject goLight;
        GameObject goLight2;
        Light light1;
        Light light2;
        // Use this for initialization
        void Start () {
            go = new GameObject("New");
            goLight = GameObject.Find("1/2/DirectionalLight");
            light1 = goLight.GetComponent<Light>();
            light1.color = Color.red;
    
            goLight2 = GameObject.Find("1 (1)/2/DirectionalLight");
            light2 = goLight2.GetComponent<Light>();
            light2.color = Color.green;
        }

    第二种写法(两个灯光)

        public Transform transRoot;
    
        Transform translight1;
        Transform translight2;
    
        // Use this for initialization
        void Start () {
            
            FindChild(transRoot,"RLight",ref translight1);
            FindChild(transRoot, "GLight", ref translight2);
            translight1.GetComponent<Light>().color = Color.red;
            translight2.GetComponent<Light>().color = Color.green;
        }
        
        /// <summary>
        /// 寻找物体
        /// </summary>
        /// <param name="trans">作为父物体的transform</param>
        /// <param name="findName">寻找的物体名称</param>
        /// <param name="_trans">找到的物体</param>
        void FindChild(Transform trans,string findName,ref Transform _trans)
        {
            if (trans.name.Equals(findName))
            {
                _trans = trans.transform;
                return;
            }
    
            if (trans.childCount!=0)
            {
                for (int i = 0,lenght=trans.childCount; i < lenght; i++)
                {
                    FindChild(trans.GetChild(i),findName,ref _trans);
                }
            }
        }

    2、Awake() & Start()   做初始化

       Update、LateUpdate、FixedUpdate   更新逻辑

       GetComponent 找到其他脚本

       Gameobject.Find 找到其他物体

     3、游戏输入

    在Edit → Project Settings → Input 设置游戏输入

    在脚本中利用Input类来检测输入状态

    Input.GetAxis 返回的值是 -1到1之间,0表示没有输入

  • 相关阅读:
    部署phpmyadmin登录不进去
    无法获取快照信息:锁定文件失败
    nginx: [emerg] BIO_new_file("/etc/nginx/ssl_key/server.crt") failed (SSL: error:02001002:syste
    nginx重启失败
    An error occurred (500 Error)
    Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.
    clnt_create: RPC: Program not registered
    [error] 2230#2230: *84 client intended to send too large body: 1711341 bytes
    lnmp部署知乎出现403
    easyui下拉框过滤优化
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/7407137.html
Copyright © 2020-2023  润新知