fix:代码提交
This commit is contained in:
68
Assets/Scripts/Tools/IconFollow2D.cs
Normal file
68
Assets/Scripts/Tools/IconFollow2D.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using DG.Tweening;
|
||||
using UnityEngine.EventSystems; // 添加这一行
|
||||
public class IconFollow2D : MonoBehaviour
|
||||
{
|
||||
//目标对象
|
||||
public Transform target;
|
||||
//目标位置
|
||||
public Vector3 targetPos;
|
||||
|
||||
|
||||
|
||||
private Camera mainCamera;
|
||||
private CanvasGroup canvas;
|
||||
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
mainCamera = Camera.main;
|
||||
canvas = GetComponent<CanvasGroup>();
|
||||
if (canvas == null)
|
||||
{
|
||||
canvas = gameObject.AddComponent<CanvasGroup>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
if (mainCamera != null)
|
||||
{
|
||||
if (target != null)
|
||||
{
|
||||
Vector3 pos = mainCamera.WorldToScreenPoint(target.position);
|
||||
pos = new Vector3(pos.x, pos.y, pos.z);
|
||||
transform.position = pos;
|
||||
}
|
||||
else if (targetPos != null)
|
||||
{
|
||||
Vector3 pos = mainCamera.WorldToScreenPoint(targetPos);
|
||||
pos = new Vector3(pos.x, pos.y, pos.z);
|
||||
transform.position = pos;
|
||||
}
|
||||
if (Time.frameCount % 5 == 0)
|
||||
{
|
||||
ResetAlpha();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ResetAlpha()
|
||||
{
|
||||
if (transform.position.z > 0f && canvas.alpha < 0.1f)
|
||||
{
|
||||
canvas.alpha = 1f;
|
||||
}
|
||||
else if (transform.position.z < 0f && canvas.alpha > 0.9f)
|
||||
{
|
||||
canvas.alpha = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Tools/IconFollow2D.cs.meta
Normal file
11
Assets/Scripts/Tools/IconFollow2D.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e6616d2c1d0fdef45b0bcab0d086666d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
Assets/Scripts/Tools/IconFollow3D.cs.meta
Normal file
11
Assets/Scripts/Tools/IconFollow3D.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aed7fea4c9a968744860ad541ef69f0b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user