Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple static reflection methods I mostly use for editor extensions.
//This tool is used to copy the values of two objects of the same type, from two seperate scripts.
//Can be used in the inspector to copy long lists from one script to another one with a different type.
public class CopyValueTool : MonoBehaviour {
public string copyFromVariable;
public string pasteToVariable;
public MonoBehaviour copyFromComponent;
public MonoBehaviour pasteToComponent;
public void CopyValues()
{
Reflection.SetValue(pasteToComponent, pasteToVariable, Reflection.GetValue(copyFromComponent, copyFromVariable));
}
}
//A custom inspector for ValueCopyTool.cs. Makes all variables selectable from a pop-up list and draws the CopyValues button.
[CustomEditor(typeof(ValueCopyTool))]
public class CopyValueToolEditor : Editor
{
private string[] copyToValues;
private string[] copyFromValues;
public override void OnInspectorGUI()
{
ValueCopyTool targetScript = (ValueCopyTool)target;
targetScript.copyFromComponent = (MonoBehaviour)EditorGUILayout.ObjectField("Copy Script", targetScript.copyFromComponent, typeof(MonoBehaviour), true);
if (targetScript.copyFromComponent != null)
{
targetScript.copyFromVariable = EditorGUILayout.Popup("Copy Value", targetScript.copyFromVariable, Reflection.GetAllVariableNames(targetScript.copyFromComponent));
}
targetScript.pasteToComponent = (MonoBehaviour)EditorGUILayout.ObjectField("Paste Script", targetScript.pasteToComponent,typeof(MonoBehaviour),true);
if (targetScript.pasteToComponent != null)
{
targetScript.pasteToVariable = EditorGUILayout.Popup("Paste Value", targetScript.pasteToVariable, Reflection.GetAllVariableNames(targetScript.pasteToComponent));
}
if(GUILayout.Button("Copy values"))
{
targetScript.CopyValues();
}
}
}
//Simple static reflection methods, can come in handy for editor extensions and designer tools.
public class Reflection : MonoBehaviour
{
public static void SetValue(object script, string valueName, object newValue)
{
if (HasValue(script, valueName))
{
script.GetType().GetField(valueName).SetValue(script, newValue);
}
}
public static object GetValue(object script, string valueName)
{
if (HasValue(script, valueName))
{
return script.GetType().GetField(valueName).GetValue(script);
}
else
{
return null;
}
}
public static bool HasVariable(object script, string variableName)
{
FieldInfo field = script.GetType().GetField(variableName);
if (field != null)
{
return true;
}
else
{
Debug.Log("Couldn't find value " + variableName + " on " + script);
return false;
}
}
public static string[] GetAllVariableNames(object script)
{
FieldInfo[] fields = script.GetType().GetFields();
string[] variableNames = new string[fields.Length];
for (int i = 0; i < fields.Length; i++)
{
variableNames[i] = fields[i].Name;
}
return variableNames;
}
}
@turbohermit

This comment has been minimized.

Show comment
Hide comment
@turbohermit
Owner

turbohermit commented Jan 3, 2018

gist_tool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment