Since I was unable to access classes the normal way I though I do try a more... hacky way which worked!
If there is another easier way I would like to know though xD Maybe I did this for nothing :blink:
I want to share this, so others can make even better mods and spend more time finding out stuff
So many possibilities...
In my More Options mod I use the code below to edit/get values.
Example getting Fields from MonoBehaviours.
Click below for the mod (With source code!)
http://steamcommunity.com/sharedfiles/filedetails/?id=408648436
If there is another easier way I would like to know though xD Maybe I did this for nothing :blink:
I want to share this, so others can make even better mods and spend more time finding out stuff
So many possibilities...
In my More Options mod I use the code below to edit/get values.
Code:
object GetFieldValue(MonoBehaviour monoObject, string fieldName)
{
try
{
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;
FieldInfo fieldInfo = monoObject.GetType().GetField(fieldName,bindingFlags);
//fieldInfo.SetValue(monoObject,value);
return fieldInfo.GetValue(monoObject);
//return true;
}
catch(Exception e)
{
ChirpLog.Error( "Unable to get Value: " + monoObject.GetType().Name + ":" + fieldName + ":" + e.Message);
ChirpLog.Flush();
return 0;
}
}
object CallMethod(MonoBehaviour monoObject, string methodName, object[] value = null)
{
object returnValue = null;
try
{
MethodInfo[] methods = monoObject.GetType().GetMethods();
foreach (MethodInfo info in methods)
{
if (info.Name == methodName)
{
returnValue = info.Invoke(monoObject, value); // [2]
}
}
return returnValue;
}
catch(Exception e)
{
ChirpLog.Error("Unable to Call Method: " + monoObject.GetType().Name + ":" + methodName + ":" + e.Message);
ChirpLog.Flush();
return returnValue;
}
}
void SetField(MonoBehaviour monoObject, string fieldName, object value)
{
try
{
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;
FieldInfo fieldInfo = monoObject.GetType().GetField(fieldName,bindingFlags);
fieldInfo.SetValue(monoObject,value);
}
catch(Exception e)
{
ChirpLog.Error("Unable to set value:" + monoObject.GetType().Name + ":" + fieldName + ":" + e.Message);
ChirpLog.Flush();
}
}
Code:
GameObject[] currentObjects = GameObject.FindObjectsOfType<GameObject>() as GameObject[];
MonoBehaviour[] behaviours;
for(int s = 0; s < currentObjects.Length; s++)
{
//ChirpLog.Debug("#####" + currentObjects[s].name +"#####");
if(currentObjects[s].name == "Seagull")
{
behaviours = currentObjects[s].GetComponents<MonoBehaviour>() as MonoBehaviour[];
for(int i = 0; i < behaviours.Length; i++)
{
ChirpLog.Debug(behaviours[i].GetType().Name);
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;
foreach (FieldInfo field in behaviours[i].GetType().GetFields(bindingFlags))
{
ChirpLog.Debug(field.Name +":" + GetFieldValue(behaviours[i],field.Name) );
}
}
}
}
Click below for the mod (With source code!)
http://steamcommunity.com/sharedfiles/filedetails/?id=408648436