unity批量获取物体组件修改值,拓展子物体查询
2015-08-18 17:03
585 查看
using UnityEngine; using System.Collections; public class Game : MonoBehaviour { // Use this for initialization void Start () { FindMaterials(this.transform); } // 根据 物体名称 获取 物体下的任何地方的子物体 void FindChild(Transform go,string name,ref Transform tr) { if(go.name.Equals(name)) { tr = go.transform; return; } if(go.childCount>0) { for(int i=0;i<go.childCount;i++) { if(go.GetChild(i).childCount>0) { FindChild(go.GetChild(i), name, ref tr); } if(go.GetChild(i).name.Equals(name)) { tr = go.GetChild(i).transform; } } } } //批量修改 但是只是引用 运行时起作用 void FindMaterials(Transform go) { MeshRenderer temp; if(go!=null&& go.childCount>0) { for(int i =0 ;i<go.childCount;i++) { //temps 第一级子物体 Transform temps = go.GetChild(i); if (temps.childCount > 0) FindMaterials(temps); //获取这一级子物体的 Material 更改 temp = temps.GetComponent<MeshRenderer>(); if (temp == null) { continue; }else { Shader s = Shader.Find("Custom/Mask"); if (temp.material.shader !=s) temp.material.shader = s; } } } } }
相关文章推荐
- Unity Transform 中文翻译
- unity3d Fatal Error : Error: Error #1000: The system is out of memory.
- Unity Component 中文翻译
- iTween方法详解
- Unity GameObject 中文翻译
- Unity Object中文翻译
- unity3d优化总结篇
- Unity_Navmesh详解
- jUnity单元测试
- unity工程中添加多个相机后警报
- Unity摄像机Viewport相关(一)
- unity基础之C#基础——[转]大白话系列之C#委托与事件讲解(一)
- Unity NGUI 资源下载
- Unity Tidy Tile Pack #1
- unity5 rendersetting选项
- Unity-Editor 编辑Inspector面板数据保存问题
- unity跨线程操作的问题
- Unity中针对Android Apk的签名验证(C#实现)
- Unity中针对Android Apk的签名验证(C#实现),防止二次打包
- Unity游戏中使用贝塞尔曲线