Unity3D: Get inspector property values programatically

25 Oct

Recently, I found an interesting question in Unity3D. How to get some field value shown in Unity3D inspector programmatically? Largely this is an undocumented area, and it took me some research to get this done.

In what follows, I am getting the value for Input Manage Axes, as shown below:



  • The first hack is that all the axes information is kept in a binary file called “InputManager.asset” in ProjectSettings folder. So we load it into memory, and Unity3D returns an array of object.
  • Only the first entity in this array is of our interest, which is the InputManager object;
  • We then convert this object to SerializedObject via the constructor
  • To get the Axes object shown in the above figure, SerializedObject.FindProperty() method is called, with the parameter value set as m_Axes
  • We then loop in the obtained array of axes, and for each axis:
    • we get its display name, e.g. “Horizontal” for the 1st axis
    • we then navigate, one by one, down to its siblings to access each SerializedProperty
    • from the figure, it was clear that we need to go to next 5 times, to get the “Positive Button” field, which is the value of my interest;
    • To get its value, which is “right” in this case, SerializedProperty.stringValue is called

That is it!

You might ask me how to get these hack tips, I would say: debug! Add a break point in the code, and add a watch value, you will find the object in memory, and do some trial-n-error experimentation, and you will get it!


Below is the code snippet and the results.

var inputManager = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];	
SerializedObject obj = new SerializedObject(inputManager);
SerializedProperty axisArray = obj.FindProperty("m_Axes");
if (axisArray.arraySize == 0)
	Debug.Log("No Axes");

for( int i = 0; i < axisArray.arraySize; ++i )
	var axis = axisArray.GetArrayElementAtIndex(i);
	var name = axis.displayName;  //axis.displayName  "Horizontal"	string
	axis.Next (true);		//axis.displayName	    "Name"	string
         axis.Next (false);  	//axis.displayName	"Descriptive Name"	string
	axis.Next (false);  	//axis.displayName	"Descriptive Negative Name"	string
	axis.Next (false);  	//axis.displayName	"Negative Button"	string
	axis.Next (false);  	//axis.displayName	"Positive Button"	string
	var value = axis.stringValue;  //"right"

	Debug.Log(name + " | " + value);


Happy coding and enjoy!

1 Comment

Posted by on October 25, 2016 in Programming, Unity 3d


Tags: , , , , , , , , , , ,

One response to “Unity3D: Get inspector property values programatically

  1. stardust

    December 2, 2017 at 12:30 am

    Wow, thank you so much for that post, you saved my live! (A lot of time.) I tried to figure it out for hours now, and I’m really glad I found this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: