• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Steven-Walker · Mar 09, 2011 at 11:35 PM · gameobjecteditoreditor-scripting

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.

Comment
yoyo
Bunny83
nschrag
Moix
Barrett-Fox
Chvalov94
rad1c
KokodokoGames
andrenospam0
Romeno

People who like this

10 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image

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.

Comment
sumeeton
naglers
Unicorn-slayer
Romeno

People who like this

4 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 10, 2011 at 02:10 AM 0
Share

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.

avatar image Steven-Walker · Mar 10, 2011 at 07:21 PM 0
Share

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

avatar image

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();
  }
}
Comment
Bunny83
AgentFire

People who like this

2 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 10, 2011 at 02:29 AM 0
Share

That's a nice solution for the Inspector part ;) Haven't tried it yet but looks like it could work.

avatar image Bunny83 · Mar 10, 2011 at 02:34 AM 0
Share

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.

avatar image Steven-Walker · Mar 10, 2011 at 06:53 PM 0
Share

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.

avatar image

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"

Comment
Seizure
ChopSushi

People who like this

2 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seizure · Sep 12, 2013 at 08:43 PM 0
Share

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 fixMe : MonoBehaviour
 {
     [MenuItem("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);
         }
     }
 }
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Callback for when a Graphic (Ui) Component is Added to any GameObject using the Editor 0 Answers

Add gameObject/transform to script component slot with editor 1 Answer

Change default sprite position when dragged into Hierarchy 1 Answer

How do you hide a GameObject's handles? 1 Answer

How to access Custom Inspector own gameObject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges