99 lines
2.9 KiB
C#
99 lines
2.9 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using UnityEditor;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public class AppBuildTab : EditorWindowTabBase
|
|||
|
|
{
|
|||
|
|
// 整包构建配置
|
|||
|
|
private BuildTarget _appBuildTarget = BuildTarget.StandaloneWindows64;
|
|||
|
|
private string _appOutputPath = "Builds/App";
|
|||
|
|
private SceneAsset _entryScene;
|
|||
|
|
private bool _developmentBuild;
|
|||
|
|
|
|||
|
|
internal override void OnGUI()
|
|||
|
|
{
|
|||
|
|
base.OnGUI();
|
|||
|
|
DrawAppBuildTab();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private void DrawAppBuildTab()
|
|||
|
|
{
|
|||
|
|
EditorGUILayout.Space();
|
|||
|
|
EditorGUILayout.LabelField("整包构建配置", EditorStyles.boldLabel);
|
|||
|
|
|
|||
|
|
DrawBuildTarget(ref _appBuildTarget);
|
|||
|
|
DrawPathSelection("输出路径:", ref _appOutputPath);
|
|||
|
|
_developmentBuild = EditorGUILayout.Toggle("开发模式:", _developmentBuild);
|
|||
|
|
|
|||
|
|
EditorGUILayout.Space();
|
|||
|
|
EditorGUILayout.LabelField("包含场景:", EditorStyles.boldLabel);
|
|||
|
|
DrawSceneList();
|
|||
|
|
|
|||
|
|
EditorGUILayout.Space();
|
|||
|
|
if (GUILayout.Button("构建应用程序", GUILayout.Height(30)))
|
|||
|
|
{
|
|||
|
|
BuildApplication();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void DrawBuildTarget(ref BuildTarget target)
|
|||
|
|
{
|
|||
|
|
target = (BuildTarget)EditorGUILayout.EnumPopup("目标平台:", target);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void DrawSceneList()
|
|||
|
|
{
|
|||
|
|
_entryScene = (SceneAsset)EditorGUILayout.ObjectField("启动场景", _entryScene, typeof(SceneAsset), false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void BuildApplication()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var options = new BuildPlayerOptions
|
|||
|
|
{
|
|||
|
|
scenes = new[] { AssetDatabase.GetAssetPath(_entryScene) },
|
|||
|
|
locationPathName = Path.Combine(_appOutputPath, GetExecutableName()),
|
|||
|
|
target = _appBuildTarget,
|
|||
|
|
options = _developmentBuild ? BuildOptions.Development : BuildOptions.None
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var report = BuildPipeline.BuildPlayer(options);
|
|||
|
|
HandleBuildReport(report);
|
|||
|
|
}
|
|||
|
|
catch (Exception e)
|
|||
|
|
{
|
|||
|
|
EditorUtility.DisplayDialog("错误", $"构建过程中发生异常: {e.Message}", "确定");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void HandleBuildReport(UnityEditor.Build.Reporting.BuildReport report)
|
|||
|
|
{
|
|||
|
|
if (report.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded)
|
|||
|
|
{
|
|||
|
|
EditorUtility.DisplayDialog("构建成功",
|
|||
|
|
$"应用程序构建完成!\n输出大小: {report.summary.totalSize / 1024 / 1024}MB", "确定");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
EditorUtility.DisplayDialog("构建失败",
|
|||
|
|
$"错误信息: {report.summary}", "确定");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GetExecutableName()
|
|||
|
|
{
|
|||
|
|
return _appBuildTarget switch
|
|||
|
|
{
|
|||
|
|
BuildTarget.StandaloneWindows64 => "Game.exe",
|
|||
|
|
BuildTarget.StandaloneOSX => "Game.app",
|
|||
|
|
BuildTarget.Android => "Game.apk",
|
|||
|
|
_ => "Game"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|