DCS/ruiyiweiUX/Assets/Scripts/Components/BodyAlphaController.cs

178 lines
4.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
using TMPro;
/// <summary>
/// Body透明度控制器
/// 提供运行时调整Body模型透明度的功能
/// </summary>
public class BodyAlphaController : MonoBehaviour
{
[Header("UI 控件")]
public Slider alphaSlider;
public TextMeshProUGUI alphaValueText;
public Button resetButton;
[Header("设置")]
[Range(0f, 1f)]
public float defaultAlpha = 0.6f;
public bool showDebugInfo = false;
private ProcessModelMatData materialProcessor;
void Start()
{
// 查找材质处理组件
FindMaterialProcessor();
// 初始化UI
InitializeUI();
}
private void FindMaterialProcessor()
{
// 尝试在场景中查找ProcessModelMatData组件
materialProcessor = FindObjectOfType<ProcessModelMatData>();
if (materialProcessor == null)
{
Debug.LogError("BodyAlphaController: 未找到ProcessModelMatData组件");
}
else if (showDebugInfo)
{
Debug.Log("BodyAlphaController: 成功找到ProcessModelMatData组件");
}
}
private void InitializeUI()
{
if (alphaSlider != null)
{
alphaSlider.minValue = 0f;
alphaSlider.maxValue = 1f;
alphaSlider.value = defaultAlpha;
alphaSlider.onValueChanged.AddListener(OnAlphaSliderChanged);
}
if (resetButton != null)
{
resetButton.onClick.AddListener(ResetToDefault);
}
// 更新显示
UpdateAlphaDisplay();
// 应用默认透明度
SetBodyAlpha(defaultAlpha);
}
private void OnAlphaSliderChanged(float value)
{
SetBodyAlpha(value);
UpdateAlphaDisplay();
}
private void UpdateAlphaDisplay()
{
if (alphaValueText != null && alphaSlider != null)
{
float percentage = alphaSlider.value * 100f;
alphaValueText.text = $"{percentage:F0}%";
}
}
/// <summary>
/// 设置Body的透明度
/// </summary>
/// <param name="alpha">透明度值</param>
public void SetBodyAlpha(float alpha)
{
if (materialProcessor != null)
{
materialProcessor.SetBodyAlpha(alpha);
// 通知状态保持器保存新状态
if (MaterialStateKeeper.Instance != null)
{
MaterialStateKeeper.Instance.SetAndSaveBodyAlpha(alpha);
}
if (showDebugInfo)
{
Debug.Log($"BodyAlphaController: 设置Body透明度为 {alpha:F2}");
}
}
}
/// <summary>
/// 重置到默认透明度
/// </summary>
public void ResetToDefault()
{
if (alphaSlider != null)
{
alphaSlider.value = defaultAlpha;
}
SetBodyAlpha(defaultAlpha);
UpdateAlphaDisplay();
if (showDebugInfo)
{
Debug.Log("BodyAlphaController: 重置Body透明度到默认值");
}
}
/// <summary>
/// 获取当前Body的透明度
/// </summary>
/// <returns></returns>
public float GetCurrentBodyAlpha()
{
if (materialProcessor != null)
{
return materialProcessor.GetBodyAlpha();
}
return defaultAlpha;
}
/// <summary>
/// 设置为完全不透明
/// </summary>
public void SetOpaque()
{
if (alphaSlider != null)
{
alphaSlider.value = 1f;
}
SetBodyAlpha(1f);
UpdateAlphaDisplay();
}
/// <summary>
/// 设置为半透明
/// </summary>
public void SetSemiTransparent()
{
float semiAlpha = 0.5f;
if (alphaSlider != null)
{
alphaSlider.value = semiAlpha;
}
SetBodyAlpha(semiAlpha);
UpdateAlphaDisplay();
}
void OnDestroy()
{
// 清理事件监听
if (alphaSlider != null)
{
alphaSlider.onValueChanged.RemoveListener(OnAlphaSliderChanged);
}
if (resetButton != null)
{
resetButton.onClick.RemoveListener(ResetToDefault);
}
}
}