GQ_Communicate/GQ_TongXin/Assets/script/报警/AlarmUI.cs

170 lines
5.6 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class AlarmUI : MonoBehaviour, IPointerClickHandler
{
public AlarmApparatus alarmApparatus;
public TextMeshProUGUI textMeshPro;
public Image expand_image;
public Button close_bt;
public GameObject preform;
public RectTransform Content;
public Sprite Alarm1;
public Sprite Alarm2;
public Sprite Alarm3;
public Image Alarm_img;
public TextMeshProUGUI Alarm_type;
public TextMeshProUGUI Alarm_name;
public TextMeshProUGUI Alarm_time;
public Canvas canvas;
public void OnPointerClick(PointerEventData eventData)
{
GameObject go;
if (canvas.transform.childCount == 0)
{
go = Instantiate(expand_image.gameObject);
go.transform.SetParent(canvas.transform);
go.transform.rotation = Quaternion.identity;
close_bt = canvas.transform.GetChild(0).Find("关闭").GetComponent<Button>();
close_bt.onClick.RemoveAllListeners();
close_bt.onClick.AddListener(() =>
{
canvas.transform.GetChild(0).gameObject.SetActive(false);
});
}
else
{
go = canvas.transform.GetChild(0).gameObject;
}
var worldPosition = expand_image.transform.position;
var list = alarmApparatus.transform.root.GetComponent<CabinetUIManager>().cabinet_ui_base_list;
var v = Array.FindAll(list.ToArray(), (itme) =>
{
return (itme.name == "报警");
});
for (int i = 0; i < v.Length; i++)
{
v[i].gameObject.GetComponent<AlarmApparatus>().alarmUI.canvas.gameObject.SetActive(false);
}
canvas.gameObject.SetActive(true);
// 获取Canvas上的RectTransform组件
RectTransform canvasRectTransform = canvas.GetComponent<RectTransform>();
// 世界坐标转屏幕坐标
Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPosition);
// 屏幕坐标转Overlay坐标
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, screenPos, canvas.worldCamera, out Vector2 overlayPos);
//go.GetComponent<RectTransform>().position = new Vector3(overlayPos.x, overlayPos.y, 0);
go.GetComponent<RectTransform>().localPosition = new Vector3(overlayPos.x, overlayPos.y, 0);
go.GetComponent<RectTransform>().localScale = Vector3.one;
go.transform.rotation = Quaternion.identity;
go.SetActive(true);
for (int i = 0; i < go.transform.Find("Scroll View/Viewport/Content").childCount; i++)
{
var R = go.transform.Find("Scroll View/Viewport/Content").GetChild(i).GetComponent<RectTransform>();
R.position = Vector3.zero;
R.rotation = Quaternion.Euler(Vector3.zero);
R.localScale = Vector3.one;
}
}
void OnEnable()
{
}
void Start()
{
}
public void LoadShowInfo(int num)
{
try
{
if (alarmApparatus.alarmQueries == null)
return;
for (int i = 0; i < alarmApparatus.alarmQueries.Count; i++)
{
foreach (var item in alarmApparatus.alarmQueries[i].tmsAlarmLists)
{
Alarm_type.text = item.alarmReason;
var d = Array.Find(GameObject.FindObjectsOfType<DeviceQuery>(), x =>
{
return (x.deviceList.id == item.deviceId);
});
Alarm_name.text = d.deviceList.deviceName;
Alarm_time.text = item.alarmDate;
switch (item.alarmLevel)
{
case "1":
Alarm_img.sprite = Alarm1;
break;
case "2":
Alarm_img.sprite = Alarm2;
break;
case "3":
Alarm_img.sprite = Alarm3;
break;
default:
break;
}
Alarm_img.SetNativeSize();
GameObject go = Instantiate(preform);
go.transform.SetParent(Content.transform);
go.SetActive(true);
GameObject game;
try
{
game = alarmApparatus.transform.parent.Find(alarmApparatus.transform.parent.name).gameObject;
}
catch (Exception)
{
game = alarmApparatus.transform.parent.gameObject;
}
GameManager.Inst.ChangeMaterialColor(game, 0, new Color(1, 109f / 255, 0, 1));
}
}
Content.sizeDelta = new Vector2(0, Content.GetComponent<GridLayoutGroup>().cellSize.y * int.Parse(textMeshPro.text));
}
catch (Exception e)
{
Debug.Log(e.StackTrace + "\n" + e.Message);
}
}
void Update()
{
//if (!gameObject.activeSelf)
//{
// GameObject game;
// try
// {
// game = alarmApparatus.transform.parent.Find(alarmApparatus.transform.parent.name).gameObject;
// }
// catch (Exception)
// {
// game = alarmApparatus.transform.parent.gameObject;
// }
// GameManager.Inst.ChangeMaterialColor(game, 0, new Color(1, 1, 1, 1));
}
}