Author Topic: A small delegate modification you guys might like  (Read 2196 times)

sanctus2099

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
A small delegate modification you guys might like
« on: May 10, 2014, 07:10:42 AM »
Hello,

So, when I first saw the delegate parameters I just had to update NGUI because I really needed those things.
I quickly made some changes and I noticed that in the arguments you can't pass as a parameter a specific component of a gameobject (prefab in my case).

I did some snooping around and managed to make it work but I'm not sure if it breaks anything else.

Here are the modifications:

NGUITools.cs

  1. static public string GetFuncName (object obj, string method)
  2.         {
  3.                 if (obj == null) return "<null>";
  4.                 //if (string.IsNullOrEmpty(method)) return "<Choose>";
  5.                 string type = obj.GetType().ToString();
  6.                 int period = type.LastIndexOf('.');
  7.                 if (period > 0) type = type.Substring(period + 1);
  8.  
  9.         if (string.IsNullOrEmpty(method))
  10.             return type;
  11.  
  12.                 return type + "." + method;
  13.         }

and

PrepertyReferenceDrawer.cs

  1. static public List<Entry> GetProperties (GameObject target, bool read, bool write)
  2.         {
  3.                 Component[] comps = target.GetComponents<Component>();
  4.  
  5.                 List<Entry> list = new List<Entry>();
  6.  
  7.                 for (int i = 0, imax = comps.Length; i < imax; ++i)
  8.         {
  9.             Component comp = comps[i];
  10.             if (comp == null) continue;
  11.  
  12.             Type type = comp.GetType();
  13.             BindingFlags flags = BindingFlags.Instance | BindingFlags.Public;
  14.             FieldInfo[] fields = type.GetFields(flags);
  15.             PropertyInfo[] props = type.GetProperties(flags);
  16.  
  17.             {
  18.                 Entry ent = new Entry();
  19.                 ent.target = comp;
  20.                 list.Add(ent);
  21.             }
  22.  
  23.             for (int b = 0; b < fields.Length; ++b)
  24.             {
  25.                 FieldInfo field = fields[b];
  26.  
  27.                 if (filter != typeof(void))
  28.                 {
  29.                     if (canConvert)
  30.                     {
  31.                         if (!PropertyReference.Convert(field.FieldType, filter)) continue;
  32.                     }
  33.                     else if (!filter.IsAssignableFrom(field.FieldType)) continue;
  34.                 }
  35.  
  36.                 Entry ent = new Entry();
  37.                 ent.target = comp;
  38.                 ent.name = field.Name;
  39.                 list.Add(ent);
  40.             }
  41.  
  42.             for (int b = 0; b < props.Length; ++b)
  43.             {
  44.                 PropertyInfo prop = props[b];
  45.                 if (read && !prop.CanRead) continue;
  46.                 if (write && !prop.CanWrite) continue;
  47.  
  48.                 if (filter != typeof(void))
  49.                 {
  50.                     if (canConvert)
  51.                     {
  52.                         if (!PropertyReference.Convert(prop.PropertyType, filter)) continue;
  53.                     }
  54.                     else if (!filter.IsAssignableFrom(prop.PropertyType)) continue;
  55.                 }
  56.  
  57.                 Entry ent = new Entry();
  58.                 ent.target = comp;
  59.                 ent.name = prop.Name;
  60.                 list.Add(ent);
  61.             }
  62.         }
  63.                 return list;
  64.         }

It would be swell if this change would be added directly on NGUI.

It basically enables buttons to call functions like this

  1. public void OnShopBuilding(Building prefab)
  2.     {
  3.         StartPlacingBuilding(prefab);
  4.     }

where Building is derived from MonoBehavior

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: A small delegate modification you guys might like
« Reply #1 on: May 11, 2014, 12:24:24 AM »
It's fine, but you missed a property conversion check in your GetProperties addition:
  1.                         if (PropertyReference.Convert(comp, filter)) // <-- this right here
  2.                         {
  3.                                 Entry ent = new Entry();
  4.                                 ent.target = comp;
  5.                                 list.Add(ent);
  6.                         }