_xiaofang/xiaofang/Assets/Script/UI/ZZZZZZ/OpcOrder2.cs

130 lines
4.0 KiB
C#
Raw Normal View History

2024-12-23 19:12:48 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OpcOrder2 : MonoBehaviour
{
public Dropdown dropdown; // Dropdown <20><><EFBFBD><EFBFBD>
private string originalText; // <20>洢ԭʼ<D4AD><CABC>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public static OpcOrder2 opcOrder2;
public string defaultOption = "<22><><EFBFBD><EFBFBD><EFBFBD>ϰ࣡"; // Ĭ<><C4AC>ѡ<EFBFBD><D1A1>
private bool isDropdownInitialized = false;
private void Start()
{
// <20><>ʼ<EFBFBD><CABC> Dropdown
if (dropdown == null)
{
dropdown = GetComponent<Dropdown>();
}
// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ѡ<EFBFBD><D1A1>
dropdown.options.Clear();
// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ѡ<EFBFBD><D1A1>
dropdown.options.Add(new Dropdown.OptionData(defaultOption)); // Ĭ<><C4AC>ѡ<EFBFBD><D1A1>A
dropdown.captionText.text = defaultOption; // <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EEA3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> A<><41>
dropdown.options.Add(new Dropdown.OptionData("ѡ<><D1A1> B"));
dropdown.options.Add(new Dropdown.OptionData("ѡ<><D1A1> C"));
dropdown.options.Add(new Dropdown.OptionData("ѡ<><D1A1> D"));
// <20><><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>¼<EFBFBD>
dropdown.onValueChanged.AddListener(OnDropdownValueChanged);
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>¼<EFBFBD>
BindToggleEvents();
}
// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ѡ<EFBFBD><D1A1><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD>ı<EFBFBD>
public string SetText(string str)
{
defaultOption = str;
dropdown.captionText.text = defaultOption; // <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>ı<EFBFBD>
return str;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>ı<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
private void OnDropdownValueChanged(int index)
{
if (isDropdownInitialized)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊѡ<CEAA>е<EFBFBD>ѡ<EFBFBD><D1A1>
// <20><><EFBFBD>Dz<EFBFBD><C7B2>ı<EFBFBD>Ĭ<EFBFBD><C4AC>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>ֵ
dropdown.captionText.text = dropdown.options[index].text;
// ִ<><D6B4><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>߼<EFBFBD>
Debug.Log($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD>{dropdown.options[index].text}");
}
isDropdownInitialized = true;
}
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD> Toggle <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
private void BindToggleEvents()
{
// <20><>ȡ Dropdown <20><> Template<74><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>
Transform dropdownTemplate = dropdown.transform.Find("Template");
if (dropdownTemplate != null)
{
Transform viewport = dropdownTemplate.Find("Viewport");
if (viewport != null)
{
Transform content = viewport.Find("Content");
if (content != null)
{
// <20><><EFBFBD><EFBFBD> Content <20>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1>е<EFBFBD>ѡ<EFBFBD>
foreach (Transform option in content)
{
Toggle toggle = option.GetComponent<Toggle>();
if (toggle != null)
{
string optionText = option.GetComponentInChildren<Text>().text;
// Ϊÿ<CEAA><C3BF>ѡ<EFBFBD><D1A1><EFBFBD>󶨵<EFBFBD><F3B6A8B5><EFBFBD><EFBFBD>¼<EFBFBD>
toggle.onValueChanged.AddListener((isOn) =>
{
if (isOn)
{
HandleOptionClick(optionText);
toggle.isOn = false; // <20><><EFBFBD><EFBFBD> Toggle ״̬<D7B4><CCAC><EFBFBD><EFBFBD>ֹѡ<D6B9><EFBFBD><EEB1A3>ѡ<EFBFBD><D1A1>
}
});
}
}
}
}
}
else
{
Debug.LogWarning(<>ҵ<EFBFBD> Dropdown <20><> Template <20><><EFBFBD>֣<EFBFBD><D6A3>޷<EFBFBD><DEB7>󶨵<EFBFBD><F3B6A8B5><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>");
}
}
// <20><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>
private void HandleOptionClick(string optionText)
{
Debug.Log($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD>{optionText}");
// <20><><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>ִ<EFBFBD>в<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>߼<EFBFBD>
switch (optionText)
{
case "ѡ<><D1A1> B":
Debug.Log(<><D6B4>ѡ<EFBFBD><D1A1> B <20><><EFBFBD>߼<EFBFBD><DFBC><EFBFBD>");
break;
case "ѡ<><D1A1> C":
Debug.Log(<><D6B4>ѡ<EFBFBD><D1A1> C <20><><EFBFBD>߼<EFBFBD><DFBC><EFBFBD>");
break;
case "ѡ<><D1A1> D":
Debug.Log(<><D6B4>ѡ<EFBFBD><D1A1> D <20><><EFBFBD>߼<EFBFBD><DFBC><EFBFBD>");
break;
default:
Debug.LogWarning($"δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD>{optionText}");
break;
}
}
}