127 lines
4.2 KiB
C#
127 lines
4.2 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;
|
||
|
||
public List<GameObject> target;//目标
|
||
|
||
// 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);
|
||
}
|
||
}
|
||
// 获取所有标签为 "Player" 的物体
|
||
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
|
||
|
||
// 遍历每个 Player,找到子物体中的摄像机
|
||
foreach (GameObject player in players)
|
||
{
|
||
target.Add(player.gameObject);
|
||
}
|
||
SetOptionCount(target.Count);
|
||
// 默认执行一次切换
|
||
OnToggleValueChanged(toggle.isOn);
|
||
|
||
}
|
||
// 切换到指定索引的摄像机
|
||
public void SwitchToCamera(int index)
|
||
{
|
||
|
||
isGodView = false;
|
||
// 将摄像机设为 target[index] 的子物体
|
||
godView.transform.SetParent(target[index].transform);
|
||
// 重置摄像机的位置和旋转
|
||
godView.transform.localPosition = (Vector3.zero+new Vector3(0, 1.5f, -1));
|
||
godView.transform.localRotation = Quaternion.identity;
|
||
|
||
// 让摄像机朝向父物体
|
||
godView.transform.LookAt(target[index].transform.position+ new Vector3(0, 1.5f,0));
|
||
|
||
}
|
||
// 用于外部传入参数来设置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.GetComponent<Camera>().orthographic = false;
|
||
SwitchToCamera(selectedIndex);//转入视角
|
||
Game.uiManager.CloseUI("Panel1_2");
|
||
Game.uiManager.ShowUI<Image>("Panel1_3");
|
||
}
|
||
|
||
void EndBtn()
|
||
{
|
||
//结束演习
|
||
Game.uiManager.ShowUI<Image>("PoPhost");
|
||
}
|
||
}
|