- Home /
In-editor object movement restriction
Hi, community!
I want to make my own tile system and right now I'm working on root object that will act like a controller for tiles, storing general information about tile size etc.
What I want to do is to place this tile root in (0, 0, 0), set it's scale and rotation to (1, 1, 1) and restrict changing it's transform values.
I've tried to use MonoBehavior's OnValidate(), but it does not work properly. I still can drag root and change it's transform's values in editor and it does not resets. Any ideas?
Here is the code, btw.
using UnityEngine;
using System.Collections;
public class TS_Root : MonoBehaviour {
private static TS_Root instance;
public static TS_Root Instance
{
get
{
if(instance == null)
{
GameObject temp = new GameObject();
temp.name = "TS_Root";
instance = temp.AddComponent<TS_Root>();
}
return instance;
}
}
public static int TileWidth = 64;
public static int TileHeight = 64;
[ExecuteInEditMode]
void OnValidate()
{
if(instance == null) instance = this;
if(transform.position != Vector3.zero) transform.position = Vector3.zero;
if(transform.localScale != Vector3.one) transform.position = Vector3.one;
if(transform.rotation != Quaternion.identity) transform.rotation = Quaternion.identity;
}
}
OnValidate gets called whenever you change something in the inspector of this Component. The Transform is another Component on the same GameObject, and thus you can't get a callback for whenever that component is changed (sadly)
@troien, yeah, I got it. So now I'm looking for a way to solve this problem.
Answer by Baste · Nov 14, 2014 at 12:47 PM
A kinda hacky way to fix this is to make your TS_Root execute in edit mode, and then reset it's size on Update:
[ExecuteInEditMode]
public class TS_Root : MonoBehaviour {
void Update() {
transform.position = Vector3.zero;
transform.localScale = new Vector3(1, 1, 1);
transform.rotation = Quaternion.identity;
}
}
This works pretty well - if you try to move or scale the transform's values, nothing happens.
You should really do it on LateUpdate though.
Or else, another component could move it after you've fixed position.
Answer by GameVortex · Nov 14, 2014 at 12:56 PM
You can use HideFlags for this. Each component on a GameObject has its own hideflag value.
You can use HideFlags.NotEditable to set the component as not editable in the inspctor. If you want to hide the component as well then you can use HideFlags.HideInInspector.
So for restricting movement you can set the Transform to not be editable:
targetObject.transform.hideFlags = HideFlags.NotEditable;
The Transform will be grayed out and non of its values will be editable, but the handles for the object in the scene will still work, so if you want to restrict that then you need to set the gameOBject to not editable as well:
targetObject.hideFlags = HideFlags.NotEditable;
This makes layers, tags and Name also not editable though. This also has the unfortunate effect of setting the HideFlags of all attached components to NotEditable as well, so if you want the GameObject to not be editable but specific components on the GameOBject be editable, you will need to specifically enable them afterwards:
targetObject.GetComponent<MyComponent>().hideFlags = 0;
Your answer
Follow this Question
Related Questions
Missing Monobehavior after switched OS from Windows to Mac 1 Answer
Auto Texture Tiling Script Problem 0 Answers
Access Monobehavior Instance from Static Function of Editor Script 1 Answer
Editor drag&drop override 1 Answer
Setting a custom Tile asset's icon/preview in the Inspector/Project Tab to its sprite 0 Answers