- Home /
How to make my own Transform inspector without using a Custom Inspector to override Transform ?
Hi guys ,
I am creating a script custom Transform component which I place on my objects . I do not wish to use a custom Inspector to override Transform.
I can set the position of my objects from the inspector but when i move the object in the scene and stop dragging it , the object moves back to the position I had set in my custom transform inspector.
here is my script :
using UnityEngine;
[ExecuteInEditMode]
public class CustomTransform : MonoBehaviour {
public Vector3 position
{
get
{
return transform.position;
}
set
{
transform.position = new value;
}
}
}
This is where i will now use my own CustomInspector to target the CustomTransform script and draw the GUI.
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(CustomTransform))]
public class CustomTransformInspector : Editor
{
private float x, y, z;
public override void OnInspectorGUI()
{
CustomTransform myTarget = (CustomTransform)target;
x = EditorGUILayout.FloatField("X", x);
y = EditorGUILayout.FloatField("Y", y);
z = EditorGUILayout.FloatField("Z", z);
myTarget.position = new Vector3(x, y, z);
}
}
Answer by LTK · Sep 01, 2016 at 03:35 AM
Sure if you want position inspector = position you move gameobject on scene, you need add update position current into your custom inspector
void OnEnable()
{
CustomTranform myTarget = (CustomTranform)target;
x = myTarget.position.x;
y = myTarget.position.y;
z = myTarget.position.z;
}
void OnEnable will be called when gameobject selected
nice, that works when i move it in OnInspectorGUI()
[CustomEditor(typeof(CustomTransform))]
public class CustomTransformInspector : Editor
{
private Vector3 pos;
public override void OnInspectorGUI()
{
CustomTransform $$anonymous$$yTransform = (CustomTransform)target;
pos.x = $$anonymous$$yTransform.position.x;
pos.y = $$anonymous$$yTransform.position.y;
pos.z = $$anonymous$$yTransform.position.z;
pos.x = EditorGUILayout.FloatField("X", pos.x);
pos.y = EditorGUILayout.FloatField("Y", pos.y);
pos.z = EditorGUILayout.FloatField("Z", pos.z);
$$anonymous$$yTransform.position = new Vector3(pos.x, pos.y, pos.z);
}
}
Answer by DevMashup · Sep 24, 2020 at 11:39 PM
Thsanks man. I think this will helpme with my custom editor.