Example #1
0
        public static object AnyField(Rect rect, object value, Type type, GUIContent content)
        {
            if (type == null)
            {
                return(value);
            }

            if (typeof(float).IsAssignableFrom(type))
            {
                return(EditorGUI.FloatField(rect, content, (float)value));
            }
            else if (typeof(int).IsAssignableFrom(type))
            {
                return(EditorGUI.IntField(rect, content, (int)value));
            }
            else if (typeof(string).IsAssignableFrom(type))
            {
                return(EditorGUI.TextField(rect, content, (string)value));
            }
            else if (typeof(bool).IsAssignableFrom(type))
            {
                return(EditorGUI.Toggle(rect, content, (bool)value));
            }
            else if (typeof(Vector2).IsAssignableFrom(type))
            {
                return(EditorGUI.Vector2Field(rect, content, (Vector2)value));
            }
            else if (typeof(Vector3).IsAssignableFrom(type))
            {
                return(EditorGUI.Vector3Field(rect, content, (Vector3)value));
            }
            else if (typeof(Vector4).IsAssignableFrom(type))
            {
                return(EditorGUI.Vector4Field(rect, content.text, (Vector4)value));
            }
            else if (typeof(Quaternion).IsAssignableFrom(type))
            {
                return(Vector4ToQuaternion(EditorGUI.Vector4Field(rect, content.text, QuaternionToVector4((Quaternion)value))));
            }
            else if (typeof(Rect).IsAssignableFrom(type))
            {
                return(EditorGUI.RectField(rect, content, (Rect)value));
            }
            else if (typeof(Color).IsAssignableFrom(type))
            {
                return(EditorGUI.ColorField(rect, content, (Color)value));
            }
            else if (typeof(LayerMask).IsAssignableFrom(type))
            {
                return(EditorGUI.LayerField(rect, content, (LayerMask)value));
            }
            else if (typeof(Enum).IsAssignableFrom(type))
            {
                return(EditorGUI.EnumPopup(rect, content, (Enum)value));
            }
            else if (typeof(Type).IsAssignableFrom(type))
            {
                current_id = GUIUtility.GetControlID(FocusType.Passive);
                if (value == null)
                {
                    if (GUI.Button(rect, "(Empty Type)", styles.type_button))
                    {
                        cached_id = current_id;
                        ShowContentTypeMenu();
                    }
                }
                else
                {
                    Type       value_cast = (Type)value;
                    GUIContent b_content  = new GUIContent(value_cast.GetTypeName(false, true), GUIReferrer.GetTypeIcon(value_cast));
                    Rect       b_rect     = new Rect(rect);
                    if (!content.text.IsNullOrEmpty())
                    {
                        b_rect.x += 5.0f + GUIUtils.GetTextWidth(content.text, styles.label);
                        GUI.Label(rect, content);
                    }
                    if (GUI.Button(b_rect, b_content, styles.type_button))
                    {
                        cached_id = current_id;
                        ShowContentTypeMenu();
                    }
                }
                if (cached_value == null)
                {
                    return(value);
                }
                else
                {
                    if (current_id == cached_id)
                    {
                        object result = cached_value;
                        cached_value = null;
                        return(result);
                    }
                    else
                    {
                        return(value);
                    }
                }
            }
            else if (typeof(UnityObject).IsAssignableFrom(type))
            {
                return(EditorGUI.ObjectField(rect, content, (UnityObject)value, type, true));
            }
            else if (typeof(IList).IsAssignableFrom(type) && !typeof(Array).IsAssignableFrom(type))
            {
                Rect area = new Rect(rect);
                GUILayout.BeginArea(area);
                object collection = AnyField(value, type, content);
                GUILayout.EndArea();
                return(collection);
            }
            GUI.Label(rect, string.Format("ERROR 404. {0} Field Not Found!", type.GetTypeName(true)));
            return(value);
        }
