34 lines
913 B
C#
34 lines
913 B
C#
using UnityEngine;
|
|
|
|
public class FaceCamera : MonoBehaviour
|
|
{
|
|
public Camera mainCamera;
|
|
|
|
// 是否启用UI与屏幕XY轴对齐功能
|
|
public bool alignWithScreenXY = true;
|
|
|
|
void Start()
|
|
{
|
|
// 如果没有手动设置摄像机,自动获取主摄像机
|
|
if (mainCamera == null)
|
|
{
|
|
mainCamera = Camera.main;
|
|
}
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
// 使3D UI面向摄像机
|
|
transform.LookAt(transform.position + mainCamera.transform.rotation * Vector3.forward,
|
|
mainCamera.transform.rotation * Vector3.up);
|
|
|
|
// 如果启用了UI与屏幕XY轴对齐功能
|
|
if (alignWithScreenXY)
|
|
{
|
|
transform.rotation = Quaternion.Euler(mainCamera.transform.rotation.eulerAngles.x,
|
|
mainCamera.transform.rotation.eulerAngles.y,
|
|
0f);
|
|
}
|
|
}
|
|
}
|