using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using UnityEditor; using UnityEngine; namespace MetaCity.BundleKit.Editor { public class BuildMainWindow : MetacityBuildWindow { [SerializeField] private BuildMainConfig mConfig; private SpawnPositionConfig mSpawnPositionConfig; private PortalConfig mPortalConfig; private Vector2 mScrollPosition; private int mTab = 1; [MenuItem("Metacity/BuildProjectWindow")] public static void OpenBuildMainWindow() { if (AssetDatabase.LoadAssetAtPath(Constants.SpawnPositionConfigPath) == null) { SpawnPositionConfig asset = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(asset, Constants.SpawnPositionConfigPath); AssetDatabase.SaveAssets(); } if (AssetDatabase.LoadAssetAtPath(Constants.PortalConfigPath) == null) { PortalConfig asset = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(asset, Constants.PortalConfigPath); AssetDatabase.SaveAssets(); } GetWindow("Metacity Build Project Window"); } protected sealed override void OnEnable() { base.OnEnable(); if (File.Exists(Constants.BuildConfigPath)) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Constants.BuildConfigPath, FileMode.Open); if (bf.Deserialize(file) is BuildMainConfig data) mConfig = data; file.Close(); } mSpawnPositionConfig = AssetDatabase.LoadAssetAtPath(Constants.SpawnPositionConfigPath); mPortalConfig=AssetDatabase.LoadAssetAtPath(Constants.PortalConfigPath); if (mConfig == null) { mConfig = new BuildMainConfig(); } RefreshConfig(); } private void RefreshConfig() { var allCandidateScenes = SceneCollector.CollectSceneEntries(); var allCandidateAssets = PrefabCollector.CollectPrefabEntries(); mConfig.PrepareSave(allCandidateScenes, allCandidateAssets); } private void SaveConfig() { RefreshConfig(); BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Constants.BuildConfigPath); bf.Serialize(file, mConfig); file.Close(); } private void OnDisable() { SaveConfig(); } private UnityEditor.Editor mSpawnPositionEditor = null; private void ShowSpawnPositionEditor() { if (mSpawnPositionEditor == null) { mSpawnPositionEditor = UnityEditor.Editor.CreateEditor(mSpawnPositionConfig); } EditorGUI.BeginChangeCheck(); EditorGUILayout.HelpBox("\n" + "Register Spawn Points of your space.\n" + "\n" , MessageType.Info); mSpawnPositionEditor.OnInspectorGUI(); if (EditorGUI.EndChangeCheck()) { AssetDatabase.SaveAssetIfDirty(mSpawnPositionConfig); } } private UnityEditor.Editor mPortalEditor=null; private void ShowPortalEditor() { if (mPortalEditor == null) { mPortalEditor = UnityEditor.Editor.CreateEditor(mPortalConfig); } EditorGUI.BeginChangeCheck(); EditorGUILayout.HelpBox("\n" + "Register Portals of your space.\n" + "\n" , MessageType.Info); mPortalEditor.OnInspectorGUI(); if (EditorGUI.EndChangeCheck()) { AssetDatabase.SaveAssetIfDirty(mPortalEditor); } } private void ShowBuildConfigEditor() { mScrollPosition = EditorGUILayout.BeginScrollView(mScrollPosition); GUILayout.BeginVertical(); GUILayout.Label(new GUIContent("Bundles To Build:")); EditorGUI.indentLevel = 1; GUILayout.Label(new GUIContent("Scenes:")); EditorGUI.indentLevel = 2; foreach (var sceneEntry in mConfig.allSceneBundles) { var oldState = !mConfig.mCustomize || sceneEntry.needBuild; GUI.enabled = mConfig.mCustomize; var newState = EditorGUILayout.ToggleLeft( sceneEntry.bundleName, oldState); GUI.enabled=true; if (newState != oldState) { mConfig.ToggleSceneBundleConfig(sceneEntry.bundleName); } } EditorGUILayout.Space(); EditorGUI.indentLevel = 1; GUILayout.Label(new GUIContent("Artifacts:")); EditorGUI.indentLevel = 2; foreach (var prefabEntry in mConfig.allAssetBundles) { var oldState = !mConfig.mCustomize || prefabEntry.needBuild; GUI.enabled = mConfig.mCustomize; var newState = EditorGUILayout.ToggleLeft( prefabEntry.bundleName, oldState); GUI.enabled=true; if (newState != oldState) { mConfig.ToggleNeedBuildArtifact(prefabEntry.bundleName); } } EditorGUI.indentLevel = 0; GUI.enabled = true; mConfig.mCustomize = EditorGUILayout.ToggleLeft("Enable Customize", mConfig.mCustomize); var mode=(PlayMode)EditorGUILayout.EnumPopup("Play Mode",mConfig.mPlayMode); if(mode!=mConfig.mPlayMode) { mConfig.SetPlayMode(mode); } EditorGUILayout.Space(); var weburl = (WebUrl)EditorGUILayout.EnumPopup("Web Url", mConfig.mWebUrl); if (weburl!=mConfig.mWebUrl) { mConfig.SetWebUrl(weburl); } EditorGUILayout.Space(); if (GUILayout.Button("Refresh")) { RefreshConfig(); } if (GUILayout.Button("Build") ) { SaveConfig(); var skipAssets = new List(); if (mConfig.mCustomize) { skipAssets.AddRange(mConfig.skipAssets); } EditorApplication.delayCall += () => { var buildCatalogs = BuildEntry.DoBuild(mConfig.mPlayMode, mConfig.allSceneBundles, skipAssets); // Open Web and Folder if build success if (buildCatalogs != null) { // Open Web string urlUpload = MetacityClient.GetUploadUrl(weburl); Application.OpenURL(urlUpload); // Open ExportBundles Folder string path = Constants.BundleFolderPath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } EditorUtility.RevealInFinder(path); } else { Debug.Log("Build failed."); } }; } m_showPublish = EditorGUILayout.BeginFoldoutHeaderGroup(m_showPublish, "Publish"); if (m_showPublish) base.OnGUI(); EditorGUILayout.EndFoldoutHeaderGroup(); GUILayout.EndVertical(); EditorGUILayout.EndScrollView(); } bool m_showPublish = false; protected sealed override void OnGUI() { mTab = GUILayout.Toolbar (mTab, new string[] {"Portal","SpawnPosition", "Build"}); switch (mTab) { case 0: ShowPortalEditor(); break; case 1: ShowSpawnPositionEditor(); break; case 2: ShowBuildConfigEditor(); break; } } } }