279 lines
7.8 KiB
C#
279 lines
7.8 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
using System.Collections.Generic;
|
||
|
||
/// <summary>
|
||
/// 通用设置面板基类
|
||
/// 提供标准化的设置面板布局和功能
|
||
/// </summary>
|
||
public abstract class BaseSettingPanel : BasePanel
|
||
{
|
||
[Header("面板标题")]
|
||
public TextMeshProUGUI TitleText;
|
||
public TextMeshProUGUI SubtitleText;
|
||
|
||
[Header("设置项容器")]
|
||
public Transform SettingsContainer;
|
||
public GameObject SettingItemPrefab;
|
||
|
||
[Header("标准按钮")]
|
||
public Button HomeButton;
|
||
public Button ConfirmButton;
|
||
public Button BackButton;
|
||
public Button ResetButton;
|
||
|
||
protected SettingsViewModel _viewModel;
|
||
protected List<SettingItemComponent> _settingItems = new List<SettingItemComponent>();
|
||
|
||
public override void Init()
|
||
{
|
||
_viewModel = new SettingsViewModel();
|
||
|
||
SetupTitle();
|
||
SetupStandardButtons();
|
||
CreateSettingItems();
|
||
LoadCurrentValues();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置面板标题
|
||
/// </summary>
|
||
protected virtual void SetupTitle()
|
||
{
|
||
if (TitleText != null)
|
||
TitleText.text = GetPanelTitle();
|
||
|
||
if (SubtitleText != null)
|
||
SubtitleText.text = GetPanelSubtitle();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置标准按钮事件
|
||
/// </summary>
|
||
protected virtual void SetupStandardButtons()
|
||
{
|
||
if (HomeButton != null)
|
||
{
|
||
HomeButton.onClick.AddListener(() =>
|
||
{
|
||
OnHomeButtonClicked();
|
||
ClosePanel();
|
||
UIManager.Instance.ShowPanel<MainPanel>();
|
||
});
|
||
}
|
||
|
||
if (ConfirmButton != null)
|
||
{
|
||
ConfirmButton.onClick.AddListener(() =>
|
||
{
|
||
if (ValidateAndApplySettings())
|
||
{
|
||
OnConfirmButtonClicked();
|
||
ClosePanel();
|
||
}
|
||
});
|
||
}
|
||
|
||
if (BackButton != null)
|
||
{
|
||
BackButton.onClick.AddListener(() =>
|
||
{
|
||
OnBackButtonClicked();
|
||
ClosePanel();
|
||
});
|
||
}
|
||
|
||
if (ResetButton != null)
|
||
{
|
||
ResetButton.onClick.AddListener(() =>
|
||
{
|
||
ResetToDefaults();
|
||
OnResetButtonClicked();
|
||
});
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建设置项
|
||
/// </summary>
|
||
protected virtual void CreateSettingItems()
|
||
{
|
||
if (SettingsContainer == null) return;
|
||
|
||
var settingConfigs = GetSettingConfigurations();
|
||
|
||
foreach (var config in settingConfigs)
|
||
{
|
||
var settingItem = CreateSettingItem(config);
|
||
if (settingItem != null)
|
||
{
|
||
_settingItems.Add(settingItem);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建单个设置项
|
||
/// </summary>
|
||
protected virtual SettingItemComponent CreateSettingItem(SettingConfig config)
|
||
{
|
||
GameObject itemGO;
|
||
|
||
if (SettingItemPrefab != null)
|
||
{
|
||
itemGO = Instantiate(SettingItemPrefab, SettingsContainer);
|
||
}
|
||
else
|
||
{
|
||
// 如果没有预制体,创建基础设置项
|
||
itemGO = CreateBasicSettingItem();
|
||
}
|
||
|
||
var settingItem = itemGO.GetComponent<SettingItemComponent>();
|
||
if (settingItem == null)
|
||
{
|
||
settingItem = itemGO.AddComponent<SettingItemComponent>();
|
||
}
|
||
|
||
// 配置设置项
|
||
ConfigureSettingItem(settingItem, config);
|
||
|
||
return settingItem;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 配置设置项
|
||
/// </summary>
|
||
protected virtual void ConfigureSettingItem(SettingItemComponent item, SettingConfig config)
|
||
{
|
||
item.SetInfo(config.Title, config.Description, config.Icon);
|
||
|
||
switch (config.Type)
|
||
{
|
||
case SettingType.Slider:
|
||
var slider = item.CreateSlider(config.MinValue, config.MaxValue, config.CurrentValue, config.WholeNumbers);
|
||
item.OnSliderChanged.AddListener((value) => OnSettingChanged(config.Key, value));
|
||
break;
|
||
|
||
case SettingType.Toggle:
|
||
var toggle = item.CreateToggle(config.CurrentValue > 0);
|
||
item.OnToggleChanged.AddListener((value) => OnSettingChanged(config.Key, value));
|
||
break;
|
||
|
||
case SettingType.InputField:
|
||
var input = item.CreateInputField(config.Placeholder, config.StringValue);
|
||
item.OnInputChanged.AddListener((value) => OnSettingChanged(config.Key, value));
|
||
break;
|
||
|
||
case SettingType.Dropdown:
|
||
var dropdown = item.CreateDropdown(config.Options, (int)config.CurrentValue);
|
||
item.OnDropdownChanged.AddListener((value) => OnSettingChanged(config.Key, value));
|
||
break;
|
||
|
||
case SettingType.Button:
|
||
var button = item.CreateButton(config.ButtonText);
|
||
item.OnButtonClicked.AddListener(() => OnSettingButtonClicked(config.Key));
|
||
break;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建基础设置项GameObject
|
||
/// </summary>
|
||
protected virtual GameObject CreateBasicSettingItem()
|
||
{
|
||
var itemGO = new GameObject("SettingItem", typeof(RectTransform));
|
||
itemGO.transform.SetParent(SettingsContainer, false);
|
||
|
||
// 设置布局
|
||
var rect = itemGO.GetComponent<RectTransform>();
|
||
rect.anchorMin = new Vector2(0, 1);
|
||
rect.anchorMax = new Vector2(1, 1);
|
||
rect.sizeDelta = new Vector2(0, 60);
|
||
|
||
return itemGO;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证并应用设置
|
||
/// </summary>
|
||
protected virtual bool ValidateAndApplySettings()
|
||
{
|
||
// 默认返回true,子类可以重写进行验证
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重置为默认值
|
||
/// </summary>
|
||
protected virtual void ResetToDefaults()
|
||
{
|
||
var configs = GetSettingConfigurations();
|
||
foreach (var config in configs)
|
||
{
|
||
OnSettingChanged(config.Key, config.DefaultValue);
|
||
}
|
||
LoadCurrentValues();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载当前值到UI
|
||
/// </summary>
|
||
protected virtual void LoadCurrentValues()
|
||
{
|
||
// 子类实现具体的值加载逻辑
|
||
}
|
||
|
||
// 抽象方法 - 子类必须实现
|
||
protected abstract string GetPanelTitle();
|
||
protected abstract string GetPanelSubtitle();
|
||
protected abstract List<SettingConfig> GetSettingConfigurations();
|
||
|
||
// 虚方法 - 子类可以重写
|
||
protected virtual void OnSettingChanged(string key, object value) { }
|
||
protected virtual void OnSettingButtonClicked(string key) { }
|
||
protected virtual void OnHomeButtonClicked() { }
|
||
protected virtual void OnConfirmButtonClicked() { }
|
||
protected virtual void OnBackButtonClicked() { }
|
||
protected virtual void OnResetButtonClicked() { }
|
||
|
||
public virtual void ClosePanel()
|
||
{
|
||
UIManager.Instance.HidePanel<BaseSettingPanel>();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置项配置
|
||
/// </summary>
|
||
[System.Serializable]
|
||
public class SettingConfig
|
||
{
|
||
public string Key;
|
||
public string Title;
|
||
public string Description;
|
||
public Sprite Icon;
|
||
public SettingType Type;
|
||
public float MinValue;
|
||
public float MaxValue;
|
||
public float CurrentValue;
|
||
public float DefaultValue;
|
||
public bool WholeNumbers;
|
||
public string StringValue;
|
||
public string Placeholder;
|
||
public string[] Options;
|
||
public string ButtonText;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置类型
|
||
/// </summary>
|
||
public enum SettingType
|
||
{
|
||
Slider,
|
||
Toggle,
|
||
InputField,
|
||
Dropdown,
|
||
Button
|
||
} |