Exemplo n.º 1
0
    /// <summary>指定のコンポーネントのフィールドを選択可能なポップアップで表示する</summary>
    public static void FieldsPopupFromComponent(ref int selectedIndex, ref object selectedField, Component component, Type filterType = null, Type filterAttr = null)
    {
        if (component == null)
        {
            MyGUIUtil.BeginContentColorChange(Color.yellow);
            EditorGUILayout.LabelField("[!]Component is null");
            MyGUIUtil.EndContentColorChange();
            return;
        }

        var fields = component.GetType()
                     .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                     .ToArray();

        var props = component.GetType()
                    .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                    .ToArray();

        if (filterType != null)
        {
            fields = fields.Where(item => item.FieldType == filterType).ToArray();
            props  = props.Where(item => item.PropertyType == filterType).ToArray();
        }
        if (filterAttr != null)
        {
            fields = fields.Where(item => Attribute.GetCustomAttribute(item, filterAttr) != null).ToArray();
            props  = props.Where(item => Attribute.GetCustomAttribute(item, filterAttr) != null).ToArray();
        }

        var members = fields
                      .Cast <MemberInfo>()
                      .Union(props.Cast <MemberInfo>())
                      .ToArray();

        if (members.Count() > 0)
        {
            selectedIndex = EditorGUILayout.Popup(selectedIndex, members.Select(item => item.Name).ToArray());
        }
        else
        {
            MyGUIUtil.BeginContentColorChange(Color.yellow);
            EditorGUILayout.LabelField("[!]No fields matched condition");
            MyGUIUtil.EndContentColorChange();
            return;
        }

        var selectedMember = members[selectedIndex];
        var prop           = selectedMember as PropertyInfo;
        var field          = selectedMember as FieldInfo;

        if (prop != null)
        {
            selectedField = prop.GetValue(component, null);
        }
        if (field != null)
        {
            selectedField = field.GetValue(component);
        }
    }
Exemplo n.º 2
0
    public static Component ComponentsPopupFromGameObject(Component selected, GameObject gameObject)
    {
        if (gameObject == null)
        {
            MyGUIUtil.BeginContentColorChange(Color.yellow);
            EditorGUILayout.LabelField("[!]GameObject is null");
            MyGUIUtil.EndContentColorChange();
            return(null);
        }

        var components = gameObject.GetComponents <Component>();
        int cmpIndex   = 0;

        if (selected != null)
        {
            cmpIndex = components
                       .Select((item, index) => new { Index = index, Value = item })
                       .Where(item => item.Value == selected)
                       .Select(item => item.Index)
                       .FirstOrDefault();
        }

        if (components.Count() > 0)
        {
            cmpIndex = EditorGUILayout.Popup(cmpIndex, components.Select(item => item.GetType().Name).ToArray());
        }
        else
        {
            MyGUIUtil.BeginContentColorChange(Color.yellow);
            EditorGUILayout.LabelField("[!]No attached components");
            MyGUIUtil.EndContentColorChange();
            return(null);
        }

        return(components[cmpIndex]);
    }
Exemplo n.º 3
0
        public override void OnInspectorGUI()
        {
            var self = target as TextInjector;

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.LabelField("Text");
            var text = EditorGUILayout.TextArea(self._format, GUILayout.Height(80));

            /*
             * if (!StringUtil.CheckValidFormat(self._format))
             * {
             *      MyGUIUtil.BeginContentColorChange(Color.red);
             *      EditorGUILayout.LabelField("[Invalid format]");
             *      MyGUIUtil.EndContentColorChange();
             * }
             */

            EditorGUILayout.LabelField("Arguments");

            if (self._arguments.Count == 0)
            {
                MyGUIUtil.BeginContentColorChange(Color.yellow);
                EditorGUILayout.LabelField("[No Arguments]");
                MyGUIUtil.EndContentColorChange();
            }
            else
            {
                _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, GUILayout.Height(160f));
                serializedObject.Update();
                var args = serializedObject.FindProperty("_arguments");

                for (int i = 0; i < args.arraySize; i++)
                {
                    EditorGUILayout.LabelField(i.ToString() + ".");
                    EditorGUILayout.BeginVertical("box");

                    var arg       = args.GetArrayElementAtIndex(i);
                    var instance  = (GameObject)arg.FindPropertyRelative(Argument.P_Instance).objectReferenceValue;
                    var index     = arg.FindPropertyRelative(Argument.P_SelectedMemberIndex).intValue;
                    var component = (Component)arg.FindPropertyRelative(Argument.P_Component).objectReferenceValue;
                    var path      = arg.FindPropertyRelative(Argument.P_PropertyPath).stringValue;

                    EditorGUILayout.PropertyField(arg.FindPropertyRelative(Argument.P_Instance), new GUIContent("インスタンス"));
                    arg.FindPropertyRelative(Argument.P_Component).objectReferenceValue =
                        MyEditorGUILayout.ComponentsPopupFromGameObject(component, instance);

                    MyEditorGUILayout.FieldsPopupFromComponent(ref index, ref path, component, null, typeof(Extractable));
                    arg.FindPropertyRelative(Argument.P_PropertyPath).stringValue     = path;
                    arg.FindPropertyRelative(Argument.P_SelectedMemberIndex).intValue = index;

                    EditorGUILayout.EndVertical();
                }
                serializedObject.ApplyModifiedProperties();
                EditorGUILayout.EndScrollView();
            }

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(target, "Changed TextInjectorInspector");
                self._format = text;

                self.Resize();
                self.TextAplly();
            }

            //base.OnInspectorGUI();
        }