Created
May 22, 2019 01:18
-
-
Save tarukosu/a118a30645d092af141062f6fc184944 to your computer and use it in GitHub Desktop.
BoxCollider の Edit Collider のような UI
This file contains 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 UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class BoundingBoxSample : MonoBehaviour | |
{ | |
public Vector3 BoundingBoxCenter; | |
public Vector3 BoundingBoxSize; | |
} | |
#if UNITY_EDITOR | |
public class BoundingBoxEditorScript | |
{ | |
[DrawGizmo(GizmoType.NonSelected | GizmoType.Active)] | |
static void DrawExampleGizmos(BoundingBoxSample bb, GizmoType gizmoType) | |
{ | |
var transform = bb.transform; | |
Gizmos.color = new Color32(145, 244, 139, 210); | |
var center = bb.BoundingBoxCenter; | |
Gizmos.matrix = Matrix4x4.TRS(transform.TransformPoint(center), transform.rotation, transform.lossyScale); | |
var scale = Vector3.Scale(bb.BoundingBoxSize, transform.lossyScale); | |
Gizmos.DrawWireCube(Vector3.zero, scale); | |
} | |
} | |
[CustomEditor(typeof(BoundingBoxSample))] | |
public class ExampleInspector : Editor | |
{ | |
void OnSceneGUI() | |
{ | |
Tools.current = Tool.None; | |
var bb = target as BoundingBoxSample; | |
var transform = bb.transform; | |
Handles.color = new Color(0f, 1f, 0f, 1f); | |
EditorGUI.BeginChangeCheck(); | |
var plusXCenter = transform.TransformPoint(bb.BoundingBoxCenter + (bb.BoundingBoxSize.x / 2) * Vector3.right); | |
Vector3 value = Handles.Slider(plusXCenter, transform.right, 0.1f, Handles.DotHandleCap, 1f); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
var point = transform.InverseTransformPoint(value) - bb.BoundingBoxCenter; | |
bb.BoundingBoxSize = new Vector3(point.x * 2, bb.BoundingBoxSize.y, bb.BoundingBoxSize.z); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment