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.
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.
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();
}
}