예제 #1
0
        private void AppendProperty(object obj, PropertyInfo propertyInfo)
        {
            Type key = null;

            if (itemTemplateMap.ContainsKey(propertyInfo.PropertyType))
            {
                key = propertyInfo.PropertyType;
            }
            else if (propertyInfo.PropertyType.IsSubclassOf(typeof(Enum)))
            {
                key = typeof(Enum);
            }
            if (key == null)
            {
                string format  = "Skipped property '{0}' (no template found for type '{1}')";
                string message = string.Format(format, propertyInfo.Name, propertyInfo.PropertyType.Name);
                Log(message);
                return;
            }

            GameObject item = Instantiate(itemTemplateMap[key]);

            items.Add(item);

            PropertyGridBinding binding = item.AddComponent <PropertyGridBinding>();

            binding.Initialize(obj, propertyInfo);

            item.name = propertyInfo.Name;
            item.transform.SetParent(itemTemplateMap[key].transform.parent);
            item.AddComponent(PropertyGridItem.TypeMap[key]);
            item.SetActive(true);
        }
예제 #2
0
        public T AppendProperty <T>(string caption, object value)
            where T : Component
        {
            if (!PropertyGridItem.TypeMap.ContainsValue(typeof(T)))
            {
                string format  = "Can not add property of type {0}";
                string message = string.Format(format, typeof(T).Name);
                Log(message);
                return(default(T));
            }

            Type type = PropertyGridItem.TypeMap.FirstOrDefault(pair => pair.Value == typeof(T)).Key;

            if (!itemTemplateMap.ContainsKey(type))
            {
                string format  = "No template found for type {0}";
                string message = string.Format(format, type.Name);
                Log(message);
                return(default(T));
            }

            GameObject item = Instantiate(itemTemplateMap[type]);

            items.Add(item);

            PropertyGridBinding binding = item.AddComponent <PropertyGridBinding>();

            binding.Initialize(caption, value, type);

            item.name = caption;
            item.transform.SetParent(itemTemplateMap[type].transform.parent);
            item.AddComponent <T>();
            item.SetActive(true);

            return(item.GetComponent <T>());
        }