Created
September 24, 2022 15:46
-
-
Save Milk-Drinker01/9bc788ef350cd78b319e4a6a5c2e3079 to your computer and use it in GitHub Desktop.
Unity Editor Bounds Handle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| #if UNITY_EDITOR | |
| using UnityEditor; | |
| using UnityEditor.IMGUI.Controls; | |
| #endif | |
| public class BoxBoundsExample : MonoBehaviour | |
| { | |
| public Bounds bounds = new Bounds(Vector3.zero, Vector3.one); | |
| } | |
| #if UNITY_EDITOR | |
| [CustomEditor(typeof(BoxBoundsExample))] | |
| public class BoxBoundsEditor : Editor | |
| { | |
| private BoxBoundsHandle m_BoundsHandle = new BoxBoundsHandle(); | |
| void OnSceneGUI() | |
| { | |
| BoxBoundsExample boundsScript = (BoxBoundsExample)target; | |
| m_BoundsHandle.center = boundsScript.bounds.center; | |
| m_BoundsHandle.size = boundsScript.bounds.size; | |
| // draw the handle | |
| EditorGUI.BeginChangeCheck(); | |
| m_BoundsHandle.DrawHandle(); | |
| if (EditorGUI.EndChangeCheck()) | |
| { | |
| Undo.RecordObject(boundsScript, "Change Bounds"); | |
| Bounds newBounds = new Bounds(); | |
| newBounds.center = m_BoundsHandle.center; | |
| newBounds.size = m_BoundsHandle.size; | |
| boundsScript.bounds = newBounds; | |
| } | |
| } | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment