- Home /
Any way to lock an object from changes in the editor?
Is there any way to lock a game object so it can no longer be changed in the editor? I haven't been able to find anything that resembles this, so it's probably a feature request, but thought I'd ask first.
The aim is to freeze all the parameters and components of a game object with a single "lock" switch. This would be helpful in setting up rigs where certain game objects should not be changed or moved particularly when working on a team. Of course, anyone should be able to unlock the object to make changes, but it would be a deliberate action.
UPDATE: I've created a feature request to fully lock objects in the editor, making them read-only: http://feedback.unity3d.com/forums/15792-unity/suggestions/1579409-lock-game-objects-from-changes
In the meantime, I think a scripting approach would work in certain cases, though it really falls short of making the object truly locked.
Answer by Bunny83 · Mar 10, 2011 at 01:57 AM
Well, as far as i know there's nothing like that in Unity (at least not yet). You can make your own freeze script to stop the object from moving/rotating. Just create a script with ExecuteInEditMode and with a flag to enable disable movement. I would also specify whether to freeze it locally or globally.
Here, i just wrote that "little" script ;)
using UnityEngine; using System.Collections;
[ExecuteInEditMode] public class FreezeObject : MonoBehaviour { public Space space = Space.World; public bool FreezePosition = false; public bool FreezeRotation = false;
private Space m_OldSpace = Space.World;
private bool m_OldFreezePosition = false;
private bool m_OldFreezeRotation = false;
private Vector3 m_Position = Vector3.zero;
private Quaternion m_Rotation = Quaternion.identity;
void Awake()
{
if (Application.isPlaying)
Destroy(this);
}
void Update()
{
if (!Application.isEditor)
{
Destroy(this);
return;
}
if (FreezePosition)
{
// Save current position if enabled
if ((FreezePosition != m_OldFreezePosition) || (space != m_OldSpace))
m_Position = (space == Space.World) ? transform.position : transform.localPosition;
// Freeze the position
if (space == Space.World)
transform.position = m_Position;
else
transform.localPosition = m_Position;
}
if (FreezeRotation)
{
// Save current rotation if enabled
if ((FreezeRotation != m_OldFreezeRotation) || (space != m_OldSpace))
m_Rotation = (space == Space.World) ? transform.rotation : transform.localRotation;
// Freeze the rotation
if (space == Space.World)
transform.rotation = m_Rotation;
else
transform.localRotation = m_Rotation;
}
m_OldSpace = space;
m_OldFreezePosition = FreezePosition;
m_OldFreezeRotation = FreezeRotation;
}
}
Just add it to an object and when you tick freeze position and/or freeze rotation the object will stay in place (local or global). If space is set to World and the object is a child object it will stay in place like it isn't a child object.
Well, if you want to freeze/unfreeze a lot of objects you could centralize the state in another script (on an empty GO). The freeze scripts could use a int var to assign it to some kind of a freeze group... Something similar could be done to hide objects. I think I will write something like that if i can find the time. Well, editor scripting is just pure fun :D.
See UPDATE in original post... This freeze script could be useful, though it doesn't really lock the object against many potential changes. It would be effective though at preventing very specific changes, such as transforms
Answer by Molix · Mar 10, 2011 at 02:05 AM
I can't think of anything built in, but one way would be with a custom inspector. e.g. you could add a "locked" flag to the object (assuming you want it persisted with individual items), then (roughly):
[CustomEditor(typeof(MyClass))]
public class MyClassInspector : Editor
{
void OnInspectorGUI()
{
MyClass targ = target as MyClass;
targ.locked = EditorGUILayout.Toggle( "Locked", targ.locked );
GUI.enabled = !targ.locked;
DrawDefaultInspector();
}
}
That's a nice solution for the Inspector part ;) Haven't tried it yet but looks like it could work.
Another idea: You could check the current selection and just prevent that the user can select some objects. Since you can't do any changes to the object there's no reason to select that object. So if you detect an "illegal" selection changed the selection and exclude the locked objects.
The locking script solution would work, but only with explicit data channels (ie. transforms, parenting). It wouldn't prevent any and all changes to the object. In short what I was hoping for was a way to make an object and all of its attributes/components read-only.
The anti-selection idea is a good one! Hadn't thought of that. Of course though, it would be problematic if you needed to view the inspector for the object in question.
Answer by MadsPB · May 04, 2012 at 11:56 AM
Have you looked at HideFlags ? http://unity3d.com/support/documentation/ScriptReference/HideFlags.html
"NotEditable The object is not be editable in the inspector"
NotEditable works great, however remember you will not be able to get rid of it once you do that. I did write an editor fixme though that will cycle through any of your objects and delete it.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class fix$$anonymous$$e : $$anonymous$$onoBehaviour
{
[$$anonymous$$enuItem("GameObject/Create Other/fix",false, -1)]
static void objectHandler()
{
Object[] oldObject= GameObject.FindObjectsOfTypeIncludingAssets(typeof(uniqueScript));
foreach (Object g in oldObject)
{
Debug.Log ("Destroy" + g.name);
DestroyImmediate(g);
}
}
}
Answer by nickgravelyn · Oct 25, 2012 at 12:49 AM
For anyone just finding this, I released an asset that allows you to lock game objects in the editor: http://forum.unity3d.com/threads/156016-Lock-your-game-objects-with-UnityLock-2-on-the-Asset-Store. It's only $2 on the Asset Store if you're interested. The forum thread has some more details.
Your answer
Follow this Question
Related Questions
Unity Editor Scripts: GameObject Added / Removed Event? 1 Answer
Adding to GameObject context menu WITHOUT [MenuItem(...)] 1 Answer
Execute Code When Component Added 6 Answers
How do I create a new object in the Editor as a child of another object? 1 Answer
Add gameObject/transform to script component slot with editor 1 Answer