您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

308 行
11 KiB

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;
//1 使用//1 和 //2 来记录之后改回来时需要删除的部分
private List<string> Scenes = new List<string>();
private int Scene_number;
//2
private int mTab = 1;
[MenuItem("Metacity/BuildProjectWindow")]
public static void OpenBuildMainWindow()
{
if (AssetDatabase.LoadAssetAtPath<SpawnPositionConfig>(Constants.SpawnPositionConfigPath) == null)
{
SpawnPositionConfig asset = ScriptableObject.CreateInstance<SpawnPositionConfig>();
AssetDatabase.CreateAsset(asset, Constants.SpawnPositionConfigPath);
AssetDatabase.SaveAssets();
}
if (AssetDatabase.LoadAssetAtPath<PortalConfig>(Constants.PortalConfigPath) == null)
{
PortalConfig asset = ScriptableObject.CreateInstance<PortalConfig>();
AssetDatabase.CreateAsset(asset, Constants.PortalConfigPath);
AssetDatabase.SaveAssets();
}
GetWindow<BuildMainWindow>("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<SpawnPositionConfig>(Constants.SpawnPositionConfigPath);
mPortalConfig=AssetDatabase.LoadAssetAtPath<PortalConfig>(Constants.PortalConfigPath);
if (mConfig == null)
{
mConfig = new BuildMainConfig();
}
RefreshConfig();
//1
foreach (var sceneEntry in mConfig.allSceneBundles)
{
Scenes.Add(sceneEntry.bundleName);
}
Scene_number = 0;
foreach (var sceneEntry in mConfig.allSceneBundles)
{
var oldState = !mConfig.mCustomize || sceneEntry.needBuild;
if (oldState)
{
mConfig.ToggleSceneBundleConfig(sceneEntry.bundleName);
}
}
mConfig.ToggleSceneBundleConfig(Scenes[0]);
//2
}
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()
{
BuildTarget platformTarget = EditorUserBuildSettings.activeBuildTarget;
bool isWebgl = platformTarget == BuildTarget.WebGL;
if (!isWebgl)
{
SwitchToWebglGUI();
return;
}
mScrollPosition = EditorGUILayout.BeginScrollView(mScrollPosition);
GUILayout.BeginVertical();
GUILayout.Label(new GUIContent("Bundles To Build:"));
EditorGUI.indentLevel = 1;
GUILayout.Label(new GUIContent("Scenes:"));
EditorGUI.indentLevel = 2;
//TODO 以后逻辑会改回来
/*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);
}
}*/
//1
var newScene_number = EditorGUILayout.Popup("Scene", Scene_number,Scenes.ToArray());
if (newScene_number != Scene_number){
mConfig.ToggleSceneBundleConfig(Scenes[Scene_number]);
mConfig.ToggleSceneBundleConfig(Scenes[newScene_number]);
Scene_number = newScene_number;
}
//2
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(EditorUserBuildSettings.activeBuildTarget != BuildTarget.WebGL)
{
EditorGUILayout.HelpBox(
"请在Build Settings中选择平台为WebGL.\n"
, MessageType.Info);
EditorGUILayout.Space(10);
}
else
{
if (GUILayout.Button("Build"))
{
SaveConfig();
var skipAssets = new List<string>();
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();
}
void SwitchToWebglGUI()
{
GUILayout.Label(new GUIContent("Please Switch to Webgl"));
if (GUILayout.Button("Switch to webgl"))
{
EditorUserBuildSettings.SwitchActiveBuildTargetAsync(BuildTargetGroup.WebGL, BuildTarget.WebGL);
}
}
//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;
}
}
}
}