unity开发 --------- prefab
2014-03-19 09:48
183 查看
当我们在EditMode下需要用脚本批量添加prefab时,可以用
注意:如果用GameObject.Instantiate来创建,创建的不是prefab。也就是说当我们在Assets中修改prefab时,Scene中的prefab并不会做相应的修改。
下面是一个在EditMode下自动生成prefab并自动排序的脚本
PrefabUtility.InstantiatePrefab(prefab) as GameObject;
注意:如果用GameObject.Instantiate来创建,创建的不是prefab。也就是说当我们在Assets中修改prefab时,Scene中的prefab并不会做相应的修改。
下面是一个在EditMode下自动生成prefab并自动排序的脚本
using UnityEngine; using UnityEditor; using System.Collections; [ExecuteInEditMode] public class shuzu : MonoBehaviour { public GameObject prefab; public int weidu = 10; public bool reset = false; public bool sort = false; // 由于unity中的二维数组无法在Inspector中显示,所以用下面的形式代替二维数组 [System.Serializable] public class test { public gridId[] testids; } public test[] ids; void Update() { if (reset) { resetnow(); reset = false; } if (sort) { sortnow (); sort = false; } } // 生成prefab,并作为该object的子object。 // 同时关联到二维数组中(此步骤是为了排序用) void resetnow() { while (transform.childCount > 0) DestroyImmediate (transform.GetChild (0).gameObject); foreach (test t in ids) { t.testids = new gridId[weidu]; for(int i = 0; i < weidu; i++){ // 创建prefab GameObject o = PrefabUtility.InstantiatePrefab(prefab) as GameObject; o.transform.parent = transform; o.transform.localScale = Vector3.one; t.testids[i] = o.GetComponent<gridId>(); } } } // 按照二维数组中的顺序,依次排开prefabs void sortnow() { int row = 0; int col = 0; Bounds bon = NGUIMath.CalculateRelativeWidgetBounds(ids[0].testids[0].transform); foreach (test t in ids) { col = 0; foreach(gridId ele in t.testids) { float depth = ele.transform.localPosition.z; ele.transform.localPosition = new Vector3(bon.size.x * col, -bon.size.y*row, depth); col++; } row++; } transform.localPosition = new Vector3 (-bon.size.x / 2 * (weidu - 1), bon.size.y / 2 * (ids.Length - 1), 0); } }
相关文章推荐
- Unity插件开发:PrefabUtility--Prefab实例引用断开和替换
- unityios开发----点击按钮添加Prefab代码
- Unity开发IOS游戏的优化建议
- Unity 3D 建立开发环境
- 【Unity游戏开发】SDK接入与集成——小白入门篇
- 写给VR手游开发小白的教程:(四)补充篇,详细介绍Unity中相机的投影矩阵
- unity开发 斗地主算法—判断手里的牌是否有大于上家的牌(是否显示要不起按钮)
- 用Unity开发Flappy Bird
- unity基础开发--学习EZGUI教程
- unity游戏开发 发布android 游戏 (一)
- Unity滑动折线图开发
- 【小松教你手游开发】【unity实用技能】u3d 层次问题总结
- 关于Unity的开发模式
- [Unity游戏开发]常用类之Time类
- Unity VR开发--场景跳转崩溃
- 使用Photon引擎进行unity网络游戏开发(三)——网络游戏大厅及房间
- unity开发炉石传说系列英雄的水晶代码部分代码
- Unity开发代码管理
- Unity独立开发——中国象棋
- Unity手游开发--NGUI实现冷却技能(CD)功能