135 lines
3.7 KiB
C#
135 lines
3.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class esPrefab : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
public Image esIcon;
|
|
public Text esName;
|
|
|
|
private int skillGroupId;
|
|
private GameObject descriptionPrefab;
|
|
private RectTransform descriptionRoot;
|
|
private GameObject spawnedDescription;
|
|
private RectTransform rectTransform;
|
|
private Canvas rootCanvas;
|
|
private Camera uiCamera;
|
|
private Vector2 descriptionOffset = new Vector2(20f, 30f);
|
|
|
|
private void Awake()
|
|
{
|
|
rectTransform = transform as RectTransform;
|
|
rootCanvas = GetComponentInParent<Canvas>();
|
|
if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay)
|
|
{
|
|
uiCamera = rootCanvas.worldCamera;
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
DestroyDescription();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
DestroyDescription();
|
|
}
|
|
|
|
public void BindSkillGroup(int groupId, string displayName, Sprite icon, GameObject desPrefab, RectTransform ownerRoot, Vector2 offset)
|
|
{
|
|
skillGroupId = Mathf.Max(0, groupId);
|
|
descriptionPrefab = desPrefab;
|
|
descriptionRoot = ownerRoot;
|
|
descriptionOffset = offset;
|
|
|
|
if (esName != null)
|
|
{
|
|
esName.text = displayName ?? string.Empty;
|
|
}
|
|
|
|
if (esIcon != null)
|
|
{
|
|
esIcon.sprite = icon;
|
|
esIcon.enabled = icon != null;
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
ShowDescription();
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
DestroyDescription();
|
|
}
|
|
|
|
private void ShowDescription()
|
|
{
|
|
if (spawnedDescription != null || descriptionPrefab == null || descriptionRoot == null || skillGroupId <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
SkillGroup skillGroup = EquipmentSkillGroupLibrary.ResolveSkillGroup(skillGroupId);
|
|
if (skillGroup == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
spawnedDescription = Instantiate(descriptionPrefab, descriptionRoot);
|
|
spawnedDescription.SetActive(true);
|
|
spawnedDescription.transform.SetAsLastSibling();
|
|
|
|
esDesPrefab description = spawnedDescription.GetComponent<esDesPrefab>();
|
|
if (description != null)
|
|
{
|
|
description.Bind(skillGroup.groupName, skillGroup.groupIcon, skillGroup.skillDescriptionsText);
|
|
}
|
|
|
|
PositionDescription(spawnedDescription.transform as RectTransform);
|
|
}
|
|
|
|
private void PositionDescription(RectTransform descriptionRect)
|
|
{
|
|
if (descriptionRect == null || rectTransform == null || descriptionRoot == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Vector3[] corners = new Vector3[4];
|
|
rectTransform.GetWorldCorners(corners);
|
|
Vector3 rightCenterWorld = (corners[2] + corners[3]) * 0.5f;
|
|
Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, rightCenterWorld);
|
|
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(descriptionRoot, screenPoint, uiCamera, out Vector2 localPoint))
|
|
{
|
|
return;
|
|
}
|
|
|
|
descriptionRect.anchorMin = new Vector2(0f, 1f);
|
|
descriptionRect.anchorMax = new Vector2(0f, 1f);
|
|
descriptionRect.pivot = new Vector2(0f, 1f);
|
|
descriptionRect.anchoredPosition = localPoint + descriptionOffset;
|
|
}
|
|
|
|
private void DestroyDescription()
|
|
{
|
|
if (spawnedDescription == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Application.isPlaying)
|
|
{
|
|
Destroy(spawnedDescription);
|
|
}
|
|
else
|
|
{
|
|
DestroyImmediate(spawnedDescription);
|
|
}
|
|
|
|
spawnedDescription = null;
|
|
}
|
|
}
|