fix:1
This commit is contained in:
118
Assets/Scripts/Tools/IconFollow3D.cs
Normal file
118
Assets/Scripts/Tools/IconFollow3D.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using DG.Tweening;
|
||||
|
||||
/// <summary>
|
||||
/// 3DUI跟随相机
|
||||
/// </summary>
|
||||
public class IconFollow3D : MonoBehaviour
|
||||
{
|
||||
public enum LookAtTye
|
||||
{
|
||||
//看向相机坐标
|
||||
CameraPos,
|
||||
//看向相机平面
|
||||
CameraPlane
|
||||
}
|
||||
|
||||
public enum RotateAxis
|
||||
{
|
||||
Any,
|
||||
X,
|
||||
Y,
|
||||
Z
|
||||
}
|
||||
|
||||
public LookAtTye lookAtTye = LookAtTye.CameraPlane;
|
||||
public RotateAxis rotateAxis = RotateAxis.Any;
|
||||
|
||||
private bool isFollow;
|
||||
private Transform mainCamera;
|
||||
private Transform followTarget;
|
||||
|
||||
private Vector3 lookAtPos;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
isFollow = true;
|
||||
mainCamera = Camera.main.transform;
|
||||
}
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (isFollow)
|
||||
{
|
||||
if (followTarget != null)
|
||||
{
|
||||
transform.position = followTarget.position;
|
||||
}
|
||||
switch (lookAtTye)
|
||||
{
|
||||
case LookAtTye.CameraPos:
|
||||
lookAtPos = mainCamera.position;
|
||||
break;
|
||||
case LookAtTye.CameraPlane:
|
||||
lookAtPos = transform.position - mainCamera.forward;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (rotateAxis)
|
||||
{
|
||||
case (RotateAxis.Any):
|
||||
break;
|
||||
case (RotateAxis.X):
|
||||
lookAtPos.x = transform.position.x;
|
||||
break;
|
||||
case (RotateAxis.Y):
|
||||
lookAtPos.y = transform.position.y;
|
||||
break;
|
||||
case (RotateAxis.Z):
|
||||
lookAtPos.z = transform.position.z;
|
||||
break;
|
||||
}
|
||||
transform.LookAt(lookAtPos);
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
isFollow = true;
|
||||
}
|
||||
|
||||
void OnBecameVisible()
|
||||
{
|
||||
isFollow = true;
|
||||
}
|
||||
|
||||
void OnBecameInvisible()
|
||||
{
|
||||
isFollow = false;
|
||||
}
|
||||
|
||||
//设置跟随相机
|
||||
public void SetCamera(Transform followCamera)
|
||||
{
|
||||
mainCamera = followCamera;
|
||||
}
|
||||
|
||||
//设置Icon坐标
|
||||
public void SetIconPos(Vector3 pos)
|
||||
{
|
||||
transform.position = pos;
|
||||
}
|
||||
|
||||
//设置跟随对象
|
||||
public void SetFollowTarget(Transform target)
|
||||
{
|
||||
followTarget = target;
|
||||
if (target.Find("iconPos") != null)
|
||||
{
|
||||
followTarget = target.Find("iconPos");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user