Advertisement
Guest User

Untitled

a guest
Jul 8th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5.  
  6. namespace VisualCore
  7. {
  8.  
  9.     [System.AttributeUsage(System.AttributeTargets.Class
  10.         | System.AttributeTargets.Struct
  11.         | System.AttributeTargets.Field
  12.         | System.AttributeTargets.Property)]
  13.  
  14.     public class EditByUser : System.Attribute
  15.     {
  16.         public string EditorName;
  17.     }
  18.  
  19.     public class PropertyGraber
  20.     {
  21.  
  22.         public class PropertyConfig
  23.         {
  24.  
  25.             public object instance;
  26.             public string EditorName;
  27.             public string name;
  28.  
  29.             private object value;
  30.             private object info;
  31.             private object instanceType;
  32.  
  33.             public PropertyConfig(System.Object inst, System.Object info)
  34.             {
  35.                 instance = inst;
  36.                 this.info = info;
  37.                 instanceType = inst.GetType();
  38.             }
  39.  
  40.             public System.Object GetValue()
  41.             {
  42.                 System.Object val = null;
  43.                 if (info.GetType() == typeof(PropertyInfo))
  44.                     val = ((PropertyInfo)info).GetValue(instance, null);
  45.                 else if (info.GetType() == typeof(FieldInfo))
  46.                     val = ((FieldInfo)info).GetValue(instance);
  47.  
  48.                 return val;
  49.             }
  50.         }
  51.  
  52.         public List<PropertyConfig> Fields;
  53.         //public Component component;
  54.         public GameObject gameObject;
  55.  
  56.         public PropertyGraber(Component c)
  57.         {
  58.             gameObject = c.gameObject;
  59.             GetPropAndFields(c);
  60.         }
  61.  
  62.         public PropertyGraber(GameObject go)
  63.         {
  64.             gameObject = go;
  65.  
  66.             foreach (Component c in go.GetComponents<MonoBehaviour>())
  67.             {
  68.                 GetPropAndFields(c);
  69.             }
  70.         }
  71.  
  72.         private void GetPropAndFields(Component c)
  73.         {
  74.             var atrr = c.GetType().GetCustomAttributes(false);
  75.             if (atrr.Length > 0 && atrr[0].GetType() == typeof(EditByUser))
  76.             {
  77.  
  78.                 PropertyInfo[] props = c.GetType().GetProperties(BindingFlags.Instance
  79.                     | BindingFlags.Public | BindingFlags.DeclaredOnly);
  80.  
  81.                 FieldInfo[] fields = c.GetType().GetFields(BindingFlags.Instance
  82.                     | BindingFlags.Public | BindingFlags.DeclaredOnly);
  83.  
  84.                 foreach (PropertyInfo p in props)
  85.                 {
  86.                     var atr = p.GetCustomAttributes(false);
  87.                     if (atr.Length > 0 && atrr[0].GetType() == typeof(EditByUser))
  88.                     {
  89.                         PropertyConfig pcfg = new PropertyConfig(c, p);
  90.                         pcfg.EditorName = ((EditByUser)atrr[0]).EditorName;
  91.                         pcfg.name = p.Name;
  92.                         Fields.Add(pcfg);
  93.                     }
  94.                 }
  95.  
  96.                 foreach (FieldInfo f in fields)
  97.                 {
  98.                     var atr = f.GetCustomAttributes(false);
  99.                     if (atr.Length > 0 && atrr[0].GetType() == typeof(EditByUser))
  100.                     {
  101.                         PropertyConfig pcfg = new PropertyConfig(c, f);
  102.                         pcfg.EditorName = ((EditByUser)atrr[0]).EditorName;
  103.                         pcfg.name = f.Name;
  104.                         Fields.Add(pcfg);
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement