Files
MuHua-Core/Packages/LabelFollow/Runtime/LabelController.cs
T
2025-02-27 17:40:31 +08:00

38 lines
888 B
C#

using UnityEngine;
namespace MuHua
{
public class LabelController : MonoBehaviour
{
public static LabelController Instance { get; private set; }
public Canvas canvas; // 包含标签的Canvas
public GameObject labelPrefab; // 标签预制体
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public static GameObject CreateLabel(Transform target, Vector3 offset)
{
return CreateLabel(target, Instance.labelPrefab, offset);
}
public static GameObject CreateLabel(Transform target, GameObject labelPrefab, Vector3 offset)
{
GameObject labelObject = Instantiate(labelPrefab, Instance.canvas.transform);
LabelFollower followObjectLabel = labelObject.GetComponent<LabelFollower>();
followObjectLabel.target = target;
followObjectLabel.offset = offset;
return labelObject;
}
}
}