_xiaofang/xiaofang/Assets/Res/gsj/scripts/Panel3.cs
2024-12-28 21:42:16 +08:00

129 lines
4.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.GraphicsBuffer;
public class Panel3 : MonoBehaviour
{
private Button cancel;
private Button online;//目前在线离线状况弹窗按钮
private Button follow;//跟随查按钮
private Dropdown dropdown;
private int optionCount; // 用于存储传入的确定Dropdown选项数量的参数
private GameObject peopleposition;
private Camera godView;
public List<GameObject> playertarget; // 存储每个角色
private int currentCameraIndex = -1;
private bool isGodView = false; // 是否当前为上帝视角
private GameObject[] players;
private Vector3 startposition = new Vector3(6, 60, -25);
private Vector3 startrotation = new Vector3(90, 0, -90);
public GameObject panel1_22;
public GameObject panel1_33;
// Start is called before the first frame update
void Start()
{
cancel=transform.Find("left/btn_enddrill").GetComponent<Button>();
cancel.onClick.AddListener(OnCancelBtn);
online=transform.Find("onlineBtn").GetComponent <Button>();
online.onClick.AddListener(OnClickOnlineBtn);
dropdown=transform.Find("left/Dropdown").GetComponent<Dropdown>();
follow = transform.Find("left/Follow").GetComponent<Button>();
follow.onClick.AddListener(FollowBtn);
peopleposition = GameObject.Find("HostCanvas/peopleposition").gameObject;
peopleposition.SetActive(false);
godView=GameObject.Find("TopCamera").GetComponent<Camera>();
//godView.GetComponent<Camera>().orthographic = false;
// 获取所有标签为 "Player" 的物体
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject player in players)
{
if (player != null)
{
playertarget.Add(player);
}
}
SetOptionCount(playertarget.Count);
currentCameraIndex = Panel2.instance.currentCameraIndex;
dropdown.value= currentCameraIndex;
}
// 切换到指定索引的摄像机
public void SwitchToCamera(int index)
{
if (index < 0 || index >= playertarget.Count)
{
Debug.LogWarning("无效的摄像机索引:" + index);
return;
}
isGodView = false;
// 将摄像机设为 target[index] 的子物体
godView.transform.SetParent(playertarget[index].transform);
// 重置摄像机的位置和旋转
godView.transform.localPosition = Vector3.zero;
godView.transform.localRotation = Quaternion.identity;
// 让摄像机朝向父物体
godView.transform.LookAt(playertarget[index].transform);
}
// 切换到上帝视角
public void SwitchToGodView()
{
godView.GetComponent<Camera>().orthographic = false ;
isGodView = true;
currentCameraIndex = -1;
godView.transform.position = startposition;
godView.transform.eulerAngles=startrotation;
}
// 用于外部传入参数来设置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()
{
}
void OnCancelBtn()
{
peopleposition.gameObject.SetActive(true);
//返回上帝视角界面
SwitchToGodView();
Debug.LogError(1111111111111);
// Camera.main.enabled = true;
panel1_22.gameObject.SetActive(true);
//Game.uiManager.ShowUI<Image>("Panel1_2");
panel1_33.gameObject.SetActive(false);
//Game.uiManager.CloseUI("Panel1_3");
}
void OnClickOnlineBtn()
{
//人员在线状态,弹出窗口
Game.uiManager.ShowUI<Image>("Panel");
}
void FollowBtn()
{
//转化角色视角
int selectedIndex = dropdown.value;
Debug.Log("进入角色" + (selectedIndex+1) + "视角");
SwitchToCamera(selectedIndex);
//godView.GetComponent<Camera>().orthographic = false;
}
}