2023-04-03 11:04:31 +08:00

217 lines
7.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BF
{
[System.Serializable]
public struct NotchScreenNode
{
public GameObject gameObject;
public float adjustHeight;
}
[ExecuteInEditMode]
[DisallowMultipleComponent]
public class UIHelper : MonoBehaviour
{
public List<BaseSortingOrderHelper> EffectList = new List<BaseSortingOrderHelper>();
public List<NotchScreenNode> NotchScreenNodeList = new List<NotchScreenNode>();
private bool hasInitDefaultNotchScreenHeight = false;
public float PositionX { get; private set; }
public float PositionY { get; private set; }
public float PositionZ { get; private set; }
public void SetInit(bool init)
{
hasInitDefaultNotchScreenHeight = init;
}
public bool GetHasInitDefaultNotchScreenHeight()
{
return hasInitDefaultNotchScreenHeight;
}
public int GetNotchScreenNodeCount()
{
return NotchScreenNodeList.Count;
}
public GameObject GetNotchScreenNodeGameObject(int index)
{
return NotchScreenNodeList[index].gameObject;
}
public float GetNotchScreenNodeAdjustHeight(int index)
{
return NotchScreenNodeList[index].adjustHeight;
}
public bool GetIsHaveNotchScreenNodeGameObject(int index)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
return NotchScreenNodeList[index].gameObject != null;
}
return false;
}
public void CacheAnchoredPosition(int index)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
var anchoredPosition = rectTransform.anchoredPosition;
PositionX = anchoredPosition.x;
PositionY = anchoredPosition.y;
}
else
{
PositionX = 0.0f;
PositionY = 0.0f;
}
}
public void CacheAnchorMin(int index)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
var anchorMin = rectTransform.anchorMin;
PositionX = anchorMin.x;
PositionY = anchorMin.y;
}
else
{
PositionX = 0.0f;
PositionY = 0.0f;
}
}
public void CacheAnchorMax(int index)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
var anchorMax = rectTransform.anchorMax;
PositionX = anchorMax.x;
PositionY = anchorMax.y;
}
else
{
PositionX = 0.0f;
PositionY = 0.0f;
}
}
public void CacheOffsetMax(int index)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
var offsetMax = rectTransform.offsetMax;
PositionX = offsetMax.x;
PositionY = offsetMax.y;
}
else
{
PositionX = 0.0f;
PositionY = 0.0f;
}
}
public void CacheOffsetMin(int index)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
var offsetMin = rectTransform.offsetMin;
PositionX = offsetMin.x;
PositionY = offsetMin.y;
}
else
{
PositionX = 0.0f;
PositionY = 0.0f;
}
}
public void SetOffsetMax(int index, float x, float y)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
rectTransform.offsetMax = new Vector2(x, y);
}
}
public void SetOffsetMin(int index, float x, float y)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
rectTransform.offsetMin = new Vector2(x, y);
}
}
public void SetAnchoredPosition(int index, float x, float y)
{
if (index >= 0 && index < NotchScreenNodeList.Count)
{
RectTransform rectTransform = NotchScreenNodeList[index].gameObject.transform as RectTransform;
rectTransform.anchoredPosition = new Vector2(x, y);
}
}
/// <summary>
/// 触发CS端刘海屏适配,仅用于Editor模式下刷新显示
/// </summary>
// public void FitNotchScreen()
// {
// for (int i = 0;i < NotchScreenNodeList.Count; i ++)
// {
// GameObject node = NotchScreenNodeList[i].gameObject;
// float adjustHeight = NotchScreenNodeList[i].adjustHeight;
// if (node != null)
// {
// // 获得初始位置
// if (!hasInitDefaultNotchScreenHeight)
// {
// Vector2 anchoredPosition = node.GetComponent<RectTransform>().anchoredPosition;
// InitAnchorPosition(i, anchoredPosition.x, anchoredPosition.y);
// }
// // 得到偏移值
// float height = - SafeAreaManager.GetNotchScreenHeight();
// float adjust = 0; // SafeAreaManager.GetAdjustHeight(adjustHeight);
// // 设置偏移值
// Vector2 defaultAnchordPosition = this.GetDefaultAnchordPosition(i);
// node.GetComponent<RectTransform>().anchoredPosition = new Vector2(defaultAnchordPosition.x, defaultAnchordPosition.y + height + adjust);
// Debug.Log("屏幕适配 FitNotchScreen -- GameObject:" + node.name + " 原始Anchor:" + defaultAnchordPosition + " 刘海屏偏移:" + height + " Adjust偏移(暂无效):" + adjust);
// }
// }
// this.SetInit(true);
// }
public void SetSortingOrder(int uiOrder)
{
int len = EffectList.Count;
for (int i = 0; i < len; i++)
{
EffectList[i].SetUIOrder(uiOrder);
}
}
public void AddEffect(BaseSortingOrderHelper helper, int uiOrder, int order)
{
helper.SetSortingOrder(uiOrder, order);
EffectList.Add(helper);
}
public void RemoveEffect(BaseSortingOrderHelper helper)
{
EffectList.Remove(helper);
}
void OnDestroy()
{
EffectList.Clear();
EffectList = null;
}
}
}