- Home /
How to get a ONLY the INSPECTOR variables that can be modified.
Hi , on another question I asked something similar but never actually ended up with a list of PUBLIC variables. HERE
I read many post suggesting to use PropertyInfo.GetSetMethod() and its many different way to use it but this does not work for me as it gives me some unintended variables :(
NOTE: This will output all the component on an object and all their variables , values and type, Now I want to filter those variables so I only output the ones THAT ARE SHOWN IN INSPECTOR
So here is the core of my script :
DrawDefaultInspector();
ScriptSetter myScript = (ScriptSetter)target;
//The refresh button should get a list of component on THAT object , it then gets all the variables . The only component we shouldnt get is myScript
if(GUILayout.Button("REFRESH"))
{
Component[] myComponents = myScript.GetComponents(typeof(Component));
foreach (Component myComp in myComponents)
{
Type myObjectType = myComp.GetType();
if (myObjectType.ToString() != myScript.ToString())
{
foreach (var thisVar in myComp.GetType().GetProperties())
{
//Here are some of my attempts to get ONLY the variables displayed in Inspector
if (!thisVar.GetSetMethod().IsPrivate)
//if (thisVar.GetSetMethod().IsPublic)
//if (thisVar.GetSetMethod() != null)
//if (thisVar.GetSetMethod(true) != null)
//if (thisVar.GetGetMethod() != null)
{
try
{
Debug.Log("Component: " + myComp.name + " Var Name: " + thisVar.Name + " Type: " + thisVar.PropertyType + " Value: " + thisVar.GetValue(myComp,null) + " " + thisVar.Attributes.ToString() + "\n");
}
catch (Exception e)
{
if (e.GetType().ToString() != "System.Reflection.TargetInvocationException") // ignore the exception for deprecated stuff
{
Debug.LogError(e);
}
}
}
}
}
}
}
Now here is an image displaying my results , you can test this too , simply create a new scene and add the script on a before clicking "Refresh".
Your answer
Follow this Question
Related Questions
Variable doesn't reset between test session. Bug? 1 Answer
Distribute terrain in zones 3 Answers
Exposing variables in unity based on an enum 0 Answers
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer