- Home /
Change position using custom and native editor
Hi, i have a script and a custom editor for it, where i setting up transform.localPosition
[CustomEditor(typeof(MyScript))]
[CanEditMultipleObjects]
public class CustomTransform : Editor {
SerializedProperty XProp;
void OnEnable() {
XProp = serializedObject.FindProperty("x");
}
public override void OnInspectorGUI() {
serializedObject.Update();
EditorGUILayout.IntSlider(XProp, 1,10, "X position");
foreach (MyScript obj in targets) {
Vector2 temp = obj.transform.localPosition;
temp.x = XProp.intValue;
obj.transform.localPosition = temp;
}
}
}
But i also want to be able to change position from native position component, so that values in my script will be updated. Now if i trying to change position using native component it resets values to values from my script.
Comment
Answer by frarees · Mar 30, 2014 at 11:24 AM
Try using EditorGUI.BeginChangeCheck
and EditorGUI.EndChangeCheck
:
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof (MyScript))]
[CanEditMultipleObjects]
public class CustomTransform : Editor {
SerializedProperty XProp;
void OnEnable () {
XProp = serializedObject.FindProperty("x");
}
public override void OnInspectorGUI() {
serializedObject.Update();
EditorGUI.BeginChangeCheck ();
EditorGUILayout.IntSlider(XProp, 1, 10, "X position");
if (EditorGUI.EndChangeCheck ()) {
foreach (MyScript obj in targets) {
Vector2 temp = obj.transform.localPosition;
temp.x = XProp.intValue;
obj.transform.localPosition = temp;
}
serializedObject.ApplyModifiedProperties ();
}
}
}