Using an IntSlider to control the transform of a Serialized Object

I’m building a custom editor and would like to control the transform with an IntSlider and/or (int * float) calculations but all attempts have failed… This code executes fine in MonoDevelop but bombs out in Unity for ‘type is not supported int value’. I understand why this does not work… what I need help/guidance with is getting the functionality I am looking for. Thanks!

using UnityEngine;
using UnityEditor;
 
[CustomEditor(typeof(Transform)), CanEditMultipleObjects]
public class testTranspose2 : Editor 
{
    private SerializedObject m_object;
    private SerializedProperty m_propPosition;
 
    private void OnEnable()
    {
       m_object = new SerializedObject(target);
       m_propPosition = m_object.FindProperty("m_LocalPosition.x");
		
    }
 
    public override void OnInspectorGUI()
    {
       m_object.Update();
 
       	EditorGUILayout.IntSlider(m_propPosition,0,9);
 
       m_object.ApplyModifiedProperties();
    }
}

Try: m_propPosition.intValue = EditorGUILayout.IntSlider(m_propPosition.intValue,0,9); Regarding m_propPosition = m_object.FindProperty("m_LocalPosition.x");, will this line work, it will extract the transform.localPosition.x? I have never seen anything like this before (just to put it out there, I am not doubting about this line, I am just asking because I just don't know)

Is it possible that .floatValue will work because transform.localPosition.x is float, not int?

1 Answer

1

So I think I figured out the cause of my problem… and if not, at the very least, fixed my problem.

Appears my issue stems from trying to change the type (in this case, to int) of the serialized property. Realizing that they’re generic, once I changed the targetScript and editorScript to reflect the changes below, it all worked as it should. It only works if you can allow ExecutiveInEditMode() on your target script… and in my case, that does happen to work for me.

targetScript:

@script ExecuteInEditMode()
var startX : int; 
function Update () {
  transform.position.x = startX;}

editorScript:

   @CustomEditor(targetScript)
    public class editorScript extends Editor {
       var startX : SerializedProperty;
    function OnEnable () {
       startX = serializedObject.FindProperty("startX");}
    function OnInspectorGUI() {
       EditorGUILayout.IntSlider(startX,-10,10);}}

The change from C# to JS was due to my example of a working Transform editor script coming from another forum post on here... Sorry if it screws anybody up, but most of this swaps over pretty easily.