_xiaofang/xiaofang/Assets/Res/gsj/scripts/Panel2.cs
2024-12-24 20:21:15 +08:00

95 lines
3.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;
public class Panel2 : MonoBehaviour
{
private Toggle toggle;//角色名是否出现
private Button follow;//跟随查按钮
private Button end;//结束按钮
private Dropdown dropdown;
private int optionCount; // 用于存储传入的确定Dropdown选项数量的参数
private List<GameObject> nameImage; // 存储所有角色标签的子物体
private GameObject peopleposition;
public Camera godView; // 上帝视角的位置
private bool isGodView = true; // 是否为上帝视角
// Start is called before the first frame update
void Start()
{
godView=GameObject.Find("TopCamera").GetComponent<Camera>();
toggle =transform.Find("right/under/Toggle").GetComponent<Toggle>();
toggle.onValueChanged.AddListener(OnToggleValueChanged);
toggle.isOn = true;
dropdown =transform.Find("left/Dropdown").GetComponent<Dropdown>();
follow=transform.Find("left/Follow").GetComponent<Button>();
follow.onClick.AddListener(FollowBtn);
end = transform.Find("left/btn_enddrill").GetComponent<Button>();
end.onClick.AddListener(EndBtn);
peopleposition = GameObject.Find("peopleposition").gameObject;
//获取所有子物体中的 Button 组件
nameImage = new List<GameObject>();
foreach (Transform child in peopleposition.transform)
{
// 查找每个子物体中的"bg"子物体
Transform bgTransform = child.Find("nameImage");
if (bgTransform != null)
{
nameImage.Add(bgTransform.gameObject);
}
}
// 默认执行一次切换
OnToggleValueChanged(toggle.isOn);
}
// 用于外部传入参数来设置Dropdown选项数量的方法
public void SetOptionCount(int count)
{
optionCount = count;
dropdown.ClearOptions();
// 创建一个临时的字符串列表用于添加选项内容
var options = new System.Collections.Generic.List<string>();
for (int i = 0; i < optionCount; i++)
{
options.Add("角色 " + (i + 1)); // 简单添加类似"角色 1"、"角色 2"这样的选项名称,可按需修改
}
dropdown.AddOptions(options);
}
// Update is called once per frame
void Update()
{
}
// Toggle状态变化时调用的方法
void OnToggleValueChanged(bool isOn)
{
// 根据Toggle的状态控制所有"nameImage"子物体的显示与隐藏
foreach (var bgObject in nameImage)
{
bgObject.SetActive(isOn); // 显示或隐藏
}
}
void FollowBtn()
{
//切换到跟随模式,显示角色视角
//peopleposition.SetActive(false);
int selectedIndex = dropdown.value;
Debug.Log("进入角色" + (selectedIndex+1) + "视角");
godView.enabled = false;
Game.uiManager.CloseUI("Panel1_2");
Game.uiManager.ShowUI<Image>("Panel1_3");
}
void EndBtn()
{
//结束演习
Game.uiManager.ShowUI<Image>("PoPhost");
}
}