unity脚本中运行时实例化一个prefab
2017-05-16 15:27
344 查看
在unity中实例化一个prefab 比实例化一个物体省代码,而且更方便灵活
实例化一个object并创建:void Start() { for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.AddComponent<Rigidbody>(); cube.transform.position = new Vector3(x, y, 0); } } }
实例化一个prefab,需提前创建好一个cube,加组件Rigidbody,创建prefab,并把该cube拖到prefab上,代码只需要两句:
void Start() { for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { Instantiate(brick, new Vector3(x, y, 0), Quaternion.identity); } } }
而且修改prefab时,不需要修改代码,灵活性好。
增加大量固定格式的物体,用实例化prefab的方法也更方便
public GameObject prefab; public int numberOfObjects = 20; public float radius = 5f; void Start() { for (int i = 0; i < numberOfObjects; i++) { float angle = i * Mathf.PI * 2 / numberOfObjects; Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; Instantiate(prefab, pos, Quaternion.identity); } }
相关文章推荐
- Unity扩展Editor菜单:提供一个统一的接口,来让策划调节prefab里相关脚本的数值
- 写一个脚本用来监控服务的运行状态并能够重启服务
- 让一个脚本自动定时运行,设置、启动 linux定时脚本 (crontab详解)
- [shell应用进阶]:限制同时运行脚本实例的个数 -- 串行化:换一个思路。
- Linux周期运行一个命令的脚本
- shell脚本同一时间只能有一个在运行
- 编写一个脚本,用它来操纵QTP的运行
- 父shell和子shell的区别,在shell脚本中要cd一个文件夹为什么要用父shell运行
- 关于hive同一个脚本运行多次而每次结果都不相同
- Unity实例化Prefab in C#
- 当遇到SouSouSMap脚本的时候,会实例化一个LSouSouSMap对象
- 一个将java程序运行在linux系统后台的脚本
- 11g R2 运行root.sh脚本不成功的一个例子: 推荐
- sock脚本单用户正常运行,多用户肯定只有一个用户正常运行,这是什么原因
- 使用ctr+c无法终止一个bash脚本,此脚本在循环执行一个需要运行一定时间的命令
- 用vbs针对一个 IP 地址范围运行脚本
- 禁止QQ运行的一个脚本
- 一个计算脚本运行时间的类
- 硬盘检测的一个shell脚本及自动重启自动运行脚本
- 朋友的一个问题:Linux开机如何自动运行自己编写好的shell脚本