Example #2
0
        public static object AnyField(object value, Type type, GUIContent content, params GUILayoutOption[] options)
        {
            if (type == null)
            {
                return(value);
            }

            if (typeof(float).IsAssignableFrom(type))
            {
                return(EditorGUILayout.FloatField(content, (float)value, options));
            }
            else if (typeof(int).IsAssignableFrom(type))
            {
                return(EditorGUILayout.IntField(content, (int)value, options));
            }
            else if (typeof(string).IsAssignableFrom(type))
            {
                return(EditorGUILayout.TextField(content, (string)value, options));
            }
            else if (typeof(bool).IsAssignableFrom(type))
            {
                return(EditorGUILayout.Toggle(content, (bool)value, options));
            }
            else if (typeof(Vector2).IsAssignableFrom(type))
            {
                return(EditorGUILayout.Vector2Field(content, (Vector2)value, options));
            }
            else if (typeof(Vector3).IsAssignableFrom(type))
            {
                return(EditorGUILayout.Vector3Field(content, (Vector3)value, options));
            }
            else if (typeof(Vector4).IsAssignableFrom(type))
            {
                return(EditorGUILayout.Vector4Field(content.text, (Vector4)value, options));
            }
            else if (typeof(Quaternion).IsAssignableFrom(type))
            {
                return(Vector4ToQuaternion(EditorGUILayout.Vector4Field(content.text, QuaternionToVector4((Quaternion)value), options)));
            }
            else if (typeof(Rect).IsAssignableFrom(type))
            {
                return(EditorGUILayout.RectField(content, (Rect)value, options));
            }
            else if (typeof(Color).IsAssignableFrom(type))
            {
                return(EditorGUILayout.ColorField(content, (Color)value, options));
            }
            else if (typeof(LayerMask).IsAssignableFrom(type))
            {
                object result = EditorGUILayout.LayerField(content, (LayerMask)value, options);
                GUILayout.EndVertical();
                return(result);
            }
            else if (typeof(Enum).IsAssignableFrom(type))
            {
                return(EditorGUILayout.EnumPopup(content, (Enum)value, options));
            }
            else if (typeof(Type).IsAssignableFrom(type))
            {
                current_id = GUIUtility.GetControlID(FocusType.Passive);

                if (value == null)
                {
                    if (!content.text.IsNullOrEmpty())
                    {
                        GUILayout.BeginHorizontal(options);
                        GUILayout.Label(content, options);
                    }
                    if (GUILayout.Button("(Empty Type)", styles.type_button, options))
                    {
                        cached_id = current_id;
                        ShowContentTypeMenu();
                    }
                    if (!content.text.IsNullOrEmpty())
                    {
                        GUILayout.EndHorizontal();
                    }
                }
                else
                {
                    Type       value_cast = (Type)value;
                    GUIContent b_content  = new GUIContent(value_cast.GetTypeName(false, true), GUIReferrer.GetTypeIcon(value_cast));
                    if (!content.text.IsNullOrEmpty())
                    {
                        GUILayout.BeginHorizontal(options);
                        GUILayout.Label(content, options);
                    }
                    if (GUILayout.Button(b_content, styles.type_button, options))
                    {
                        cached_id = current_id;
                        ShowContentTypeMenu();
                    }
                    if (!content.text.IsNullOrEmpty())
                    {
                        GUILayout.EndHorizontal();
                    }
                }
                if (cached_value == null)
                {
                    return(value);
                }
                else
                {
                    if (current_id == cached_id)
                    {
                        object result = cached_value;
                        cached_value = null;
                        return(result);
                    }
                    else
                    {
                        return(value);
                    }
                }
            }
            else if (typeof(UnityObject).IsAssignableFrom(type))
            {
                return(EditorGUILayout.ObjectField(content, (UnityObject)value, type, true, options));
            }
            else if (typeof(Array).IsAssignableFrom(type))
            {
                Array array      = (Array)value;
                Type  array_type = type.GetElementType();
                if (array == null)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label(content, options);
                    GUILayout.Label("[NULL]", options);
                    GUILayout.EndHorizontal();
                    if (GUILayout.Button("Create Array"))
                    {
                        array = Array.CreateInstance(array_type, 1);
                    }
                }
                else
                {
                    bool f;
                    if (!foldouts.TryGetValue(value, out f))
                    {
                        foldouts[value] = true;
                    }
                    GUILayout.BeginVertical("HelpBox", GUILayout.MinHeight(10.0f));
                    {
                        GUILayout.BeginHorizontal();
                        {
                            GUILayout.Space(10.0f);
                            GUILayout.BeginVertical();
                            {
                                GUILayout.BeginHorizontal();
                                if (array.Length > 0)
                                {
                                    foldouts[value] = EditorGUILayout.Foldout(foldouts[value], new GUIContent(string.Format("{0}: [{1}]", content.text, array.Length), content.image, content.tooltip));
                                }
                                else
                                {
                                    GUILayout.Label(new GUIContent(string.Format("{0}: [0]", content.text), content.image, content.tooltip), options);
                                }

                                GUILayout.FlexibleSpace();
                                if (GUILayout.Button("+", "minibutton"))
                                {
                                    Extensions.Resize(ref array, array_type, array.Length + 1);
                                }
                                if (GUILayout.Button("x", "minibutton"))
                                {
                                    if (array.Length > 0)
                                    {
                                        Extensions.Resize(ref array, array_type, array.Length - 1);
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                                GUILayout.EndHorizontal();
                                if (foldouts[value])
                                {
                                    for (int id = 0; id < array.Length; id++)
                                    {
                                        array.SetValue(AnyField(array.GetValue(id), array_type, new GUIContent(string.Format("Element {0}", id)), options), id);
                                    }
                                }
                            }
                            GUILayout.EndVertical();
                        }
                        GUILayout.EndHorizontal();
                    }
                    GUILayout.EndVertical();
                }
                return(array);
            }
            else if (typeof(IList).IsAssignableFrom(type) && !typeof(Array).IsAssignableFrom(type))
            {
                IList  collection    = (IList)value;
                Type[] list_gen_type = type.GetGenericArguments();
                // is IList
                if (list_gen_type.Length == 0)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label(content);
                    GUILayout.FlexibleSpace();
                    if (collection == null)
                    {
                        GUILayout.Label("[NULL]");
                    }
                    else
                    {
                        GUILayout.Label(string.Format("[{0}]", collection.Count));
                    }
                    GUILayout.EndHorizontal();
                    return(value);
                }
                // is List<T>
                else
                {
                    Type list_type = list_gen_type[0];

                    if (collection == null)
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.Label(content, options);
                        GUILayout.Label("[NULL]", options);
                        GUILayout.EndHorizontal();
                        if (GUILayout.Button("Create List"))
                        {
                            Type gen_list_type       = typeof(List <>);
                            Type constructedListType = gen_list_type.MakeGenericType(list_type);
                            collection = (IList)Activator.CreateInstance(constructedListType);
                        }
                    }
                    else
                    {
                        bool f;
                        if (!foldouts.TryGetValue(value, out f))
                        {
                            foldouts[value] = true;
                        }
                        GUILayout.BeginVertical("HelpBox", GUILayout.MinHeight(10.0f));
                        {
                            GUILayout.BeginHorizontal();
                            {
                                GUILayout.Space(10.0f);
                                GUILayout.BeginVertical();
                                {
                                    GUILayout.BeginHorizontal();
                                    if (collection.Count > 0)
                                    {
                                        foldouts[value] = EditorGUILayout.Foldout(foldouts[value], new GUIContent(string.Format("{0}: [{1}]", content.text, collection.Count), content.image, content.tooltip));
                                    }
                                    else
                                    {
                                        GUILayout.Label(new GUIContent(string.Format("{0}: [0]", content.text), content.image, content.tooltip), options);
                                    }

                                    GUILayout.FlexibleSpace();
                                    if (GUILayout.Button("+", "minibutton"))
                                    {
                                        if (list_type.IsAbstract || typeof(UnityObject).IsAssignableFrom(list_type))
                                        {
                                            collection.Add(null);
                                        }
                                        else
                                        {
                                            collection.Add(Activator.CreateInstance(list_type));
                                        }
                                    }
                                    if (GUILayout.Button("x", "minibutton"))
                                    {
                                        if (collection.Count > 0)
                                        {
                                            collection.RemoveAt(collection.Count - 1);
                                        }
                                        else
                                        {
                                            return(null);
                                        }
                                    }
                                    GUILayout.EndHorizontal();
                                    if (foldouts[value])
                                    {
                                        for (int id = 0; id < collection.Count; id++)
                                        {
                                            collection[id] = AnyField(collection[id], list_type, new GUIContent(string.Format("Element {0}", id)), options);
                                        }
                                    }
                                }
                                GUILayout.EndVertical();
                            }
                            GUILayout.EndHorizontal();
                        }
                        GUILayout.EndVertical();
                    }
                }
                return(collection);
            }
            GUILayout.Label(string.Format("ERROR 404. {0} Field Not Found!", type.GetTypeName(true)));
            return(value);
        }
