2023-06-05 19:57:17 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using BITKit;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit
|
|
|
|
{
|
|
|
|
public class WorldToScreenPoint : MonoBehaviour
|
|
|
|
{
|
|
|
|
[Header(Constant.Header.Settings)]
|
2023-08-11 23:57:37 +08:00
|
|
|
[SerializeField] private Transform root;
|
|
|
|
[SerializeField] private Optional<Vector3> localOffset;
|
|
|
|
[SerializeField] private Optional<Vector3> worldOffset;
|
|
|
|
[SerializeField] private Optional<float> activeDistance;
|
|
|
|
|
2023-06-05 19:57:17 +08:00
|
|
|
[Header(Constant.Header.Events)]
|
|
|
|
[SerializeField] private UnityEvent<bool> setHeaderEnabled;
|
|
|
|
[SerializeField] private UnityEvent<Vector3> setHeaderPosition;
|
2023-08-11 23:57:37 +08:00
|
|
|
|
|
|
|
private Transform cameraTrans;
|
|
|
|
private void Start()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
if (Camera.main != null) cameraTrans = Camera.main.transform;
|
|
|
|
}
|
2023-08-11 23:57:37 +08:00
|
|
|
private void Update()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-08-11 23:57:37 +08:00
|
|
|
var position = root.position;
|
|
|
|
if (localOffset.Allow)
|
|
|
|
{
|
|
|
|
position += root.rotation * localOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worldOffset.Allow)
|
|
|
|
{
|
|
|
|
position+=worldOffset;
|
|
|
|
}
|
2023-06-05 19:57:17 +08:00
|
|
|
|
|
|
|
setHeaderPosition.Invoke(position);
|
|
|
|
|
2023-06-07 18:38:07 +08:00
|
|
|
if (activeDistance.Allow)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
var distance = Vector3.Distance(position, cameraTrans.position);
|
|
|
|
var _enabled = distance < activeDistance;
|
|
|
|
setHeaderEnabled.Invoke(_enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|