_xiaofang/xiaofang/Assets/yhj/scripts/JueseChoicePop.cs
2024-12-24 18:51:35 +08:00

130 lines
4.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.Assertions.Must;
using UnityEngine.Networking.Types;
using UnityEngine.UI;
public class JueseChoicePop : MonoBehaviour
{
public ClassMate classMate;
public ClassItem classItem;
public Button CloseBtn;
public Button Surebtn;
public GameObject classmatePrefab;
public Transform content;
public List<ClassMate> classMateList = new List<ClassMate> ();
// 存储已实例化的 GameObject 列表
private List<GameObject> instantiatedObjects = new List<GameObject>();
public string s;
public JSONReader jsonReader;
public Panel panel;
//Start is called before the first frame update
void Start()
{
this.gameObject.SetActive(false);
CloseBtn.onClick.AddListener(CLosePop);
Surebtn.onClick.AddListener(CLosePop);
//Surebtn.onClick.AddListener(SetFenguan);
//SetClass();
}
void Update()
{
}
void CLosePop()
{
this.gameObject.SetActive(false);
}
public void openPop()
{
this.gameObject.SetActive(true);
}
void SetFenguan()
{
//设置此区分的分管人员为用户选中的角色,并刷新界面信息
classItem.isSet = true;
//classItem.setClassItem();
}
//加载分管人员
public void SetClass(int id, string scnenName)
{
Debug.LogError(">>>>>>>>>>>>>>>>>>>>>>>>>>"+id);
// 检查是否已经实例化过该 id 的对象
if (instantiatedObjects.Any(obj => obj.GetComponent<ClassMate>().id == id))
{
Debug.Log($"这个: {id} 已经被实例化.");
return; // 如果已实例化,跳过
}
// 清除当前内容
foreach (Transform child in content)
{
Destroy(child.gameObject);
}
// 获取指定 ID 的 LocationData
LocationData locationData = jsonReader.GetAreaDateById(id);
// 判断 Oversee 字段
if (locationData.Oversee == "-1")
{
Debug.Log($"Skipping ID {id}: Oversee is -1.");
return; // 跳过
}
// 分割 Oversee 字段
string[] overseeParts = locationData.Oversee.Split(',');
string firstElement = overseeParts[0];
string lastElement = overseeParts[1];
if (overseeParts.Length > 0)
{
// 判断第一个元素是否为 "1"
if (lastElement == "1")
{
foreach (var pair in panel.sceneDataDictionary)
{
foreach (var selectedInfo in pair.Value)
{
if (selectedInfo.dutyId == firstElement)
{
GameObject item = GameObject.Instantiate(classmatePrefab, content);
ClassMate classMate = item.GetComponent<ClassMate>();
Button button = item.GetComponent<Button>();
button.onClick.AddListener(() => {
SetClassMate(classMate, scnenName);
});
classMate.name = selectedInfo.name;
classMate.classmatename.text = classMate.name;
classMate.id = id;
// 将实例化的对象添加到列表中
instantiatedObjects.Add(item);
}
}
}
Debug.Log($"Instantiated ClassMate for ID {id} with name {locationData.Name}");
}
}
}
//点击人员后设置位置的方法
public void SetClassMate(ClassMate classMate, string a = "")
{
this.classMate = classMate;
this.classMate.classmatename.text = classMate.name + "(" + a + ")";
classMate.isBeSet = false;
}
public void SetClassItem(ClassItem classItem)
{
this.classItem = classItem;
}
}