void OnGUI()
        {
            EditorGUILayout.HelpBox("Here you can specify frequently used types for your game for easier access wherever you need to select a type, like for example when you create a new blackboard variable or using any refelection based actions. Furthermore, it is essential when working with AOT platforms like iOS or WebGL, that you generate an AOT Classes and link.xml files with the relevant button bellow.\nTo add types in the list quicker, you can also Drag&Drop an object, or a Script file in this editor window, or seach it bellow.", MessageType.Info);



            GUILayout.BeginHorizontal();
            search = EditorGUILayout.TextField(search, (GUIStyle)"ToolbarSeachTextField");
            if (GUILayout.Button("", (GUIStyle)"ToolbarSeachCancelButton"))
            {
                search = null;
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.EndHorizontal();

            if (search != null && search.Length > 2)
            {
                GUILayout.Label(string.Format("<b>{0}</b>", "Showing Search Results. Click the plus button to add the type."));
                GUILayout.Space(5);
                scrollPos = GUILayout.BeginScrollView(scrollPos);
                foreach (var type in EditorUtils.GetAssemblyTypes(typeof(object)))
                {
                    if (type.Name.ToUpper().Contains(search.ToUpper()))
                    {
                        GUILayout.BeginHorizontal("box");
                        if (GUILayout.Button("+", GUILayout.Width(20)))
                        {
                            AddType(type);
                        }
                        EditorGUILayout.LabelField(type.Name, type.Namespace);
                        GUILayout.EndHorizontal();
                    }
                }
                GUILayout.EndScrollView();

                return;
            }



            if (GUILayout.Button("Add New Type"))
            {
                GenericMenu.MenuFunction2 Selected = delegate(object o){
                    if (o is System.Type)
                    {
                        AddType((System.Type)o);
                    }
                    if (o is string)                      //namespace
                    {
                        foreach (var type in EditorUtils.GetAssemblyTypes(typeof(object)))
                        {
                            if (type.Namespace == (string)o)
                            {
                                AddType(type);
                            }
                        }
                    }
                };

                var menu = new UnityEditor.GenericMenu();
                menu.AddItem(new GUIContent("Classes/System/Object"), false, Selected, typeof(object));
                foreach (var t in EditorUtils.GetAssemblyTypes(typeof(object)))
                {
                    var friendlyName = (string.IsNullOrEmpty(t.Namespace)? "No Namespace/" : t.Namespace.Replace(".", "/") + "/") + t.FriendlyName();
                    var category     = "Classes/";
                    if (t.IsInterface)
                    {
                        category = "Interfaces/";
                    }
                    if (t.IsEnum)
                    {
                        category = "Enumerations/";
                    }
                    menu.AddItem(new GUIContent(category + friendlyName), false, Selected, t);

                    if (t.Namespace != null)
                    {
                        var ns = t.Namespace.Replace(".", "/");
                        menu.AddItem(new GUIContent("Namespaces/" + ns), false, Selected, t.Namespace);
                    }
                }
                menu.ShowAsContext();
                Event.current.Use();
            }

#if !UNITY_WEBPLAYER
            if (GUILayout.Button("Generate AOTClasses.cs and link.xml Files"))
            {
                if (EditorUtility.DisplayDialog("Generate AOT Classes", "A script relevant to AOT compatibility for certain platforms will now be generated.", "OK"))
                {
                    var path = EditorUtility.SaveFilePanelInProject("AOT Classes File", "AOTClasses", "cs", "");
                    if (!string.IsNullOrEmpty(path))
                    {
                        AOTClassesGenerator.GenerateAOTClasses(path);
                    }
                }

                if (EditorUtility.DisplayDialog("Generate link.xml File", "A file relevant to 'code stripping' for platforms that have code stripping enabled will now be generated.", "OK"))
                {
                    var path = EditorUtility.SaveFilePanelInProject("AOT link.xml", "link", "xml", "");
                    if (!string.IsNullOrEmpty(path))
                    {
                        AOTClassesGenerator.GenerateLinkXML(path);
                    }
                }

                AssetDatabase.Refresh();
            }

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("RESET DEFAULTS"))
            {
                if (EditorUtility.DisplayDialog("Reset Preferred Types", "Are you sure?", "Yes", "NO!"))
                {
                    UserTypePrefs.ResetTypeConfiguration();
                    typeList = UserTypePrefs.GetPreferedTypesList(typeof(object));
                    Save();
                }
            }

            if (GUILayout.Button("Save Preset"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save Types Preset", "", "typePrefs", "");
                if (!string.IsNullOrEmpty(path))
                {
                    System.IO.File.WriteAllText(path, JSONSerializer.Serialize(typeof(List <System.Type>), typeList, true));
                    AssetDatabase.Refresh();
                }
            }

            if (GUILayout.Button("Load Preset"))
            {
                var path = EditorUtility.OpenFilePanel("Load Types Preset", "Assets", "typePrefs");
                if (!string.IsNullOrEmpty(path))
                {
                    var json = System.IO.File.ReadAllText(path);
                    typeList = JSONSerializer.Deserialize <List <System.Type> >(json);
                    Save();
                }
            }

            GUILayout.EndHorizontal();
#endif

            GUILayout.Space(5);

            scrollPos = GUILayout.BeginScrollView(scrollPos);

            for (int i = 0; i < typeList.Count; i++)
            {
                GUILayout.BeginHorizontal("box");
                EditorGUILayout.LabelField(typeList[i].Name, typeList[i].Namespace);
                if (GUILayout.Button("X", GUILayout.Width(18)))
                {
                    RemoveType(typeList[i]);
                }
                GUILayout.EndHorizontal();
            }

            GUILayout.EndScrollView();

            AcceptDrops();

            Repaint();
        }
示例#2
0
 void OnEnable()
 {
     titleContent = new GUIContent("Preferred Types");
     typeList     = UserTypePrefs.GetPreferedTypesList();
     alltypes     = EditorUtils.GetAssemblyTypes(typeof(object)).Where(t => !t.IsGenericType && !t.IsGenericTypeDefinition).ToList();
 }
        public static void GenerateAOTClasses(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var spoofTypes = defaultSpoofTypes;

            spoofTypes.AddRange(UserTypePrefs.GetPreferedTypesList(typeof(object), true).Where(t => t.IsValueType && !spoofTypes.Contains(t)));
            var types = EditorUtils.GetAssemblyTypes(typeof(object)).Where(t => t.RTGetAttribute <SpoofAOTAttribute>(true) != null).ToList();

            var nClasses = 0;
            var nMethods = 0;

            var sb = new StringBuilder();

            sb.AppendLine("#pragma warning disable 0219, 0168, 0612");
            sb.AppendLine("namespace ParadoxNotion.Internal{");
            sb.AppendLine();
            sb.AppendLine("	//Auto generated classes for AOT support, where using undeclared generic classes with value types is limited. These are not actualy used but rather just declared for the compiler");
            sb.AppendLine("	class AOTDummy{");
            sb.AppendLine();
            sb.AppendLine("		object o = null;");



            foreach (var type in types)
            {
                if (!type.IsAbstract && type.IsGenericTypeDefinition && type.GetGenericArguments().Length == 1)
                {
                    var constrains = type.GetGenericArguments()[0].GetGenericParameterConstraints();
                    if (constrains.Length == 0 || constrains[0].IsValueType)
                    {
                        if (typeof(Delegate).IsAssignableFrom(type))
                        {
                            nClasses++;
                            sb.AppendLine(string.Format("		void {0}()", type.FriendlyName(true).Replace(".", "_").Replace("<T>", "_Delegate")) + "{");
                            foreach (var spoofType in spoofTypes)
                            {
                                var a = type.FriendlyName(true).Replace("<T>", "<" + spoofType.FullName + ">").Replace("+", ".");
                                var b = "_" + type.FriendlyName().Replace("<T>", "_" + spoofType.FullName.Replace(".", "_").Replace("+", "_"));
                                sb.AppendLine(string.Format("			{0} {1} = null;", a, b));
                            }
                            sb.AppendLine("		}");
                        }
                        else
                        {
                            foreach (var spoofType in spoofTypes)
                            {
                                nClasses++;
                                var a = type.FriendlyName(true).Replace(".", "_").Replace("<T>", "_" + spoofType.FullName.Replace(".", "_").Replace("+", "_"));
                                var b = type.FriendlyName(true).Replace("<T>", "<" + spoofType.FullName + ">").Replace("+", ".") + "{}";
                                sb.AppendLine(string.Format("		class {0} : {1}", a, b));
                            }
                        }

                        sb.AppendLine();
                    }
                }
            }

            foreach (var type in types)
            {
                var index = 0;
                foreach (var method in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly))
                {
                    if (method.IsGenericMethodDefinition && method.GetGenericArguments().Length == 1)
                    {
                        var constrains = method.GetGenericArguments()[0].GetGenericParameterConstraints();
                        if (constrains.Length == 0 || constrains[0].IsValueType)
                        {
                            index++;

                            var decType = method.DeclaringType;
                            var varName = "_" + decType.FullName.Replace(".", "_");
                            sb.AppendLine(string.Format("		void {0}_{1}_{2}()", decType.FullName.Replace(".", "_"), method.Name, index) + " {");
                            if (!method.IsStatic)
                            {
                                sb.AppendLine(string.Format("			{0} {1} = null;", decType.FullName, varName));
                            }

                            foreach (var spoofType in spoofTypes)
                            {
                                nMethods++;
                                var a            = method.IsStatic? decType.FullName : varName;
                                var b            = method.Name;
                                var c            = spoofType.FullName.Replace("+", ".");
                                var paramsString = "";
                                var parameters   = method.GetParameters();
                                for (var i = 0; i < parameters.Length; i++)
                                {
                                    var parameter = parameters[i];
                                    var toString  = parameter.ParameterType.FullName;
                                    if (parameter.ParameterType.IsGenericParameter)
                                    {
                                        toString = spoofType.FullName;
                                    }
                                    if (parameter.ParameterType.IsGenericType)
                                    {
                                        toString = parameter.ParameterType.FriendlyName(true).Replace("<T>", "<" + spoofType.FullName + ">");
                                        toString = toString.Replace("[[T]]", "");
                                    }
                                    toString      = toString.Replace("+", ".");
                                    paramsString += string.Format("({0})o", toString);
                                    if (i < parameters.Length - 1)
                                    {
                                        paramsString += ", ";
                                    }
                                }
                                var d = paramsString;
                                sb.AppendLine(string.Format("			{0}.{1}<{2}>( {3} );", a, b, c, d));
                            }

                            sb.AppendLine("		}");
                            sb.AppendLine();
                        }
                    }
                }
            }

            //custom stuff
            sb.AppendLine("		void CustomSpoof(){");
            foreach (var spoofType in spoofTypes)
            {
                var sName = spoofType.FullName.Replace("+", ".");
                var fName = spoofType.FullName.Replace(".", "_").Replace("+", "_");
                sb.AppendLine(string.Format("			System.Action<{0}> Action_{1};", sName, fName));
                sb.AppendLine(string.Format("			System.Func<{0}> Func_{1};", sName, fName));
                sb.AppendLine(string.Format("			System.Collections.Generic.IList<{0}> List_{1};", sName, fName));
                sb.AppendLine(string.Format("			System.Collections.Generic.IDictionary<System.String, {0}> Dict_{1};", sName, fName));
            }
            sb.AppendLine("		}");

            sb.AppendLine("	}");
            sb.AppendLine("}");
            sb.AppendLine();
            sb.AppendLine(string.Format("//{0} Classes | {1} Methods", nClasses, nMethods));
            sb.AppendLine("#pragma warning restore 0219, 0168, 0612");

            File.WriteAllText(path, sb.ToString());
        }