Example #3
0
        void GenerateScript()
        {
            code_list = new Dictionary <string, string>();
            Type base_type = ReflectionUtils.GetTypeByName(type_name);

            foreach (MethodInfo method in base_type.GetMethods(METHOD_BIND_FLAGS))
            {
                if (method_name.IsNullOrEmpty() || method.Name.ToLower().Contains(method_name.ToLower()) || method.Name.ToLower() == method_name.ToLower())
                {
                    bool   is_value_node = method.ReturnType != typeof(void);
                    string node_type     = "NODETYPE";
                    string node_name     = "NODENAME";
                    string icon          = GUIReferrer.GetTypeIconName(base_type);
                    string title         = "NODETITLE";
                    string subtitle      = "NODESUBTITLE";
                    string display_name  = "NODESUBTITLE";
                    string method_invoke = "//METHOD NOT IMPLEMENTED";

                    string variables = "//#VARIABLES#";
                    string ports     = "//#REGISTERPORTS#";

                    if (is_value_node)
                    {
                        code = VALUE_NODE_FORMAT;

                        node_type = base_type.GetTypeName();
                        if (base_type.GetGenericArguments().Length > 0)
                        {
                            node_name = string.Format("{0}{1}<{2}>", node_type, method.Name.NicifyPropertyName(), string.Join(", ", base_type.GetGenericArguments().Select(a => a.GetTypeName(true, true)).ToArray()));
                        }
                        else
                        {
                            node_name = string.Format("{0}{1}", node_type, method.Name.NicifyPropertyName());
                        }
                        code = code.Replace("#RETURNTYPE#", method.ReturnType.GetTypeName(true, true));

                        ParameterInfo[] parameters  = method.GetParameters();
                        string[]        param_names = new string[parameters.Length];
                        if (parameters.Length > 0)
                        {
                            int id = 0;
                            foreach (ParameterInfo parameter in parameters)
                            {
                                param_names[id++] = parameter.Name + ".value";
                                string variable = VARIABLE_FORMAT.Replace("#PARAMETERNAME#", parameter.Name);
                                variable   = variable.Replace("#PARAMETERTYPE#", parameter.ParameterType.GetTypeName(true, true));
                                variables += Environment.NewLine + "\t" + variable;

                                string port = REGISTER_PORT_FORMAT.Replace("#PARAMETERNAME#", parameter.Name);
                                port   = port.Replace("#PARAMETERDISPLAYNAME#", parameter.Name.AddSpacesToSentence());
                                port   = port.Replace("#PARAMETERTYPE#", parameter.ParameterType.GetTypeName(true, true));
                                ports += Environment.NewLine + "\t\t" + port;
                            }
                        }

                        title        = method.Name;
                        subtitle     = node_type;
                        display_name = method.GetSignName();
                        if (method.IsStatic)
                        {
                            if (method.IsSpecialName)
                            {
                                method_invoke = string.Format("{0}.{1}", method.ReflectedType.GetTypeName(true), method.Name);
                                method_invoke = method_invoke.NicifyPropertyName();
                            }
                            else
                            {
                                method_invoke = string.Format("{0}.{1}({2})", method.ReflectedType.GetTypeName(true), method.Name, string.Join(",", param_names));
                            }
                        }
                        else
                        {
                            string target = VARIABLE_FORMAT.Replace("#PARAMETERNAME#", "m_target");
                            target     = target.Replace("#PARAMETERTYPE#", base_type.GetTypeName(true, true));
                            variables += Environment.NewLine + "\t" + target;

                            string port = REGISTER_PORT_FORMAT.Replace("#PARAMETERNAME#", "this.m_target");
                            port   = port.Replace("#PARAMETERDISPLAYNAME#", "target");
                            port   = port.Replace("#PARAMETERTYPE#", base_type.GetTypeName(true, true));
                            ports += Environment.NewLine + "\t\t" + port;

                            if (method.IsSpecialName)
                            {
                                method_invoke = string.Format("{0}.{1}", "this.m_target.value", method.Name);
                                method_invoke = method_invoke.Replace("get_", string.Empty);
                            }
                            else
                            {
                                method_invoke = string.Format("{0}.{1}({2})", "this.m_target.value", method.Name, string.Join(",", param_names));
                            }
                        }
                    }

                    code = code.Replace("#NODETYPE#", node_type);
                    code = code.Replace("#NODENAME#", node_name + "NSG");
                    code = code.Replace("#NODEDISPLAYNAME#", display_name);
                    code = code.Replace("#NODEICON#", icon);
                    code = code.Replace("#NODETITLE#", title);
                    code = code.Replace("#NODESUBTITLE#", subtitle);

                    code = code.Replace("//#VARIABLES#", variables);
                    code = code.Replace("//#REGISTERPORTS#", ports);

                    code = code.Replace("#METHODINVOKE#", method_invoke);

                    code_list[node_name] = code;
                }
            }
        }