71 lines
1.6 KiB
C#
71 lines
1.6 KiB
C#
using UnityEngine;
|
|
|
|
public class UIFollowTarget_Ino : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 3D target that this object will be positioned above.
|
|
/// </summary>
|
|
public Transform target;
|
|
public bool isTarget;
|
|
|
|
/// <summary>
|
|
/// UI 위치 오프셋, z는 0
|
|
/// </summary>
|
|
public Vector3 offSet;
|
|
|
|
/// <summary>
|
|
/// UI camera to use.
|
|
/// </summary>
|
|
public Camera uiCamera;
|
|
bool isUICam;
|
|
|
|
Transform mTrans;
|
|
int mIsVisible = -1;
|
|
|
|
/// <summary>
|
|
/// Whether the target is currently visible or not.
|
|
/// </summary>
|
|
|
|
public bool isVisible { get { return mIsVisible == 1; } }
|
|
public bool runUpdate { get; set; }
|
|
|
|
/// <summary>
|
|
/// Cache the transform;
|
|
/// </summary>
|
|
|
|
void Awake() { mTrans = transform; }
|
|
|
|
/// <summary>
|
|
/// Update the position of the HUD object every frame such that is position correctly over top of its real world object.
|
|
/// </summary>
|
|
|
|
void Update()
|
|
{
|
|
if (runUpdate)
|
|
{
|
|
if (!isUICam)
|
|
{
|
|
uiCamera = NewGameUI.Ins.UICamera;
|
|
isUICam = uiCamera;
|
|
}
|
|
if (isTarget && isUICam)
|
|
{
|
|
Vector3 pos = Camera.main.WorldToViewportPoint(target.position);
|
|
|
|
// Determine the visibility and the target alpha
|
|
//int isVisible = (gameCamera.orthographic || pos.z > 0f) && (pos.x > 0f && pos.x < 1f && pos.y > 0f && pos.y < 1f) ? 1 : 0;
|
|
//bool vis = (isVisible == 1);
|
|
|
|
// If visible, update the position
|
|
//if (vis)
|
|
{
|
|
pos = mTrans.parent.InverseTransformPoint(uiCamera.ViewportToWorldPoint(pos));
|
|
pos.z = 0f;
|
|
mTrans.localPosition = pos + offSet;
|
|
}
|
|
}
|
|
else
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
} |