在实际开发过程中,会有特殊需求,比如当选中某个物件之后,进入编辑模式,这时希望鼠标在Scene视图不要再选中其他物体
可以通过Editor.OnSceneGUI中调用HandleUtility.AddDefaultControl来实现
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(MyXxxObject))]
public class MyXxxEditor : Editor
{
void OnSceneGUI ()
{
Event e = Event.current;
if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)
{
// 忽略鼠标选中其他物体
HandleUtility.AddDefaultControl(0);
// 射线检测
Ray mouseRay = HandleUtility.GUIPointToWorldRay(e.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(mouseRay, out hit, 5000)
{
// 绘制操作手柄
Handles.color = new Color(1f, 0f, 0f, 0.5f);
Handles.DrawSolidDisc(hit.point, hit.normal, 10);
// 立即更新视图
SceneView.RepaintAll();
}
}
}
}
参考:
https://forum.unity.com/threads/solved-custom-editor-onscenegui-scripting.34137/
https://forum.unity.com/threads/better-onscenegui-refresh.85066/