Skip to content

Instantly share code, notes, and snippets.

@tarukosu
Created May 22, 2019 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarukosu/a118a30645d092af141062f6fc184944 to your computer and use it in GitHub Desktop.
Save tarukosu/a118a30645d092af141062f6fc184944 to your computer and use it in GitHub Desktop.
BoxCollider の Edit Collider のような UI
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