码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d 常用的方法

时间:2017-08-22 00:28:30      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:常用   init   this   unity   return   param   ret   unit   gre   

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);
            }
        }
    }

 

Unity3d 常用的方法

标签:常用   init   this   unity   return   param   ret   unit   gre   

原文地址:http://www.cnblogs.com/youmingkuang/p/7407137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!