GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • 文章归档
  • Unity3D频道
  • 【Unity3D研究院之游戏开发】
  • 【UGUI研究院】
  • 【NGUI研究院之Unity插件】
  • 【Unity3D拓展编辑器】
  • 【你好Unity3D】
  • 【Unity杂文】
  • Godot频道
  • 【Godot学习笔记】
  • Android频道
  • 【Android研究院之游戏开发】
  • 【Android研究院之应用开发】

This view

  • Original page
  • New address

GeezerVison is showing www.xuanyusong.com — Unity3D研究院之自动计算所有子对象包围盒(六) – 雨松MOMO程序研究院

如下图所示,拖入不同的mesh在一个节点下面,自动生成一个BoxCollider包围盒。这个功能可以方便关卡策划,不然手动设置BoxCollider太蛋疼了。。

Snip20150505_1
先选择一个父节点,然后执行下面脚本。。 或者也可以根据需求在关卡编辑器上做成自动的。。

C#
	[MenuItem ("MyMenu/Do Test")]
	static void Test () 
	{
		Transform parent = 	Selection.activeGameObject.transform;
		Vector3 postion = parent.position;
		Quaternion rotation = parent.rotation;
		Vector3 scale = parent.localScale;
		parent.position = Vector3.zero;
		parent.rotation = Quaternion.Euler(Vector3.zero);
		parent.localScale = Vector3.one;

		Collider[] colliders = parent.GetComponentsInChildren<Collider>();
		foreach (Collider child in colliders){
			DestroyImmediate(child);
		}
		Vector3 center = Vector3.zero;
		Renderer[] renders  = 	parent.GetComponentsInChildren<Renderer>();
		foreach (Renderer child in renders){
			center += child.bounds.center;   
		}
		center /= parent.GetComponentsInChildren().Length; 
		Bounds bounds = new Bounds(center,Vector3.zero);
		foreach (Renderer child in renders){
			bounds.Encapsulate(child.bounds);   
		}
		BoxCollider boxCollider = 	parent.gameObject.AddComponent<BoxCollider>();
		boxCollider.center = bounds.center - parent.position;
		boxCollider.size = bounds.size;

		parent.position = postion;
		parent.rotation = rotation;
		parent.localScale = scale;
	}

避免子节点中有残留的Collider,生成前先把所有子节点的Collider删除。

参考文章 http://stackoverflow.com/questions/11949463/how-to-get-size-of-parent-game-object

  • 本文固定链接: https://www.xuanyusong.com/archives/3461
  • 转载请注明: 雨松MOMO2015年05月05日 于 雨松MOMO程序研究院 发表
最后编辑:2016-05-03
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
站内专栏 QQ交谈 腾讯微博 新浪微博
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!
You are visitor 01402622 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.