Exemplo n.º 1
0
        static bool PopupProperty(FieldInfo p_fieldInfo, Object p_object, GUIContent p_name)
        {
            if (!IsPopupProperty(p_fieldInfo))
            {
                return(false);
            }

            ClassPopupAttribute popupAttribute = p_fieldInfo.GetCustomAttribute <ClassPopupAttribute>();

            // We are caching assembly domain lookups as it is heavy operation
            // TODO need to enable option to recache later since users can implement new types
            if (!cachedTypes.ContainsKey(popupAttribute.ClassType))
            {
                cachedTypes[popupAttribute.ClassType] = ReflectionUtils.GetAllTypes(popupAttribute.ClassType);
            }

            List <string> options = cachedTypes[popupAttribute.ClassType].Select(c => c.ToString()).ToList();

            options.Insert(0, "NONE");
            object value = p_fieldInfo.GetValue(p_object);
            int    index = value == null ? 0 : options.IndexOf(value.ToString());

            EditorGUILayout.BeginHorizontal();

            EditorGUI.BeginChangeCheck();
            int newIndex = EditorGUILayout.Popup(p_name, index, options.ToArray());

            if (index != 0)
            {
                if (GUILayout.Button(IconManager.GetIcon("Script_Icon"), GUIStyle.none, GUILayout.MaxWidth(20), GUILayout.MaxHeight(20)))
                {
                    AssetDatabase.OpenAsset(EditorUtils.GetScriptFromType(cachedTypes[popupAttribute.ClassType][index - 1]), 1);
                }
            }
            EditorGUILayout.EndHorizontal();

            if (EditorGUI.EndChangeCheck())
            {
                if (newIndex != index)
                {
                    p_fieldInfo.SetValue(p_object,
                                         newIndex == 0 ? null : Activator.CreateInstance(cachedTypes[popupAttribute.ClassType][newIndex - 1]));
                }

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        static bool IsPopupProperty(FieldInfo p_fieldInfo)
        {
            ClassPopupAttribute popupAttribute = p_fieldInfo.GetCustomAttribute <ClassPopupAttribute>();

            return(popupAttribute != null);
        }