Unity编辑器模式下忽略鼠标选中其他物体

在实际开发过程中,会有特殊需求,比如当选中某个物件之后,进入编辑模式,这时希望鼠标在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/how-to-ignore-selecting-other-objects-in-the-editors-sceneview.199587/

https://forum.unity.com/threads/solved-custom-editor-onscenegui-scripting.34137/

https://forum.unity.com/threads/better-onscenegui-refresh.85066/

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值