54 lines
1.9 KiB
C#
54 lines
1.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
[CustomEditor(typeof(VolumeLightDrawer))]
|
|
public class RectangleDrawerEditor : Editor
|
|
{
|
|
private void OnSceneGUI()
|
|
{
|
|
VolumeLightDrawer drawer = (VolumeLightDrawer)target;
|
|
// 将中心点转换为世界坐标
|
|
Transform transform = drawer.transform;
|
|
|
|
EditorGUI.BeginChangeCheck();
|
|
Vector3 minPos = Handles.PositionHandle(drawer.BoundMinPos, Quaternion.identity);
|
|
Vector3 maxPos = Handles.PositionHandle(drawer.BoundMaxPos, Quaternion.identity);
|
|
Vector3 worldCenter = (minPos + maxPos) / 2;
|
|
Vector3 minPos2 = Vector3.Min(minPos, maxPos);
|
|
Vector3 maxPos2 = Vector3.Max(minPos, maxPos);
|
|
Vector3 Size = minPos2 - maxPos2;
|
|
|
|
if (EditorGUI.EndChangeCheck())
|
|
{
|
|
Undo.RecordObject(drawer, "Move Rectangle Center");
|
|
drawer.BoundMinPos = minPos;
|
|
drawer.BoundMaxPos = maxPos;
|
|
drawer.transform.position = worldCenter;
|
|
}
|
|
else
|
|
{
|
|
//只要选中这个物体,之后所有的鼠标拖拽事件这里都可以监控到
|
|
if (Event.current.type == EventType.MouseDrag)
|
|
{
|
|
Undo.RecordObject(drawer, "Move drawer Center");
|
|
drawer.BoundMinPos = transform.position - Size/2; //minPos;
|
|
drawer.BoundMaxPos = transform.position + Size/2;//maxPos;
|
|
//如果加入下面的代码,这个鼠标拖拽事件就不会被引擎内的控件相应,也就拖不动物体自己的坐标轴了
|
|
//使用改方法使鼠标事件被自己创造的坐标控件截胡
|
|
// Event.current.Use();
|
|
}
|
|
|
|
}
|
|
|
|
// 绘制矩形线框
|
|
Handles.color = drawer.lineColor;
|
|
Handles.DrawWireCube(worldCenter,Size);
|
|
|
|
}
|
|
|
|
}
|