137 lines
4.4 KiB
C#
137 lines
4.4 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
|
||
{
|
||
public static Panel2 instance;
|
||
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; // 上帝视角的位置
|
||
public List<Camera> playerCameras = new List<Camera>(); // 存储每个角色的摄像机
|
||
private bool isGodView = true; // 是否为上帝视角
|
||
public int currentCameraIndex = -1;
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
instance = this;
|
||
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);
|
||
}
|
||
}
|
||
// 获取所有标签为 "People" 的物体
|
||
GameObject[] players = GameObject.FindGameObjectsWithTag("People");
|
||
|
||
// 遍历每个 Player,找到子物体中的摄像机
|
||
foreach (GameObject player in players)
|
||
{
|
||
// 在 Player 的子物体中查找摄像机组件
|
||
Camera camera = player.GetComponentInChildren<Camera>();
|
||
if (camera != null)
|
||
{
|
||
playerCameras.Add(camera);
|
||
camera.enabled = false; // 初始化时禁用所有摄像机
|
||
}
|
||
}
|
||
// 默认执行一次切换
|
||
OnToggleValueChanged(toggle.isOn);
|
||
|
||
}
|
||
// 切换到指定索引的摄像机
|
||
public void SwitchToCamera(int index)
|
||
{
|
||
var index2=index;
|
||
if (index < 0 || index >= playerCameras.Count)
|
||
{
|
||
Debug.LogWarning("无效的摄像机索引:" + index);
|
||
return;
|
||
}
|
||
// 关闭上帝视角摄像机
|
||
godView.enabled=false;
|
||
isGodView = false;
|
||
|
||
// 禁用当前激活的摄像机(如果在角色视角)
|
||
if (currentCameraIndex >= 0)
|
||
{
|
||
playerCameras[index2].enabled=false;
|
||
}
|
||
|
||
// 激活指定索引的摄像机
|
||
currentCameraIndex = index;
|
||
playerCameras[currentCameraIndex].enabled=true;
|
||
}
|
||
// 用于外部传入参数来设置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) + "视角");
|
||
|
||
|
||
SwitchToCamera(selectedIndex);//转入视角
|
||
Game.uiManager.CloseUI("Panel1_2");
|
||
Game.uiManager.ShowUI<Image>("Panel1_3");
|
||
}
|
||
|
||
void EndBtn()
|
||
{
|
||
//结束演习
|
||
Game.uiManager.ShowUI<Image>("PoPhost");
|
||
}
|
||
}
|