/// <summary>
 /// Gets the attribute.
 /// </summary>
 /// <returns>The attribute.</returns>
 /// <param name="type">Type.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static T GetAttribute <T>(this Type type)
 {
     object[] objArray = UnityEditorUtility.GetCustomAttributes(type);
     for (int i = 0; i < (int)objArray.Length; i++)
     {
         if (objArray[i].GetType() == typeof(T) || objArray[i].GetType().IsSubclassOf(typeof(T)))
         {
             return((T)objArray[i]);
         }
     }
     return(default(T));
 }
Esempio n. 2
0
        protected virtual void OnGUI()
        {
            DoApplyToPrefab();
            this.m_ScrollPosition = EditorGUILayout.BeginScrollView(this.m_ScrollPosition);
            for (int i = 0; i < this.m_Targets.Length; i++)
            {
                UnityEngine.Object target = this.m_Targets[i];
                Editor             editor = this.m_Editors[i];

                if (UnityEditorUtility.Titlebar(target, GetContextMenu(target)))
                {
                    EditorGUI.indentLevel += 1;
                    editor.OnInspectorGUI();
                    EditorGUI.indentLevel -= 1;
                }
            }
            EditorGUILayout.EndScrollView();
            GUILayout.FlexibleSpace();
            DoAddButton();
            GUILayout.Space(10f);
            DoCopyPaste();
        }
        protected virtual void SelectDatabase()
        {
            string searchString = "Search...";

            T[] databases = UnityEditorUtility.FindAssets <T>();
            UtilityInstanceWindow.ShowWindow("Select Settings", delegate() {
                searchString = UnityEditorUtility.SearchField(searchString);

                for (int i = 0; i < databases.Length; i++)
                {
                    if (!string.IsNullOrEmpty(searchString) && !searchString.Equals("Search...") && !databases[i].name.Contains(searchString))
                    {
                        continue;
                    }
                    GUIStyle style = new GUIStyle("button");
                    style.wordWrap = true;
                    if (GUILayout.Button(AssetDatabase.GetAssetPath(databases[i]), style))
                    {
                        database = databases[i];
                        ResetChildEditors();
                        Show();
                        UtilityInstanceWindow.CloseWindow();
                    }
                }
                GUILayout.FlexibleSpace();
                Color color         = GUI.backgroundColor;
                GUI.backgroundColor = Color.green;
                if (GUILayout.Button("Create"))
                {
                    T db = AssetCreator.CreateAsset <T>(true);
                    if (db != null)
                    {
                        ArrayUtility.Add <T>(ref databases, db);
                    }
                }
                GUI.backgroundColor = color;
            });
        }
Esempio n. 4
0
        protected virtual void CreateScript(string scriptName)
        {
            scriptName = scriptName.Replace(" ", "_");
            scriptName = scriptName.Replace("-", "_");
            string path = "Assets/" + scriptName + ".cs";

            if (File.Exists(path) == false)
            {
                using (StreamWriter outfile = new StreamWriter(path))
                {
                    MethodInfo[] methods = elementType.GetAllMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    methods = methods.Where(x => x.IsAbstract).ToArray();

                    outfile.WriteLine("using UnityEngine;");
                    outfile.WriteLine("using System.Collections;");
                    outfile.WriteLine("using " + elementType.Namespace + ";");
                    outfile.WriteLine("");
                    if (!typeof(Component).IsAssignableFrom(elementType))
                    {
                        outfile.WriteLine("[System.Serializable]");
                    }
                    outfile.WriteLine("public class " + scriptName + " : " + elementType.Name + "{");
                    for (int i = 0; i < methods.Length; i++)
                    {
                        MethodInfo      method          = methods[i];
                        ParameterInfo[] parameters      = method.GetParameters();
                        string          parameterString = string.Empty;
                        for (int j = 0; j < parameters.Length; j++)
                        {
                            string typeName      = parameters[j].ParameterType.Name;
                            string parameterName = string.Empty;
                            if (Char.IsLower(typeName, 0))
                            {
                                parameterName = "_" + typeName;
                            }
                            else
                            {
                                parameterName = char.ToLowerInvariant(typeName[0]) + typeName.Substring(1);
                            }
                            parameterString += ", " + typeName + " " + parameterName;
                        }

                        if (!string.IsNullOrEmpty(parameterString))
                        {
                            parameterString = parameterString.Substring(1);
                        }

                        outfile.WriteLine("\t" + (method.IsPublic?"public":"protected") + " override " + UnityEditorUtility.CovertToAliasString(method.ReturnType) + " " + method.Name + "(" + parameterString + ") {");

                        if (method.ReturnType == typeof(string))
                        {
                            outfile.WriteLine("\t\treturn string.Empty;");
                        }
                        else if (method.ReturnType == typeof(bool))
                        {
                            outfile.WriteLine("\t\treturn true;");
                        }
                        else if (method.ReturnType == typeof(Vector2))
                        {
                            outfile.WriteLine("\t\treturn Vector2.zero;");
                        }
                        else if (method.ReturnType == typeof(Vector3))
                        {
                            outfile.WriteLine("\t\treturn Vector3.zero;");
                        }
                        else if (!method.ReturnType.IsValueType || method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            outfile.WriteLine("\t\treturn null;");
                        }
                        else if (UnityUtility.IsInteger(method.ReturnType))
                        {
                            outfile.WriteLine("\t\treturn 0;");
                        }
                        else if (UnityUtility.IsFloat(method.ReturnType))
                        {
                            outfile.WriteLine("\t\treturn 0.0f;");
                        }
                        else if (method.ReturnType.IsEnum)
                        {
                            outfile.WriteLine("\t\treturn " + method.ReturnType.Name + "." + Enum.GetNames(method.ReturnType)[0] + ";");
                        }

                        outfile.WriteLine("\t}");
                        outfile.WriteLine("");
                    }
                    outfile.WriteLine("}");
                }
            }
            AssetDatabase.Refresh();
            EditorPrefs.SetString("NewScriptToCreate", scriptName);
            EditorPrefs.SetInt("AssetWindowID", GetInstanceID());
        }
 protected virtual void DoSearchGUI()
 {
     searchString = UnityEditorUtility.SearchField(searchString, GUILayout.Width(sidebarRect.width - 20));
 }