首页 > 其他分享 >unity 获取scene整体的bound包围盒以及center

unity 获取scene整体的bound包围盒以及center

时间:2022-11-10 15:14:56浏览次数:44  
标签:scene center List bounds renderers Gizmos bound

如下:多场景的情况也适合

参考: https://gamedev.stackexchange.com/questions/134675/unity-bounds-includes-the-center-of-the-scene

 1         private List<Bounds> _worldBoundsList = new List<Bounds>();
 2         [Button("testBound")]
 3         public void TestBounds()
 4         {
 5             _worldBoundsList.Clear();
 6             for (int i = 0; i < SceneManager.sceneCount; i++)
 7             {
 8                 var scene = SceneManager.GetSceneAt(i);
 9                 GameObject[] gameObjects = scene.GetRootGameObjects();
10                 List<Renderer> renderers = new List<Renderer>();
11                 foreach (var o in gameObjects)
12                 {
13                     renderers.AddRange(o.GetComponentsInChildren<Renderer>());
14                 }
15                 var worldBounds = renderers[0].bounds;
16                 for (int j = 1; j < renderers.Count; ++j)
17                 {
18                     worldBounds.Encapsulate(renderers[j].bounds);
19                 }
20                 _worldBoundsList.Add(worldBounds);
21             }
22         }
23         private void OnDrawGizmos()
24         {
25             foreach (var bounds in _worldBoundsList)
26             {
27                 Gizmos.color = new Color(0f, 1f, 0f, 0.1f);
28                 Gizmos.DrawCube(bounds.center, bounds.size);
29 
30                 Gizmos.color = new Color(0f, 1f, 0f, 1f);
31                 Gizmos.DrawWireCube(bounds.center, bounds.size);
32             }
33         }

 

标签:scene,center,List,bounds,renderers,Gizmos,bound
From: https://www.cnblogs.com/sun-shadow/p/16877062.html

相关文章