玉盘珍馐值万钱 缝缝补补又三天 长音已改逾卅遍
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class loadDetailsPrefab : MonoBehaviour
|
||||
{
|
||||
public static loadDetailsPrefab Instance { get; private set; }
|
||||
|
||||
[Header("Details Prefab")]
|
||||
public GameObject detailsPrefab; // 详细信息prefab,必须有detailsPrefabController组件
|
||||
public GameObject detailsParent; // 详细信息prefab的父物体
|
||||
public GameObject detailsPrefab;
|
||||
public GameObject detailsParent;
|
||||
|
||||
[Header("Settings")]
|
||||
public float hoverDelay = 0.5f; // 鼠标悬停多少秒后展示prefab
|
||||
public Vector2 offset = new Vector2(0, -50); // 偏移量(像素)
|
||||
public float hoverDelay = 0.5f;
|
||||
|
||||
[Tooltip("偏移量相对于屏幕宽高的百分比 (0.01 = 1%)")]
|
||||
public Vector2 offsetPercent = new Vector2(0f, -0.03f);
|
||||
|
||||
[Tooltip("最小偏移像素值")]
|
||||
public Vector2 minOffsetPixels = new Vector2(0f, -50f);
|
||||
|
||||
private GameObject currentDetailsInstance;
|
||||
private detailsPrefabController currentController;
|
||||
@@ -20,18 +24,18 @@ public class loadDetailsPrefab : MonoBehaviour
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance == null) Instance = this;
|
||||
else Destroy(gameObject);
|
||||
if (Instance == null)
|
||||
Instance = this;
|
||||
else
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
// 显示详细信息
|
||||
public void ShowDetails(string type, string name, string status, string description, Vector3 position)
|
||||
{
|
||||
HideDetails(); // 先隐藏之前的
|
||||
HideDetails();
|
||||
|
||||
if (detailsPrefab == null) return;
|
||||
|
||||
// 启动协程延迟显示
|
||||
showCoroutine = StartCoroutine(ShowDetailsDelayed(type, name, status, description, position));
|
||||
}
|
||||
|
||||
@@ -41,6 +45,7 @@ public class loadDetailsPrefab : MonoBehaviour
|
||||
|
||||
Transform parent = detailsParent != null ? detailsParent.transform : transform;
|
||||
currentDetailsInstance = Instantiate(detailsPrefab, parent);
|
||||
|
||||
currentController = currentDetailsInstance.GetComponent<detailsPrefabController>();
|
||||
if (currentController == null)
|
||||
{
|
||||
@@ -49,13 +54,11 @@ public class loadDetailsPrefab : MonoBehaviour
|
||||
yield break;
|
||||
}
|
||||
|
||||
// 设置文本
|
||||
if (currentController.item_nameText != null) currentController.item_nameText.text = name;
|
||||
if (currentController.item_typeText != null) currentController.item_typeText.text = type;
|
||||
if (currentController.item_statusText != null) currentController.item_statusText.text = status;
|
||||
if (currentController.item_detailInformationText != null) currentController.item_detailInformationText.text = description;
|
||||
|
||||
// 设置状态文本颜色
|
||||
if (currentController.item_statusText != null)
|
||||
{
|
||||
Color statusColor;
|
||||
@@ -67,40 +70,12 @@ public class loadDetailsPrefab : MonoBehaviour
|
||||
{
|
||||
statusColor = currentController.unavailableColor;
|
||||
}
|
||||
statusColor.a = 1f; // 确保透明度为1,使其可见
|
||||
statusColor.a = 1f;
|
||||
currentController.item_statusText.color = statusColor;
|
||||
}
|
||||
|
||||
// 设置位置并检查边界
|
||||
RectTransform rt = currentDetailsInstance.GetComponent<RectTransform>();
|
||||
if (rt != null)
|
||||
{
|
||||
Vector3 pos = position;
|
||||
// 获取Canvas的scaleFactor来调整偏移量
|
||||
float scaleFactor = 1f;
|
||||
Transform parentTransform = detailsParent != null ? detailsParent.transform : transform;
|
||||
Canvas canvas = parentTransform.GetComponentInParent<Canvas>();
|
||||
if (canvas != null)
|
||||
{
|
||||
scaleFactor = canvas.scaleFactor;
|
||||
}
|
||||
pos.x += offset.x / scaleFactor;
|
||||
pos.y += offset.y / scaleFactor;
|
||||
SetupPosition(position);
|
||||
|
||||
float width = rt.rect.width;
|
||||
float height = rt.rect.height;
|
||||
|
||||
// 检查边界并调整
|
||||
if (pos.x + width > Screen.width) pos.x = Screen.width - width; // 超出右边界,左移
|
||||
if (pos.x < 0) pos.x = 0; // 超出左边界,右移
|
||||
|
||||
if (pos.y < 0) pos.y = position.y + height; // 下方空间不足,显示在上方(框底部在鼠标位置上方)
|
||||
if (pos.y + height > Screen.height) pos.y = Screen.height - height; // 超出上边界,下移
|
||||
|
||||
rt.position = pos;
|
||||
}
|
||||
|
||||
// 设置不阻挡射线以避免闪烁
|
||||
CanvasGroup cg = currentDetailsInstance.GetComponent<CanvasGroup>();
|
||||
if (cg == null) cg = currentDetailsInstance.AddComponent<CanvasGroup>();
|
||||
cg.blocksRaycasts = false;
|
||||
@@ -108,7 +83,117 @@ public class loadDetailsPrefab : MonoBehaviour
|
||||
currentDetailsInstance.SetActive(true);
|
||||
}
|
||||
|
||||
// 隐藏详细信息
|
||||
private void SetupPosition(Vector3 position)
|
||||
{
|
||||
RectTransform rt = currentDetailsInstance.GetComponent<RectTransform>();
|
||||
if (rt == null) return;
|
||||
|
||||
Canvas canvas = GetCanvas();
|
||||
if (canvas == null) return;
|
||||
|
||||
RectTransform canvasRect = canvas.GetComponent<RectTransform>();
|
||||
|
||||
// 计算动态偏移量
|
||||
Vector2 dynamicOffset = CalculateDynamicOffset();
|
||||
|
||||
Vector2 screenPos = new Vector2(position.x, position.y) + dynamicOffset;
|
||||
|
||||
if (canvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.worldCamera != null)
|
||||
{
|
||||
Vector2 localPoint;
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
||||
canvasRect,
|
||||
screenPos,
|
||||
canvas.worldCamera,
|
||||
out localPoint))
|
||||
{
|
||||
rt.anchoredPosition = localPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt.position = screenPos;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float scaleFactor = canvas.scaleFactor;
|
||||
Vector3 worldPos = new Vector3(
|
||||
screenPos.x / scaleFactor,
|
||||
screenPos.y / scaleFactor,
|
||||
0
|
||||
);
|
||||
rt.position = worldPos;
|
||||
}
|
||||
|
||||
AdjustPositionWithinCanvas(rt, canvas);
|
||||
}
|
||||
|
||||
private Vector2 CalculateDynamicOffset()
|
||||
{
|
||||
// 获取当前屏幕分辨率
|
||||
float screenWidth = Screen.width;
|
||||
float screenHeight = Screen.height;
|
||||
|
||||
// 计算基于百分比的偏移
|
||||
Vector2 percentOffset = new Vector2(
|
||||
screenWidth * offsetPercent.x,
|
||||
screenHeight * offsetPercent.y
|
||||
);
|
||||
|
||||
// 应用最小像素偏移限制
|
||||
Vector2 finalOffset = new Vector2(
|
||||
Mathf.Abs(percentOffset.x) > Mathf.Abs(minOffsetPixels.x) ? percentOffset.x : minOffsetPixels.x,
|
||||
Mathf.Abs(percentOffset.y) > Mathf.Abs(minOffsetPixels.y) ? percentOffset.y : minOffsetPixels.y
|
||||
);
|
||||
|
||||
return finalOffset;
|
||||
}
|
||||
|
||||
private Canvas GetCanvas()
|
||||
{
|
||||
if (detailsParent != null)
|
||||
{
|
||||
Canvas parentCanvas = detailsParent.GetComponentInParent<Canvas>();
|
||||
if (parentCanvas != null) return parentCanvas;
|
||||
}
|
||||
|
||||
Canvas selfCanvas = GetComponentInParent<Canvas>();
|
||||
if (selfCanvas != null) return selfCanvas;
|
||||
|
||||
return FindObjectOfType<Canvas>();
|
||||
}
|
||||
|
||||
private void AdjustPositionWithinCanvas(RectTransform rt, Canvas canvas)
|
||||
{
|
||||
RectTransform canvasRect = canvas.GetComponent<RectTransform>();
|
||||
Vector3[] rtCorners = new Vector3[4];
|
||||
Vector3[] canvasCorners = new Vector3[4];
|
||||
|
||||
rt.GetWorldCorners(rtCorners);
|
||||
canvasRect.GetWorldCorners(canvasCorners);
|
||||
|
||||
float canvasLeft = canvasCorners[0].x;
|
||||
float canvasRight = canvasCorners[2].x;
|
||||
float canvasBottom = canvasCorners[0].y;
|
||||
float canvasTop = canvasCorners[2].y;
|
||||
|
||||
Vector3 adjustedPos = rt.position;
|
||||
|
||||
// 调整水平位置
|
||||
if (rtCorners[2].x > canvasRight)
|
||||
adjustedPos.x -= (rtCorners[2].x - canvasRight);
|
||||
if (rtCorners[0].x < canvasLeft)
|
||||
adjustedPos.x += (canvasLeft - rtCorners[0].x);
|
||||
|
||||
// 调整垂直位置
|
||||
if (rtCorners[2].y > canvasTop)
|
||||
adjustedPos.y -= (rtCorners[2].y - canvasTop);
|
||||
if (rtCorners[0].y < canvasBottom)
|
||||
adjustedPos.y += (canvasBottom - rtCorners[0].y);
|
||||
|
||||
rt.position = adjustedPos;
|
||||
}
|
||||
|
||||
public void HideDetails()
|
||||
{
|
||||
if (showCoroutine != null)
|
||||
@@ -124,4 +209,4 @@ public class loadDetailsPrefab : MonoBehaviour
|
||||
currentController = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user