AlicizaX/Client/Assets/Editor/PlayModePlus/PlayModeToolbar.cs

123 lines
5.4 KiB
C#
Raw Normal View History

2025-01-24 16:21:00 +08:00
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using YooAsset;
namespace BuildCli
{
public class PlayModeToolbar : VisualElement
{
private readonly PlayModeManager _playModeManager = new();
private readonly BuildManager _buildManager = new();
private readonly Button _buildButton;
private readonly DropdownField _selectSceneDropdown;
private readonly DropdownField _selectPlayModeSettingsDropdown;
private readonly DropdownField _selectPlayerSettingsPresetsDropdown;
public PlayModeToolbar()
{
#if UNITY_6000_0_OR_NEWER
var visualTree = Resources.Load<VisualTreeAsset>("com.disillusion.play-mode-plus/PlayModePlusToolbar6000");
#else
var visualTree = Resources.Load<VisualTreeAsset>("com.disillusion.play-mode-plus/PlayModePlusToolbar");
#endif
visualTree.CloneTree(this);
style.flexGrow = 1;
_buildButton = this.Q<Button>("build-button");
_buildButton.clicked -= OnBuildClicked;
_buildButton.clicked += OnBuildClicked;
#region Select Scene Dropdown
_selectSceneDropdown = this.Q<DropdownField>("select-scene-dropdown");
_selectSceneDropdown.RegisterCallback<FocusEvent>(evt =>
_selectSceneDropdown.choices = _playModeManager.GenerateResourceModeList());
_selectSceneDropdown.RegisterCallback<ChangeEvent<string>>(evt => { _playModeManager.SetPlayMode(evt.newValue); });
#endregion
#region Play Mode Settings Dropdown
_selectPlayModeSettingsDropdown = this.Q<DropdownField>("play-mode-settings-dropdown");
_selectPlayModeSettingsDropdown.RegisterCallback<FocusEvent>(evt =>
_selectSceneDropdown.choices = _playModeManager.GenerateResourceModeList());
_selectPlayModeSettingsDropdown.RegisterCallback<ChangeEvent<string>>(evt =>
{
var editorSettings = new SerializedObject(
AssetDatabase.LoadAssetAtPath("ProjectSettings/EditorSettings.asset", typeof(Object)));
var enterPlayModeOptionsEnabled =
editorSettings.FindProperty("m_EnterPlayModeOptionsEnabled");
var enterPlayModeOptions = editorSettings.FindProperty("m_EnterPlayModeOptions");
switch (_selectPlayModeSettingsDropdown.value)
{
case "Default (Reload Domain, Reload Scene":
enterPlayModeOptionsEnabled.boolValue = false;
enterPlayModeOptions.intValue = (int)EnterPlayModeOptions.None;
break;
case "Disable Reload Domain":
enterPlayModeOptionsEnabled.boolValue = true;
enterPlayModeOptions.intValue = (int)EnterPlayModeOptions.DisableDomainReload;
break;
case "Disable Reload Scene":
enterPlayModeOptionsEnabled.boolValue = true;
enterPlayModeOptions.intValue = (int)EnterPlayModeOptions.DisableSceneReload;
break;
case "Disable All":
enterPlayModeOptionsEnabled.boolValue = true;
enterPlayModeOptions.intValue |= (int)EnterPlayModeOptions.DisableDomainReload;
enterPlayModeOptions.intValue |= (int)EnterPlayModeOptions.DisableSceneReload;
break;
default:
enterPlayModeOptionsEnabled.boolValue = false;
enterPlayModeOptions.intValue = (int)EnterPlayModeOptions.None;
break;
}
editorSettings.ApplyModifiedProperties();
EditorUtility.SetDirty(editorSettings.targetObject);
AssetDatabase.SaveAssets();
});
#endregion
#region Build Settings Dropdown
_selectPlayerSettingsPresetsDropdown = this.Q<DropdownField>("build-settings-dropdown");
_selectPlayerSettingsPresetsDropdown.RegisterCallback<FocusEvent>(evt =>
_selectPlayerSettingsPresetsDropdown.choices = _buildManager.GenerateBuildSettingsList());
_selectPlayerSettingsPresetsDropdown.RegisterCallback<ChangeEvent<string>>(evt =>
{
_buildManager.SelectedBuildPreset =
_buildManager.PlayerSettingsPresetsInProject.FirstOrDefault(presets =>
presets.name == evt.newValue);
BuildManager.ApplyPreset(_buildManager.SelectedBuildPreset);
});
#endregion
// Set the play mode scene for Default play button
PlayModeManager.EditorStartScene();
// Populate Dropdowns
_selectSceneDropdown.choices = _playModeManager.GenerateResourceModeList();
_selectPlayModeSettingsDropdown.choices = _playModeManager.GeneratePlayModeSettingsList();
_selectPlayerSettingsPresetsDropdown.choices = _buildManager.GenerateBuildSettingsList();
// Set Select Scene Dropdown value to Last Scene which was opened
_selectSceneDropdown.index = _playModeManager.GetSelectPlayMode();
}
private void OnBuildClicked() => _buildManager.OpenBuildWindow();
}
}