记录一下在编写编辑器时对unity的一些操作限制,防止遗忘
static void OnHierarchyGUI(int instanceID, Rect selectionRect) { var e = UnityEngine.Event.current; //禁用 TBCParticleSystem 在Hierarchy面板的右键菜单 if (selectionRect.Contains(e.mousePosition) && e.button == 1 && e.type <= EventType.MouseUp) { var ps = Selection.activeGameObject.GetComponent("TBC.TBCParticleSystem"); if (ps != null) { e.type = EventType.Used; return; } } //禁止在编辑器运行过程中在Hierarchy面板[使用快捷键删除]由编辑器创建的 TBCParticleSystem ,用户自己创建的可以删除 if (e.keyCode == KeyCode.Delete) { if (Selection.activeGameObject != null) { var ps = Selection.activeGameObject.GetComponent("TBC.TBCParticleSystem"); if (ps != null) { var assetGUID = (string)TBCEditorExDll.GetReflectionValue(ps.GetType(), "assetGUID", BindingFlags.Public | BindingFlags.Instance, ReflectionType.Property, ps); if (!string.IsNullOrEmpty(assetGUID)) { e.type = EventType.Used; return; } } } } //禁止在编辑器运行过程中在Hierarchy面板[使用快捷键复制] TBCParticleSystem if (e.modifiers == EventModifiers.Control) { if (e.keyCode == KeyCode.D) { var ps = Selection.activeGameObject.GetComponent("TBC.TBCParticleSystem"); if (ps != null) { e.type = EventType.Used; return; } } } //禁止在编辑器打开的时候从Project面板拖拽资源到Hierarchy面板 if (e.type == EventType.DragUpdated) { var objectReferences = DragAndDrop.objectReferences; for (int i = 0; i < objectReferences.Length; i++) { var objectRef = objectReferences[i]; if (objectRef?.GetHashCode() > 0) { e.type = EventType.Used; return; } } } } //禁止SceneView中选中GameObject static void OnSceneGUI(SceneView sceneview) { var e = UnityEngine.Event.current; int controlID = GUIUtility.GetControlID(FocusType.Passive); if (e.type == EventType.Layout) { HandleUtility.AddDefaultControl(controlID); } }
监听事件
SceneView.duringSceneGui += OnSceneGUI; EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
文章评论