215 lines
6.6 KiB
C#
215 lines
6.6 KiB
C#
using DefaultNamespace;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using Framework.Dto;
|
||
using Framework.Manager;
|
||
using MotionFramework;
|
||
|
||
/// <summary>
|
||
/// 表单选择下拉框处理器
|
||
/// 监听TMP_Dropdown选择变化,判断formselection是表单1还是表单2,并打印选择的值
|
||
/// </summary>
|
||
public class FormSelectionDropdownHandler : MonoBehaviour
|
||
{
|
||
[Header("调试设置")] [Tooltip("是否启用调试日志")] [SerializeField]
|
||
private bool enableDebugLog = true;
|
||
|
||
[Tooltip("下拉框组件")] [SerializeField] private TMP_Dropdown tmpDropdown;
|
||
|
||
public GameObject plane1;
|
||
public GameObject plane2;
|
||
public TMP_Text plane2Text;
|
||
private InventoryReversalVoucherAnalyzer returnRevVoucherAnalyzer;
|
||
|
||
/// <summary>
|
||
/// 初始化组件
|
||
/// </summary>
|
||
private void Start()
|
||
{
|
||
// 获取InventoryReversalVoucherAnalyzer实例
|
||
returnRevVoucherAnalyzer = (InventoryReversalVoucherAnalyzer)MotionEngine.GetModule<GlobalDataStorage>().materialTaskObj;
|
||
|
||
// 如果没有手动指定下拉框,尝试自动获取
|
||
if (tmpDropdown == null)
|
||
{
|
||
tmpDropdown = GetComponent<TMP_Dropdown>();
|
||
}
|
||
|
||
// 检查组件是否存在
|
||
if (tmpDropdown == null)
|
||
{
|
||
Debug.LogError($"FormSelectionDropdownHandler: 在 {gameObject.name} 中未找到 TMP_Dropdown 组件!");
|
||
return;
|
||
}
|
||
|
||
if (returnRevVoucherAnalyzer == null)
|
||
{
|
||
Debug.LogError($"FormSelectionDropdownHandler: 无法获取 InventoryReversalVoucherAnalyzer 实例!");
|
||
return;
|
||
}
|
||
|
||
// 监听下拉框选择变化事件
|
||
tmpDropdown.onValueChanged.AddListener(OnDropdownValueChanged);
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"FormSelectionDropdownHandler: 初始化完成 - {gameObject.name}");
|
||
Debug.Log($"当前formselection值: {returnRevVoucherAnalyzer.formselection}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下拉框选择变化回调
|
||
/// </summary>
|
||
/// <param name="value">选择的索引</param>
|
||
private void OnDropdownValueChanged(int value)
|
||
{
|
||
// 检查索引是否有效
|
||
if (value < 0 || value >= tmpDropdown.options.Count)
|
||
{
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.LogWarning($"FormSelectionDropdownHandler: 选择无效,索引: {value}");
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
// 获取选择的文本值
|
||
string selectedValue = tmpDropdown.options[value].text;
|
||
|
||
// 判断formselection是表单1还是表单2
|
||
|
||
// 打印判断结果
|
||
PrintSelectionResult(selectedValue, returnRevVoucherAnalyzer.formselection);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 打印选择结果
|
||
/// </summary>
|
||
/// <param name="selectedValue">选择的值</param>
|
||
/// <param name="formType">表单类型</param>
|
||
private void PrintSelectionResult(string selectedValue, string formType)
|
||
{
|
||
string logMessage = $"=== 下拉框选择结果 ===\n" +
|
||
$"选择的值: {selectedValue}\n" +
|
||
$"formselection: {returnRevVoucherAnalyzer.formselection}\n" +
|
||
$"判断结果: {formType}\n" +
|
||
$"下拉框名称: {gameObject.name}";
|
||
|
||
// 也可以输出到控制台
|
||
print(logMessage);
|
||
|
||
plane1.SetActive(false);
|
||
plane2.SetActive(true);
|
||
|
||
|
||
if (returnRevVoucherAnalyzer.formselection == "表单1")
|
||
{
|
||
switch (selectedValue)
|
||
{
|
||
case "1805L01111":
|
||
plane2Text.text = "目前1805L01111批次生产中";
|
||
break;
|
||
case "1805L01099":
|
||
plane2Text.text = "目前1805L01099批次物资已发送";
|
||
break;
|
||
case "1805L01095":
|
||
plane2Text.text = "目前1805L01095批次生产中";
|
||
break;
|
||
case "1804L00346":
|
||
plane2Text.text = "目前1804L00346批次物资已到货";
|
||
break;
|
||
case "1804L00345":
|
||
plane2Text.text = "目前1804L00345批次图纸已完成确认";
|
||
break;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
switch (selectedValue)
|
||
{
|
||
case "1805L01111":
|
||
plane2Text.text = "目前1805L01111批次生产中";
|
||
break;
|
||
case "1805L01099":
|
||
plane2Text.text = "目前1805L01099批次物资已发送";
|
||
break;
|
||
case "1805L01095":
|
||
plane2Text.text = "目前1805L01095批次生产中";
|
||
break;
|
||
case "1804L00346":
|
||
plane2Text.text = "目前1804L00346批次物资已到货";
|
||
break;
|
||
case "1804L00345":
|
||
plane2Text.text = "目前1804L00345批次图纸已到货";
|
||
break;
|
||
}
|
||
}
|
||
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log(logMessage);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 手动触发判断(用于测试)
|
||
/// </summary>
|
||
public void TriggerManualCheck()
|
||
{
|
||
if (tmpDropdown != null && returnRevVoucherAnalyzer != null)
|
||
{
|
||
OnDropdownValueChanged(tmpDropdown.value);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置是否启用调试日志
|
||
/// </summary>
|
||
/// <param name="enabled">是否启用</param>
|
||
public void SetDebugLogEnabled(bool enabled)
|
||
{
|
||
enableDebugLog = enabled;
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"FormSelectionDropdownHandler: 调试日志 {(enabled ? "启用" : "禁用")} - {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前formselection值
|
||
/// </summary>
|
||
/// <returns>formselection值</returns>
|
||
public string GetCurrentFormSelection()
|
||
{
|
||
return returnRevVoucherAnalyzer?.formselection ?? "未获取到";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前选择的文本
|
||
/// </summary>
|
||
/// <returns>当前选择的文本</returns>
|
||
public string GetCurrentSelectedValue()
|
||
{
|
||
if (tmpDropdown != null && tmpDropdown.value >= 0 && tmpDropdown.value < tmpDropdown.options.Count)
|
||
{
|
||
return tmpDropdown.options[tmpDropdown.value].text;
|
||
}
|
||
|
||
return "无选择";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 组件销毁时清理事件监听
|
||
/// </summary>
|
||
private void OnDestroy()
|
||
{
|
||
if (tmpDropdown != null)
|
||
{
|
||
tmpDropdown.onValueChanged.RemoveListener(OnDropdownValueChanged);
|
||
}
|
||
}
|
||
} |