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

330 lines
9.5 KiB
C#
Raw Normal View History

using System;
2024-12-23 19:12:48 +08:00
using System.Collections;
using System.Collections.Generic;
2024-12-26 18:03:45 +08:00
using DG.Tweening;
using UnityEditor.UIElements;
2024-12-23 19:12:48 +08:00
using UnityEngine;
using UnityEngine.UI;
public class OpcOrder2 : MonoBehaviour
{
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//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;
// }
//}
#endregion
public GameObject Tip;
public Button btn;
public GameObject itemPrefab; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD>ItemԤ<6D><D4A4><EFBFBD><EFBFBD>
public Transform contentTransform; // Content<6E><74>Transform
public JSONReader jsonReader;
2024-12-26 22:30:20 +08:00
public List<List<string>> groupedData;
public List<GameObject> ItemList;
2024-12-26 18:03:45 +08:00
public string name;
public OpcOrder2 hylCS;
int a = 0;
2024-12-23 19:12:48 +08:00
private void Start()
{
2024-12-26 23:18:09 +08:00
2024-12-26 18:03:45 +08:00
GetOpcID(8006);
hylCS = this;
btn.onClick.AddListener(() =>
2024-12-23 19:12:48 +08:00
{
bool isActive = Tip.activeSelf;
Tip.SetActive(!isActive);
2024-12-26 22:30:20 +08:00
if (ItemList.Count == 0)
{
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-12-26 22:30:20 +08:00
CreateItem(2001, 0);
}
});
}
2024-12-26 18:03:45 +08:00
/// <summary>
/// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
/// </summary>
/// <param name="ID"></param>
public void GetOpcID(int ID)
{
a = ID;
name=jsonReader.GetOcpName(a);
}
public void CreateItem(int ID, int k)
{
2024-12-26 22:30:20 +08:00
if (k > 3)
{
if (k == 4) k = 1;
if (k == 5) k = 2;
if (k == 6) k = 3;
2024-12-23 19:12:48 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
foreach (var kvp in jsonReader.incidentSiteDictionary)
{
IncidentSite select = kvp.Value;
2024-12-23 19:12:48 +08:00
if (select.Name == ID)
{
groupedData = ParseData(select.Role);
}
}
2024-12-26 22:30:20 +08:00
print(groupedData.Count);
// <20><><EFBFBD><EFBFBD> k <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
if (k < 0 || k >= groupedData.Count)
{
Debug.LogError($"<22><><EFBFBD><EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7>k = {k}, groupedData.Count = {groupedData.Count}");
return;
}
// <20><><EFBFBD><EFBFBD> groupedData[k] <20>Ƿ<EFBFBD>Ϊ<EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (groupedData[k] == null || groupedData[k].Count == 0)
{
Debug.LogError($"groupedData[{k}] <20>ǿջ<C7BF>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>");
return;
}
2024-12-23 19:12:48 +08:00
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>Item<65><6D><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Content
2024-12-26 18:03:45 +08:00
for (int i = 0; i < groupedData[k].Count; i++)
{
// <20><><EFBFBD><EFBFBD>Item<65><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪContent<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GameObject newItem = Instantiate(itemPrefab, contentTransform);
ItemList.Add(newItem);
2024-12-23 19:12:48 +08:00
// Ϊÿ<CEAA><C3BF>Item<65><6D><EFBFBD>Ӱ<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
Button button = newItem.GetComponent<Button>();
if (button != null)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>Item<65><6D><EFBFBD>󣬱<EFBFBD><F3A3ACB1><EFBFBD><EFBFBD>հ<EFBFBD><D5B0><EFBFBD><EFBFBD><EFBFBD>
GameObject currentItem = newItem;
2024-12-23 19:12:48 +08:00
// <20><><EFBFBD>ݾ<EFBFBD><DDBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󶨶<EFBFBD><F3B6A8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>
button.onClick.AddListener(() => OnItemClick(currentItem, i)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD>
}
else
{
Debug.LogError("<22><><EFBFBD>ɵ<EFBFBD>Item<65><6D>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD>Button<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
}
2024-12-23 19:12:48 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
SetItemText(k);
2024-12-26 18:03:45 +08:00
2024-12-23 19:12:48 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private void OnItemClick(GameObject item, int index)
2024-12-23 19:12:48 +08:00
{
2024-12-26 22:30:20 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + index);
2024-12-26 18:03:45 +08:00
if ( item.GetComponentInChildren<Text>().text==name)
{
}
else
{
item.SetActive(false); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ص<EFBFBD>Item
Tip.SetActive(false);
2024-12-23 19:12:48 +08:00
}
}
2024-12-26 18:03:45 +08:00
//// <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ص<EFBFBD>ǰItem
//private bool ShouldHideItem(int index)
//{
// // ʾ<><CABE><EFBFBD>߼<EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
// // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊż<CEAA><C5BC><EFBFBD><EFBFBD>Item
// return index == a;
//}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
public void SetItemText(int k)
{
2024-12-26 22:30:20 +08:00
// <20><><EFBFBD><EFBFBD> k <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
if (k < 0 || k >= groupedData.Count)
{
Debug.LogError($"<22><><EFBFBD><EFBFBD> k <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7>k = {k}, groupedData.Count = {groupedData.Count}");
return;
}
for (int i = 1; i < ItemList.Count+1; i++)
2024-12-23 19:12:48 +08:00
{
2024-12-26 22:30:20 +08:00
// <20><><EFBFBD><EFBFBD> i <20>Ƿ񳬳<C7B7> groupedData[k] <20>ķ<EFBFBD>Χ
if (i >= groupedData[k].Count)
{
Debug.LogWarning($"<22><><EFBFBD><EFBFBD> i <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7>i = {i}, groupedData[{k}].Count = {groupedData[k].Count}");
break;
}
Text itemText = ItemList[i].GetComponentInChildren<Text>();
if (itemText != null)
2024-12-23 19:12:48 +08:00
{
itemText.text = jsonReader.GetOcpName(int.Parse(groupedData[k][i]));
}
else
{
Debug.LogError("<22><><EFBFBD>ɵ<EFBFBD>Item<65><6D>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD>Text<78><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-12-23 19:12:48 +08:00
}
}
}
public static List<List<string>> ParseData(string data)
2024-12-23 19:12:48 +08:00
{
// <20><EFBFBD><E6B4A2><EFBFBD>ս<EFBFBD><D5BD><EFBFBD>
List<List<string>> result = new List<List<string>>();
2024-12-23 19:12:48 +08:00
// <20><> `|` <20>ָ<EFBFBD><D6B8>ַ<EFBFBD><D6B7><EFBFBD>
string[] groups = data.Split('|');
foreach (var group in groups)
2024-12-23 19:12:48 +08:00
{
// <20><> `,` <20>ָ<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
string[] items = group.Split(',');
// <20><><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
result.Add(new List<string>(items));
2024-12-23 19:12:48 +08:00
}
return result;
2024-12-23 19:12:48 +08:00
}
2024-12-26 18:03:45 +08:00
/// <summary>
/// <20>ӵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <typeparam name="T">Ҫ<><D2AA><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
/// <returns><3E><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD>ҵ<EFBFBD><D2B5>򷵻<EFBFBD> null</returns>
public T FindParentOfType<T>() where T : Component
{
Transform current = transform;
// <20><><EFBFBD>ϲ<EFBFBD><CFB2>ң<EFBFBD>ֱ<EFBFBD><D6B1>û<EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>
while (current.parent != null)
{
current = current.parent;
T component = current.GetComponent<T>();
if (component != null)
{
return component; // <20>ҵ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
// <20><><EFBFBD><EFBFBD>δ<EFBFBD>ҵ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> null
return null;
}
2024-12-23 19:12:48 +08:00
}