Forest_Client/Forest/Assets/Editor/Build/BuildWindow.cs

186 lines
6.3 KiB
C#

using System;
using UnityEngine;
using UnityEditor;
using Sirenix.Utilities;
using System.Collections;
using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;
using UnityEngine.Serialization;
using System.Collections.Generic;
using Sirenix.OdinInspector.Editor;
using UnityEditor.AddressableAssets.Settings;
public class BuildWindow : OdinEditorWindow
{
private static BuildWindow thisWindow;
private List<string> profileList = new List<string>();
[MenuItem("Tools/打包工具")]
public static void ShowWindow()
{
thisWindow = GetWindow<BuildWindow>();
thisWindow.position = GUIHelper.GetEditorWindowRect().AlignCenter(500, 280);
AddressableBuildBase.BuildParam.Settings = AssetDatabase.LoadAssetAtPath<ScriptableObject>(
"Assets/AddressableAssetsData/AddressableAssetSettings.asset")
as AddressableAssetSettings;
if (AddressableBuildBase.BuildParam.Settings != null)
foreach (var profileName in AddressableBuildBase.BuildParam.Settings.profileSettings.GetAllProfileNames())
{
thisWindow.profileList.Add(profileName);
}
thisWindow.separateGroup = new List<AddressableAssetGroup>();
if (AddressableBuildBase.BuildParam.Settings != null)
foreach (var group in AddressableBuildBase.BuildParam.Settings.groups)
{
thisWindow.separateGroup.Add(group);
}
}
protected override void OnEnable()
{
string buildTargetString = PlayerPrefs.GetString("BuildTarget");
if (Enum.TryParse<BuildTarget>(buildTargetString, out BuildTarget parsedBuildTarget))
{
buildTarget = parsedBuildTarget;
}
profileName = PlayerPrefs.GetString("Profile");
version = PlayerPrefs.GetString("Version");
workSpace = PlayerPrefs.GetString("WorkSpace");
isSeparatePackage = BuildUtils.ConvertBool(PlayerPrefs.GetString("IsSeparatePackage"));
needClearCache = BuildUtils.ConvertBool(PlayerPrefs.GetString("ClearCache"));
ignoreHotUpdateDll = BuildUtils.ConvertBool(PlayerPrefs.GetString("IgnoreHotUpdateDll"));
base.OnEnable();
}
protected override void OnDisable()
{
PlayerPrefs.SetString("BuildTarget", buildTarget.ToString());
PlayerPrefs.SetString("Profile", profileName);
PlayerPrefs.SetString("Version", version);
PlayerPrefs.SetString("WorkSpace", workSpace);
PlayerPrefs.SetString("IsSeparatePackage", isSeparatePackage.ToString());
PlayerPrefs.SetString("ClearCache", needClearCache.ToString());
PlayerPrefs.SetString("IgnoreHotUpdateDll", ignoreHotUpdateDll.ToString());
PlayerPrefs.Save();
base.OnDisable();
}
private static IEnumerable GetAllProfiles()
{
var list = new ValueDropdownList<string>();
for (var i = 0; i < thisWindow.profileList.Count; i++)
{
list.Add(thisWindow.profileList[i], thisWindow.profileList[i]);
}
return list;
}
[FormerlySerializedAs("buildtarget")] [VerticalGroup("TargetParam")] [LabelText("打包平台"), LabelWidth(150)]
public BuildTarget buildTarget = BuildTarget.Android;
[VerticalGroup("ProfileParam")] [ValueDropdown("GetAllProfiles")] [LabelText("Profile"), LabelWidth(150)]
public string profileName;
[VerticalGroup("Version")] [LabelText("版本号"), LabelWidth(150)]
public string version;
[VerticalGroup("developmentBuild")] [LabelText("developmentBuild"), LabelWidth(150)]
public bool developmentBuild = true;
[VerticalGroup("GroupParam")] [LabelText("单独打包组"), LabelWidth(150)]
public bool isSeparatePackage;
[VerticalGroup("GroupParam")] [ShowIf("isSeparatePackage")] [LabelText("选择组")]
public List<AddressableAssetGroup> separateGroup;
[VerticalGroup("ClearCache")] [LabelText("清除缓存"), LabelWidth(150)]
public bool needClearCache;
[HideInInspector]
[VerticalGroup("HotUpdate")] [LabelText("忽略热更新"), LabelWidth(150)]
public bool ignoreHotUpdateDll;
[HideInInspector]
[VerticalGroup("WorkSpace")] [LabelText("WorkSpace"), LabelWidth(150)]
public string workSpace = "WorkSpace";
[HorizontalGroup("package")]
[PropertySpace(50)]
[Button("Only Build Addressables")]
private void OnlyBuildAddressables()
{
ApplySettings();
EditorBuild.BuildAddressable();
}
[PropertySpace(50)]
[HorizontalGroup("package")]
[Button("Build Addressable and Player")]
private void BuildAddressableAndPlayer()
{
ApplySettings();
EditorBuild.BuildContentAndPlayer();
}
[PropertySpace(10)]
[HorizontalGroup("package2")]
[Button("Update a Previous Build")]
private void UpdatePreviousBuild()
{
ApplySettings();
EditorBuild.UpdatePreviousBuild();
}
[PropertySpace(10)]
[HorizontalGroup("package2")]
[Button("Only build Player")]
private void OnBuildPlayer()
{
ApplySettings();
EditorBuild.BuildPlayer();
}
[PropertySpace(10)]
[HorizontalGroup("Clean")]
[Button("还原设置")]
private void RevertSettings()
{
buildTarget = BuildTarget.Android;
profileName = "Debug";
version = PlayerSettings.bundleVersion;
workSpace = "WorkSpace";
isSeparatePackage = false;
needClearCache = false;
ignoreHotUpdateDll = false;
}
private void ApplySettings()
{
AddressableBuildBase.BuildParam.BuildTargetPlatform = buildTarget;
AddressableBuildBase.BuildParam.ProfileID = profileName;
AddressableBuildBase.BuildParam.WorkSpace = workSpace;
AddressableBuildBase.BuildParam.AppVersion = version;
AddressableBuildBase.BuildParam.DevelopmentBuild = developmentBuild;
if (!isSeparatePackage)
AddressableBuildBase.BuildParam.Groups = null;
else
{
var groups = new string[separateGroup.Count];
for (int i = 0; i < separateGroup.Count; i++)
{
groups[i] = separateGroup[i].name;
}
AddressableBuildBase.BuildParam.Groups = groups;
}
AddressableBuildBase.BuildParam.ClearCached = needClearCache;
AddressableBuildBase.BuildParam.EnableHotUpdate = ignoreHotUpdateDll;
}
}