2024-08-16 16:33:47 +08:00
|
|
|
using PhxhSDK.Res;
|
2024-01-19 17:12:36 +08:00
|
|
|
using System;
|
2024-02-01 17:47:50 +08:00
|
|
|
using System.Collections.Generic;
|
2024-08-16 16:33:47 +08:00
|
|
|
using System.Linq;
|
2023-08-22 19:08:45 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
namespace PhxhSDK
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
public class CameraManager : Singlenton<CameraManager>, IInitable
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 主相机
|
|
|
|
|
/// </summary>
|
2023-08-22 19:08:45 +08:00
|
|
|
private Camera _mainCamera;
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 主相机切换回调
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<Camera> AfterSwitchMainCamera;
|
2024-08-16 14:47:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 主相机栈
|
|
|
|
|
/// </summary>
|
2024-02-01 17:47:50 +08:00
|
|
|
private Stack<Camera> _usedMainCameraStack = new();
|
2024-08-16 14:47:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 默认的主相机RenderTexture
|
|
|
|
|
/// </summary>
|
2024-02-01 17:47:50 +08:00
|
|
|
private RenderTexture _defaultRenderTarget;
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
#region 属性
|
2024-02-01 17:47:50 +08:00
|
|
|
public RenderTexture DefaultRenderTarget
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2024-02-01 17:47:50 +08:00
|
|
|
get => _defaultRenderTarget;
|
|
|
|
|
set
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2024-02-01 17:47:50 +08:00
|
|
|
if (_defaultRenderTarget != value)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2024-02-01 17:47:50 +08:00
|
|
|
_defaultRenderTarget = value;
|
2025-05-13 12:59:33 +08:00
|
|
|
if (_mainCamera != null)
|
|
|
|
|
_mainCamera.targetTexture = _defaultRenderTarget;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 主相机
|
|
|
|
|
/// </summary>
|
2024-02-01 17:47:50 +08:00
|
|
|
public Camera MainCamera => _mainCamera;
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// UI相机
|
|
|
|
|
/// </summary>
|
2023-08-22 19:08:45 +08:00
|
|
|
public Camera UICamera
|
|
|
|
|
{
|
|
|
|
|
get { return UIRoot.Instance.UICamera; }
|
|
|
|
|
}
|
2024-01-08 13:59:23 +08:00
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// UI Top相机
|
|
|
|
|
/// </summary>
|
2024-07-19 13:12:50 +08:00
|
|
|
public Camera UITopCamera
|
|
|
|
|
{
|
|
|
|
|
get { return UIRoot.Instance.UITopCamera; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 技能相机
|
|
|
|
|
/// </summary>
|
2024-01-08 13:59:23 +08:00
|
|
|
public Camera SkillCamera { get; private set; }
|
2025-05-19 19:01:02 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否正在移动
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool IsMainCameraMoving { get; set; }
|
2024-08-14 17:04:21 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
_mainCamera = Camera.main;
|
2025-05-21 13:13:15 +08:00
|
|
|
|
2025-05-23 17:12:16 +08:00
|
|
|
/*if (null == _mainCamera)
|
2025-05-21 13:13:15 +08:00
|
|
|
_mainCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
if (null == _mainCamera)
|
|
|
|
|
CreateCamera("Main Camera", "MainCamera");
|
|
|
|
|
|
|
|
|
|
if (null != _mainCamera)
|
2025-05-23 17:12:16 +08:00
|
|
|
UnityEngine.Object.DontDestroyOnLoad(MainCamera.gameObject);*/
|
2025-04-07 14:18:07 +08:00
|
|
|
|
|
|
|
|
var skillCameraObj = GameObject.Find("SkillCamera");
|
|
|
|
|
if(skillCameraObj!=null)
|
|
|
|
|
{
|
|
|
|
|
SkillCamera = GameObject.Find("SkillCamera").GetComponent<Camera>();
|
|
|
|
|
SkillCamera.gameObject.SetActive(false);
|
|
|
|
|
}
|
2024-08-14 17:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Release()
|
|
|
|
|
{
|
|
|
|
|
_usedMainCameraStack.Clear();
|
|
|
|
|
}
|
2024-01-08 13:59:23 +08:00
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 创建相机
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <param name="tag"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-19 15:00:19 +08:00
|
|
|
public Camera CreateCamera(string name, string tag = null)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
GameObject go = new(name);
|
|
|
|
|
Camera camera = go.AddComponent<Camera>();
|
|
|
|
|
if (null != tag)
|
2024-02-01 17:47:50 +08:00
|
|
|
camera.tag = tag;
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
return camera;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
2024-02-01 17:47:50 +08:00
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
public void DestroyCamera(Camera camera)
|
|
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
if (null != camera)
|
|
|
|
|
UnityEngine.Object.Destroy(camera.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 16:33:47 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 获取场景中的相机
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sceneHandle"></param>
|
|
|
|
|
/// <param name="camera"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool TryGetSceneCamera(SceneHandle sceneHandle, out Camera camera)
|
|
|
|
|
{
|
|
|
|
|
camera = null;
|
|
|
|
|
|
|
|
|
|
if (null == sceneHandle)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (null != sceneHandle.SceneCameraRoot)
|
|
|
|
|
{
|
|
|
|
|
camera = sceneHandle.SceneCameraRoot.Camera;
|
|
|
|
|
if (null != camera)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GameObject[] rootGameObjects = sceneHandle.Scene.GetRootGameObjects();
|
|
|
|
|
if (null == rootGameObjects || 0 >= rootGameObjects.Length)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
GameObject gameObject = rootGameObjects.FirstOrDefault(root => root.GetComponent<Camera>() != null);
|
|
|
|
|
if (null == gameObject)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
camera = gameObject.GetComponent<Camera>();
|
|
|
|
|
|
|
|
|
|
return null != camera;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭所有相机(忽略UI相机)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void CloseAllCameraIgnoreUI()
|
|
|
|
|
{
|
|
|
|
|
SetMainCameraActive(false);
|
|
|
|
|
UITopCamera.gameObject.SetActive(false);
|
|
|
|
|
SkillCamera.gameObject.SetActive(false);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-16 14:47:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 全部相机重新渲染
|
|
|
|
|
/// </summary>
|
2023-08-22 19:08:45 +08:00
|
|
|
public void ReRenderAll()
|
|
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
foreach (Camera c in Camera.allCameras)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
bool oldEnabled = c.enabled;
|
2023-08-22 19:08:45 +08:00
|
|
|
c.enabled = true;
|
|
|
|
|
c.Render();
|
|
|
|
|
c.enabled = oldEnabled;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 设置主相机的激活状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="isActive"></param>
|
|
|
|
|
public void SetMainCameraActive(bool isActive)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2025-05-07 18:55:01 +08:00
|
|
|
if(null == _mainCamera || null == _mainCamera.gameObject)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-05-21 13:13:15 +08:00
|
|
|
if (isActive)
|
|
|
|
|
_mainCamera.enabled = true;
|
|
|
|
|
|
2025-05-07 18:55:01 +08:00
|
|
|
_mainCamera.gameObject.SetActive(isActive);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 设置UITop相机的激活状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="isOpen"></param>
|
|
|
|
|
public void SetUITopCameraOpen(bool isOpen = false)
|
2023-12-29 19:58:17 +08:00
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
UITopCamera.gameObject.SetActive(isOpen);
|
2023-12-29 19:58:17 +08:00
|
|
|
}
|
2024-01-08 13:59:23 +08:00
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 检查主相机是否激活
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2024-04-28 14:45:31 +08:00
|
|
|
public bool CheckMainCameraIsActive()
|
|
|
|
|
{
|
|
|
|
|
if (!_mainCamera)
|
|
|
|
|
return false;
|
2024-08-14 17:04:21 +08:00
|
|
|
|
2024-04-28 14:45:31 +08:00
|
|
|
return _mainCamera.gameObject.activeSelf;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 切换主相机
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="camera"></param>
|
2024-01-15 14:20:22 +08:00
|
|
|
public void SwitchMainCamera(Camera camera)
|
2024-02-01 17:47:50 +08:00
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
if (null == camera || camera == _mainCamera)
|
2024-02-01 17:47:50 +08:00
|
|
|
return;
|
2024-08-14 17:04:21 +08:00
|
|
|
|
2024-02-01 17:47:50 +08:00
|
|
|
_usedMainCameraStack.Push(_mainCamera);
|
|
|
|
|
_SwitchCameraPurely(camera);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 还原主相机
|
|
|
|
|
/// </summary>
|
2024-02-01 17:47:50 +08:00
|
|
|
public void RevertMainCamera()
|
|
|
|
|
{
|
2024-02-21 17:54:08 +08:00
|
|
|
// DebugUtil.LogError("还原相机");
|
2024-02-01 17:47:50 +08:00
|
|
|
if (_usedMainCameraStack.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
DebugUtil.LogError($"_usedMainCameraStack count is 0");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
Camera _lastMainCamera = GetOutMostCameraInStack();
|
2024-02-01 17:47:50 +08:00
|
|
|
|
|
|
|
|
if (_lastMainCamera == null)
|
|
|
|
|
{
|
2025-05-26 14:13:01 +08:00
|
|
|
DebugUtil.Log($"相机栈中找不到旧的使用过的主相机");
|
2024-02-01 17:47:50 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_SwitchCameraPurely(_lastMainCamera);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _SwitchCameraPurely(Camera camera)
|
2024-01-15 14:20:22 +08:00
|
|
|
{
|
2024-08-14 17:04:21 +08:00
|
|
|
if (null == camera || _mainCamera == camera)
|
2024-01-15 14:20:22 +08:00
|
|
|
return;
|
2024-08-14 17:04:21 +08:00
|
|
|
|
|
|
|
|
Camera currCamera = _mainCamera;
|
|
|
|
|
if (null != currCamera)
|
2024-02-01 17:47:50 +08:00
|
|
|
DeActiveMainCamera(currCamera);
|
|
|
|
|
|
2024-01-15 14:20:22 +08:00
|
|
|
_mainCamera = camera;
|
2024-02-01 17:47:50 +08:00
|
|
|
|
|
|
|
|
ActiveMainCamera(_mainCamera);
|
2024-01-19 17:12:36 +08:00
|
|
|
AfterSwitchMainCamera?.Invoke(_mainCamera);
|
2024-02-01 17:47:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Camera GetOutMostCameraInStack()
|
|
|
|
|
{
|
|
|
|
|
Camera result = null;
|
|
|
|
|
int popCount = 1;
|
2024-08-14 17:04:21 +08:00
|
|
|
foreach (Camera camera in _usedMainCameraStack)
|
2024-02-01 17:47:50 +08:00
|
|
|
{
|
|
|
|
|
//可能会出现之前使用过的相机已经销毁的情况
|
|
|
|
|
if (camera == null)
|
|
|
|
|
popCount++;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = camera;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
popCount = popCount > _usedMainCameraStack.Count ? _usedMainCameraStack.Count : popCount;
|
|
|
|
|
for (int i = 0; i < popCount; i++)
|
|
|
|
|
{
|
|
|
|
|
_usedMainCameraStack.Pop();
|
|
|
|
|
}
|
2024-01-15 14:20:22 +08:00
|
|
|
|
2024-02-01 17:47:50 +08:00
|
|
|
return result;
|
2024-01-15 14:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 激活主相机
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cam"></param>
|
2024-02-01 17:47:50 +08:00
|
|
|
private void ActiveMainCamera(Camera cam)
|
2024-01-15 14:20:22 +08:00
|
|
|
{
|
2025-05-20 18:44:35 +08:00
|
|
|
cam.targetTexture = DefaultRenderTarget; // 一定先赋值rt再激活
|
2024-02-01 17:47:50 +08:00
|
|
|
cam.gameObject.SetActive(true);
|
2024-01-15 14:20:22 +08:00
|
|
|
}
|
2024-02-01 17:47:50 +08:00
|
|
|
|
2024-08-14 17:04:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 禁用主相机
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cam"></param>
|
2024-02-01 17:47:50 +08:00
|
|
|
private void DeActiveMainCamera(Camera cam)
|
|
|
|
|
{
|
|
|
|
|
cam.gameObject.SetActive(false);
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
}
|