In unity, we can set the status for the component, such as the status that cannot be edited. Taking the transform component as an example, we can disable the original functions of the transform component. The Code is as follows:
Using unityengine; using unityeditor; using system. reflection; [customeditor (typeof (Transform)] The public class component cannot be edited: Editor {private editor m_editor; private void onenable () {m_editor = editor. createeditor (target, assembly. getassembly (typeof (editor )). getType ("unityeditor. transforminspector ", true);} public override void oninspectorgui () {If (guilayout. button ("Expand button") {} // start to Disable GUI. enabled = false; m_editor.oninspectorgui (); // Disable GUI termination. enabled = true; If (guilayout. button ("Expand button") {} // base. oninspectorgui ();}}
Similarly, use reflection to get the transform component, and disable the transform component by using gui. Enabled = false. The transform component cannot be edited. The effect is as follows:
In this case, the original functions of the transform component are disabled (gray indicates that the transform component cannot be edited), but this does not affect the two buttons that I expand up and down.
Can we choose whether to disable this component by ourselves? Of course, the Code is as follows:
Using unityengine; using unityeditor; Public Class components cannot be edited. 2 {[menuitem ("gameobject/3D object/lock", false, 0)] Static void lock () {If (selection. gameobjects! = NULL) {foreach (VAR gameobject in selection. gameobjects) {gameobject. hideflags = hideflags. noteditable ;}} [menuitem ("gameobject/3D object/lock/unlock", false, 1)] Static void unlock () {If (selection. gameobjects! = NULL) {foreach (VAR gameobject in selection. gameobjects) {gameobject. hideflags = hideflags. None ;}}}}
The effect is as follows:
In this case, you can select any game object and select 3d object> lock or unlock from the right-click menu ). The principle is to set hideflags for game objects. It should be noted that we do not have to set the hideflags of the game object, or we can set hideflags for a component separately. This will only affect one component, not all.
PS:Hideflags can use bitwise OR (|) to maintain multiple attributes at the same time. The meaning of hideflags is quite understandable. You can enter your own code to debug it.
Hideflags. None clear status
Hideflags. dontsave setting object will not be saved (used only in edit mode and excluded during runtime)
Hideflags. dontsaveinbuild setting object will not be saved after building
Hideflags. dontunloadunuesdasset this object will not be detached when resources. unloadunusedasset () detaches useless Resources
Hideflags. hideanddontsave sets the object to be hidden and should not be saved
Hideflags. hideinhierarchy sets the object to be hidden in the hierarchical view.
Hideflags. hideininspector sets the object to be hidden in the control panel View
Hideflags. noteditable: the setting object cannot be edited.
Extended Editor (11) _ the component cannot be edited