Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEngine;
- namespace VisualCore
- {
- [System.AttributeUsage(System.AttributeTargets.Class
- | System.AttributeTargets.Struct
- | System.AttributeTargets.Field
- | System.AttributeTargets.Property)]
- public class EditByUser : System.Attribute
- {
- public string EditorName;
- }
- public class PropertyGraber
- {
- public class PropertyConfig
- {
- public object instance;
- public string EditorName;
- public string name;
- private object value;
- private object info;
- private object instanceType;
- public PropertyConfig(System.Object inst, System.Object info)
- {
- instance = inst;
- this.info = info;
- instanceType = inst.GetType();
- }
- public System.Object GetValue()
- {
- System.Object val = null;
- if (info.GetType() == typeof(PropertyInfo))
- val = ((PropertyInfo)info).GetValue(instance, null);
- else if (info.GetType() == typeof(FieldInfo))
- val = ((FieldInfo)info).GetValue(instance);
- return val;
- }
- }
- public List<PropertyConfig> Fields;
- //public Component component;
- public GameObject gameObject;
- public PropertyGraber(Component c)
- {
- gameObject = c.gameObject;
- GetPropAndFields(c);
- }
- public PropertyGraber(GameObject go)
- {
- gameObject = go;
- foreach (Component c in go.GetComponents<MonoBehaviour>())
- {
- GetPropAndFields(c);
- }
- }
- private void GetPropAndFields(Component c)
- {
- var atrr = c.GetType().GetCustomAttributes(false);
- if (atrr.Length > 0 && atrr[0].GetType() == typeof(EditByUser))
- {
- PropertyInfo[] props = c.GetType().GetProperties(BindingFlags.Instance
- | BindingFlags.Public | BindingFlags.DeclaredOnly);
- FieldInfo[] fields = c.GetType().GetFields(BindingFlags.Instance
- | BindingFlags.Public | BindingFlags.DeclaredOnly);
- foreach (PropertyInfo p in props)
- {
- var atr = p.GetCustomAttributes(false);
- if (atr.Length > 0 && atrr[0].GetType() == typeof(EditByUser))
- {
- PropertyConfig pcfg = new PropertyConfig(c, p);
- pcfg.EditorName = ((EditByUser)atrr[0]).EditorName;
- pcfg.name = p.Name;
- Fields.Add(pcfg);
- }
- }
- foreach (FieldInfo f in fields)
- {
- var atr = f.GetCustomAttributes(false);
- if (atr.Length > 0 && atrr[0].GetType() == typeof(EditByUser))
- {
- PropertyConfig pcfg = new PropertyConfig(c, f);
- pcfg.EditorName = ((EditByUser)atrr[0]).EditorName;
- pcfg.name = f.Name;
- Fields.Add(pcfg);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement