【Unity】カスタムエディタでシーンビューにバウンディングボックスを表示する

投稿者: | 2023-04-25

カスタムエディタを使って、シーンビューにバウンディングボックス(AABB)を表示し、ハンドルで位置やサイズを変更してみました。

スクリプタブルオブジェクト

バウンディングボックスはスクリプタブルオブジェクトが持っています。

  1. using UnityEngine;
  2.  
  3. [CreateAssetMenu(fileName = "TestBounds", menuName = "ScriptableObjects/TestBounds")]
  4. public class TestBounds : ScriptableObject
  5. {
  6. public Bounds bounds = new Bounds(Vector3.zero, Vector3.one);
  7. }

CreateAssetMenu属性を使って、メインメニューのAssets > Create からこのインスタンスを作成できるようにしています。

カスタムエディタを作る

このスクリプトのためのカスタムエディタを作ります。

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomEditor(typeof(TestBounds))]
  5. public class TestBoundsEditor : Editor
  6. {
  7. void OnEnable()
  8. {
  9. // シーンビューのイベントを購読
  10. SceneView.duringSceneGui += OnSceneGUI;
  11. }
  12.  
  13. void OnDisable()
  14. {
  15. // シーンビューのイベント購読を解除
  16. SceneView.duringSceneGui -= OnSceneGUI;
  17. }
  18.  
  19. void OnSceneGUI(SceneView sv)
  20. {
  21. // Boundsの値を取得
  22. var t = (target as TestBounds);
  23. var bounds = t.bounds;
  24.  
  25. EditorGUI.BeginChangeCheck();
  26.  
  27. // バウンディングボックスの中央にハンドルを表示
  28. bounds.center = Handles.PositionHandle(bounds.center, Quaternion.identity);
  29.  
  30. // バウンディングボックスの右面の中央の位置
  31. var rightPos = bounds.center + new Vector3(bounds.size.x / 2f, 0f, 0f);
  32.  
  33. // シーンビューのカメラからの距離
  34. var distance = Vector3.Distance(rightPos, SceneView.currentDrawingSceneView.camera.transform.position);
  35.  
  36. // バウンディングボックスの右面の中央にハンドルを表示
  37. rightPos = Handles.FreeMoveHandle(rightPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  38.  
  39. // バウンディングボックスの左面
  40. var leftPos = bounds.center + new Vector3(-bounds.size.x / 2f, 0f, 0f);
  41. distance = Vector3.Distance(leftPos, SceneView.currentDrawingSceneView.camera.transform.position);
  42. leftPos = Handles.FreeMoveHandle(leftPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  43. // バウンディングボックスの上面
  44. var upPos = bounds.center + new Vector3(0f, bounds.size.y / 2f, 0f);
  45. distance = Vector3.Distance(upPos, SceneView.currentDrawingSceneView.camera.transform.position);
  46. upPos = Handles.FreeMoveHandle(upPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  47.  
  48. // バウンディングボックスの下面
  49. var downPos = bounds.center + new Vector3(0f, -bounds.size.y / 2f, 0f);
  50. distance = Vector3.Distance(downPos, SceneView.currentDrawingSceneView.camera.transform.position);
  51. downPos = Handles.FreeMoveHandle(downPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  52.  
  53. // バウンディングボックスの表面
  54. var forwardPos = bounds.center + new Vector3(0f, 0f, bounds.size.z / 2f);
  55. distance = Vector3.Distance(forwardPos, SceneView.currentDrawingSceneView.camera.transform.position);
  56. forwardPos = Handles.FreeMoveHandle(forwardPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  57.  
  58. // バウンディングボックスの裏面
  59. var backPos = bounds.center + new Vector3(0f, 0f, -bounds.size.z / 2f);
  60. distance = Vector3.Distance(backPos, SceneView.currentDrawingSceneView.camera.transform.position);
  61. backPos = Handles.FreeMoveHandle(backPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  62.  
  63. // ハンドルに変更があったとき
  64. if (EditorGUI.EndChangeCheck())
  65. {
  66. var size = bounds.size;
  67.  
  68. // ボウンディングボックスのサイズを変更
  69. size.x = Mathf.Abs(rightPos.x - leftPos.x);
  70. size.y = Mathf.Abs(upPos.y - downPos.y);
  71. size.z = Mathf.Abs(forwardPos.z - backPos.z);
  72.  
  73. bounds.size = size;
  74. t.bounds = bounds;
  75.  
  76. // 保存対象にする
  77. EditorUtility.SetDirty(t);
  78. }
  79.  
  80. // バウンディングボックスを描画
  81. Handles.DrawWireCube(bounds.center, bounds.size);
  82. }
  83. }

オブジェクトが有効になるとOnEnableメソッドが呼ばれます。シーンビューにCubeやハンドルを描画するために、duringSceneGuiイベントを購読しています。

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomEditor(typeof(TestBounds))]
  5. public class TestBoundsEditor : Editor
  6. {
  7. void OnEnable()
  8. {
  9. // シーンビューのイベントを購読
  10. SceneView.duringSceneGui += OnSceneGUI;
  11. }
  12.  
  13. void OnDisable()
  14. {
  15. // シーンビューのイベント購読を解除
  16. SceneView.duringSceneGui -= OnSceneGUI;
  17. }

OnDisableメソッドで購読を解除しています。

シーンビューでは、ターゲットのBounds型のフィールドを取得して、バウンディングボックスの中心にハンドルを表示しています。

  1. void OnSceneGUI(SceneView sv)
  2. {
  3. // Boundsの値を取得
  4. var t = (target as TestBounds);
  5. var bounds = t.bounds;
  6.  
  7. EditorGUI.BeginChangeCheck();
  8.  
  9. // バウンディングボックスの中央にハンドルを表示
  10. bounds.center = Handles.PositionHandle(bounds.center, Quaternion.identity);

サイズもハンドルで変更できるようにします。まずバウンディングボックスの右面の中央の位置を取得して、そこにハンドルを表示します。位置はバウンディングボックスの中心のX座標にサイズのX座標の半分を足したベクトルです。

  1. // バウンディングボックスの右面の中央の位置
  2. var rightPos = bounds.center + new Vector3(bounds.size.x / 2f, 0f, 0f);
  3.  
  4. // シーンビューのカメラからの距離
  5. var distance = Vector3.Distance(rightPos, SceneView.currentDrawingSceneView.camera.transform.position);
  6.  
  7. // バウンディングボックスの右面の中央にハンドルを表示
  8. rightPos = Handles.FreeMoveHandle(rightPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);

それをHandles.FreeMoveHandleメソッドの第一引数に入れて、同じ変数で戻り値を受けるとハンドルを動かせます。

遠くからでもハンドルが見やすいように、ハンドルのサイズにシーンビューのカメラからの距離をかけています。第5引数で矩形を指定しています。

左面、上下面、表裏面にも同様にハンドルを表示しています。

  1. // バウンディングボックスの左面
  2. var leftPos = bounds.center + new Vector3(-bounds.size.x / 2f, 0f, 0f);
  3. distance = Vector3.Distance(leftPos, SceneView.currentDrawingSceneView.camera.transform.position);
  4. leftPos = Handles.FreeMoveHandle(leftPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  5. // バウンディングボックスの上面
  6. var upPos = bounds.center + new Vector3(0f, bounds.size.y / 2f, 0f);
  7. distance = Vector3.Distance(upPos, SceneView.currentDrawingSceneView.camera.transform.position);
  8. upPos = Handles.FreeMoveHandle(upPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  9.  
  10. // バウンディングボックスの下面
  11. var downPos = bounds.center + new Vector3(0f, -bounds.size.y / 2f, 0f);
  12. distance = Vector3.Distance(downPos, SceneView.currentDrawingSceneView.camera.transform.position);
  13. downPos = Handles.FreeMoveHandle(downPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  14.  
  15. // バウンディングボックスの表面
  16. var forwardPos = bounds.center + new Vector3(0f, 0f, bounds.size.z / 2f);
  17. distance = Vector3.Distance(forwardPos, SceneView.currentDrawingSceneView.camera.transform.position);
  18. forwardPos = Handles.FreeMoveHandle(forwardPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);
  19.  
  20. // バウンディングボックスの裏面
  21. var backPos = bounds.center + new Vector3(0f, 0f, -bounds.size.z / 2f);
  22. distance = Vector3.Distance(backPos, SceneView.currentDrawingSceneView.camera.transform.position);
  23. backPos = Handles.FreeMoveHandle(backPos, Quaternion.identity, 0.01f * distance, Vector3.one * 0.5f, Handles.RectangleHandleCap);

ハンドルに変更があれば、ハンドルの位置からボウンディングボックスのサイズを変更します。例えば、サイズのX座標は、右面のX座標と左面のX座標の差にします。

  1. // ハンドルに変更があったとき
  2. if (EditorGUI.EndChangeCheck())
  3. {
  4. var size = bounds.size;
  5.  
  6. // ボウンディングボックスのサイズを変更
  7. size.x = Mathf.Abs(rightPos.x - leftPos.x);
  8. size.y = Mathf.Abs(upPos.y - downPos.y);
  9. size.z = Mathf.Abs(forwardPos.z - backPos.z);
  10.  
  11. bounds.size = size;
  12. t.bounds = bounds;
  13.  
  14. // 保存対象にする
  15. EditorUtility.SetDirty(t);
  16. }

EditorUtility.SetDirtyメソッドを使わないと保存されないようです。

最後にバウンディングボックスを描画します。

  1. Handles.DrawWireCube(bounds.center, bounds.size);
  2. }
  3. }

これでシーンビューにバウンディングボックスを表示して、ハンドルで変更できました。

コメントを残す

メールアドレスが公開されることはありません。