- Home /
Removing transform from Inspector
Hi there, I've written a GUI script which positions elements based on their viewport positions. As such, I don't want to (mistakenly) manipulate the transform of the object, so I wondered is there a way to make the objects transform non-editable or not appear at all in the inspector? Cheers!
not shure, but did you try to use Destroy, or override transform geter?
@lacost: Destroy makes no sense since every GameObject has a Transform. when you try to destroy it you will get an error that you can't destroy it. The transform property has nothing to do with the Transform inspector. The property is just a shortcut for GetComponent
Answer by Bunny83 · Feb 15, 2013 at 08:37 PM
I wouldn't recommend to completely lock the transform. I would do something like this:
void Reset()
{
LockTransform();
}
[ContextMenu("lock transform")]
void LockTransform()
{
transform.hideFlags = HideFlags.NotEditable;
}
[ContextMenu("unlock transform")]
void UnlockTransform()
{
transform.hideFlags = 0;
}
This will automatically lock the Transform component in the inspector when this script is attached or resetted (the Reset function). Additionally you can right click your script to lock and unlock the transform if it's necessary.
It is also possible to completely hide the transform component, but again, it's never a good idea to hide things. If the user doesn't know it's there it's just confusing.
To completely hide the Transform use those value:
HideFlags.NotEditable | HideFlags.HideInInspector | HideFlags.HideInHierarchy;
$$anonymous$$eep in $$anonymous$$d that you can still move / scale / rotate the object with the scene handles and you can still change the parent in the hierarchy. It's always better to show a warning message than trying to lock it down. It just breaks the basic workflow.
Thanks for the response and code Bunny - one thing you mention interests me, " It's always better to show a warning message than trying to lock it down. It just breaks the basic workflow." I see your point, so is it possible to display a message when editting the transform? Thanks again
Well, there are several ways. One would be to remember the position you set from your custom script and whenever you access the position again, check it against your cached data. If it's different show a warning and if it's important that the objects need to use it's original position, revert it to your saved position.
If you want immediate feedback, implement a custom inspector for your class and hook into the update delegate or use any of the callbacks you have in your custom inspector and do the check there.