Author Topic: C# Function to set properties and fields of NGUI components  (Read 1987 times)

FWCorey

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
C# Function to set properties and fields of NGUI components
« on: February 05, 2013, 07:20:26 PM »
Here is a method I have in my Utilities toolbelt. I use it from MonoBehaviours in external dll files to access properties and fields of NGUI widgets.

  1. public static void GenericValueSet(object targetObject, string propName, object value)
  2. {
  3.         Type objectType = targetObject.GetType();
  4.         PropertyInfo info = objectType.GetProperty(propName);
  5.         if (info != null && info.CanWrite)
  6.         {
  7.                 Debug.Log("Setting property " + propName + " to " + value.ToString());
  8.                 info.SetValue(targetObject, value, null);
  9.         }
  10.         FieldInfo finfo = objectType.GetField(propName);
  11.         if (finfo != null && finfo.IsPublic)
  12.         {
  13.                 Debug.Log("Setting public field " + propName + " to " + value.ToString());
  14.                 finfo.SetValue(targetObject, value);
  15.         }
  16. }

Feel free to remove the debug messages and shorten variable names, they are there for troubleshooting purposes when first getting used to using the method. I tried to make it as easy to read as possible.