GQ_Communicate/GQ_TongXin/Assets/Scripts/HUD/StationInformation.cs

83 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//============================================================
//支持中文文件使用UTF-8编码
//@author YangHua
//@create 20230426
//@company Adam
//
//@description:
//============================================================
namespace HUD
{
public enum WorldOrLocal
{
World,
Local
}
public class StationInformation : MonoBehaviour
{
//public float offsetY = 1f;
public WorldOrLocal worldOrLocal = WorldOrLocal.World;
public bool lockXZ = false;
public UnityEvent onClick;
private Transform lookAtTarget;
public void SetTarget(Transform target)
{
lookAtTarget = target;
}
public void Init(Transform parent, Bounds b)
{
transform.SetParent(parent);
transform.localPosition = Vector3.zero;
Vector3 pos = b.center;
Vector3 extentsPos = b.extents;
if (worldOrLocal == WorldOrLocal.Local)
{
//transform.localEulerAngles = Vector3.zero;
Vector3 localPos = transform.InverseTransformPoint(pos);
transform.localPosition = new Vector3(localPos.x, localPos.y + extentsPos.y, localPos.z);
}
else if (worldOrLocal == WorldOrLocal.World)
{
Vector3 localPos = transform.InverseTransformPoint(pos);
Vector3 worldPos = transform.TransformPoint(localPos);
transform.position = new Vector3(worldPos.x, worldPos.y + extentsPos.y + 1f, worldPos.z);
}
else
{
}
}
private void Update()
{
if (lookAtTarget != null)
{
Vector3 pos;
if (lockXZ)
{
pos = new Vector3(lookAtTarget.localPosition.x, 0, lookAtTarget.localPosition.z);
}
else
{
pos = lookAtTarget.localPosition;
}
transform.LookAt(pos);
}
}
private void OnMouseDown()
{
onClick?.Invoke();
}
}
}