- Home /
How can I disable the ability to delete gameobjects in scene view?
I have gameobjects that are created from a "create" menu/editor script - when you create these objects, materials (and other things) can be created depending on which options are enabled on the item.
I'd like to add a "Delete This Item" GUILayout.Button which will replace the typical "delete" key functionality when you have a gameobject selected (so that it removes all materials/etc that may have also been created).
Currently. when you delete the object from the scene view, all materials/etc remain in the folders they were added to via AssetDatabase.CreateAsset
Is there any was "lock" or otherwise disable the delete key function on actively selected gameobjects in the scene view? Thanks
Answer by SolarianZ · May 27, 2020 at 06:04 AM
Try this:
[CustomEditor(typeof(YourComponentType))]
public class InterceptKeyboardDeleteEditor : Editor
{
// NOTE: you can still delete GameObjects by right click and select delete in Hierarchy view
protected virtual void OnEnable()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
protected virtual void OnDisable()
{
EditorApplication.hierarchyWindowItemOnGUI -= OnHierarchyGUI;
}
// disable the ability to delete GameObjects in Scene view
protected virtual void OnSceneGUI()
{
InterceptKeyboardDelete();
}
// disable the ability to delete GameObjects in Hierarchy view
protected virtual void OnHierarchyGUI(int instanceID, Rect selectionRect)
{
InterceptKeyboardDelete();
}
// intercept keyboard delete event
private void InterceptKeyboardDelete()
{
var e = Event.current;
if (e.keyCode == KeyCode.Delete)
{
//e.Use(); // warning
e.type = EventType.Used;
}
}
}
See this for more details: https://arvin.blog.csdn.net/article/details/106380122