_xiaofang/xiaofang/Assets/Res/gsj/scripts/Panel2.cs
2024-12-20 10:10:19 +08:00

127 lines
4.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class Panel2 : MonoBehaviour
{
private Toggle toggle;//角色名是否出现
private Button follow;//跟随查按钮
private Button end;//结束按钮
private Dropdown dropdown;
private int optionCount; // 用于存储传入的确定Dropdown选项数量的参数
private List<Camera> characterCameras; // 存储每个角色的摄像头
private List<GameObject> nameImage; // 存储所有角色标签的子物体
private GameObject peopleposition;
// Start is called before the first frame update
void Start()
{
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 = transform.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);
// 初始化角色摄像头列表
//characterCameras = new List<Camera>();
//// 假设角色摄像头是挂载在名为 "CharacterCamera1", "CharacterCamera2", 等等的对象上
//for (int i = 1; i <= optionCount; i++)
//{
// GameObject characterCameraObject = GameObject.Find("CharacterCamera" + i);
// if (characterCameraObject != null)
// {
// characterCameras.Add(characterCameraObject.GetComponent<Camera>());
// }
//}
}
// 用于外部传入参数来设置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) + "视角");
// 切换摄像头
//SwitchToCharacterCamera(selectedIndex);
Game.uiManager.CloseUI("Panel1_2");
Game.uiManager.ShowUI<Image>("Panel1_3");
}
// 切换到选中的角色视角
void SwitchToCharacterCamera(int index)
{
// 禁用所有角色摄像头
foreach (var camera in characterCameras)
{
if (camera != null)
{
camera.gameObject.SetActive(false);
}
}
// 启用选中角色的摄像头
if (index >= 0 && index < characterCameras.Count)
{
Camera selectedCamera = characterCameras[index];
if (selectedCamera != null)
{
selectedCamera.gameObject.SetActive(true);
}
}
}
void EndBtn()
{
//结束演习
Game.uiManager.ShowUI<Image>("PoPhost");
}
}