Exemplo n.º 1
0
        static void UpdateScriptReferences()
        {
            var defaultScriptReferences = CreateInstance <DefaultScriptReferences>();

            var prefabsRoot             = new GameObject(Path.GetFileNameWithoutExtension(k_Path));
            Action <ICollection> create = types =>
            {
                foreach (Type t in types)
                {
                    if (t.IsNested || !typeof(MonoBehaviour).IsAssignableFrom(t))
                    {
                        continue;
                    }

                    if (t.GetCustomAttributes(true).OfType <EditorOnlyWorkspaceAttribute>().Any())
                    {
                        continue;
                    }

                    var mb = (MonoBehaviour)ObjectUtils.CreateGameObjectWithComponent(t, runInEditMode: false);
                    if (mb)
                    {
                        mb.gameObject.hideFlags = HideFlags.None;
                        mb.enabled          = false;
                        mb.transform.parent = prefabsRoot.transform;
                    }
                }
            };

            create(ObjectUtils.GetImplementationsOfInterface(typeof(IEditor)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(IProxy)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(ITool)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(ISystemModule)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(IMainMenu)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(IToolsMenu)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(IAlternateMenu)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(IAction)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(IWorkspace)).ToList());
            create(ObjectUtils.GetImplementationsOfInterface(typeof(IScriptReference)).ToList());

            var directory = Path.GetDirectoryName(k_Path);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

#if UNITY_2018_3_OR_NEWER
            defaultScriptReferences.m_ScriptPrefab = PrefabUtility.SaveAsPrefabAsset(prefabsRoot, Path.ChangeExtension(k_Path, "prefab"));
#else
            defaultScriptReferences.m_ScriptPrefab = PrefabUtility.CreatePrefab(Path.ChangeExtension(k_Path, "prefab"), prefabsRoot);
#endif
            defaultScriptReferences.m_EditingContexts = EditingContextManager.GetEditingContextAssets().ConvertAll(ec => (ScriptableObject)ec);

            AssetDatabase.CreateAsset(defaultScriptReferences, k_Path);

            DestroyImmediate(prefabsRoot);

            AssetDatabase.SaveAssets();
        }