2024-01-19 17:12:36 +08:00
|
|
|
using System;
|
2024-02-01 17:47:50 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2023-08-22 19:08:45 +08:00
|
|
|
using UnityEngine;
|
2024-01-08 13:59:23 +08:00
|
|
|
using UnityEngine.Rendering.Universal;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
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
|
|
|
{
|
|
|
|
|
private Camera _mainCamera;
|
|
|
|
|
|
2024-02-01 17:47:50 +08:00
|
|
|
private Stack<Camera> _usedMainCameraStack = new();
|
|
|
|
|
|
|
|
|
|
private RenderTexture _defaultRenderTarget;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
_mainCamera.targetTexture = _defaultRenderTarget;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 17:47:50 +08:00
|
|
|
public event Action<Camera> AfterSwitchMainCamera;
|
|
|
|
|
|
|
|
|
|
public Camera MainCamera => _mainCamera;
|
|
|
|
|
|
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-07-19 13:12:50 +08:00
|
|
|
public Camera UITopCamera
|
|
|
|
|
{
|
|
|
|
|
get { return UIRoot.Instance.UITopCamera; }
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-08 13:59:23 +08:00
|
|
|
public Camera SkillCamera { get; private set; }
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
public Camera CreateCamera(string name, string tag = null)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
var go = new GameObject(name);
|
|
|
|
|
var camera = go.AddComponent<Camera>();
|
|
|
|
|
if (tag != null)
|
|
|
|
|
{
|
2024-02-01 17:47:50 +08:00
|
|
|
camera.tag = tag;
|
2023-10-19 15:00:19 +08:00
|
|
|
}
|
2024-02-01 17:47:50 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (camera != null)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
GameObject.Destroy(camera.gameObject);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReRenderAll()
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in Camera.allCameras)
|
|
|
|
|
{
|
|
|
|
|
var oldEnabled = c.enabled;
|
|
|
|
|
c.enabled = true;
|
|
|
|
|
c.Render();
|
|
|
|
|
c.enabled = oldEnabled;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
2024-02-01 17:47:50 +08:00
|
|
|
_mainCamera = Camera.main;
|
|
|
|
|
if (_mainCamera == null)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
CreateCamera("Main Camera", "MainCamera");
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
2024-02-01 17:47:50 +08:00
|
|
|
|
|
|
|
|
if (_mainCamera != null)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
GameObject.DontDestroyOnLoad(MainCamera.gameObject);
|
|
|
|
|
}
|
2024-01-08 13:59:23 +08:00
|
|
|
|
|
|
|
|
SkillCamera = GameObject.Find("SkillCamera").GetComponent<Camera>();
|
2024-04-10 17:03:28 +08:00
|
|
|
SkillCamera.gameObject.SetActive(false);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 19:58:17 +08:00
|
|
|
public void SetMainCameraActive(bool isActive)
|
|
|
|
|
{
|
|
|
|
|
_mainCamera?.gameObject.SetActive(isActive);
|
|
|
|
|
}
|
2024-01-08 13:59:23 +08:00
|
|
|
|
2024-04-28 14:45:31 +08:00
|
|
|
public bool CheckMainCameraIsActive()
|
|
|
|
|
{
|
|
|
|
|
if (!_mainCamera)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return _mainCamera.gameObject.activeSelf;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 14:20:22 +08:00
|
|
|
public void SwitchMainCamera(Camera camera)
|
2024-02-01 17:47:50 +08:00
|
|
|
{
|
2024-02-21 17:54:08 +08:00
|
|
|
// DebugUtil.LogError("切换相机");
|
2024-02-01 17:47:50 +08:00
|
|
|
if (camera == null || camera == _mainCamera)
|
|
|
|
|
return;
|
|
|
|
|
_usedMainCameraStack.Push(_mainCamera);
|
|
|
|
|
_SwitchCameraPurely(camera);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _lastMainCamera = GetOutMostCameraInStack();
|
|
|
|
|
|
|
|
|
|
if (_lastMainCamera == null)
|
|
|
|
|
{
|
|
|
|
|
DebugUtil.LogError($"相机栈中找不到旧的使用过的主相机");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_SwitchCameraPurely(_lastMainCamera);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 13:12:50 +08:00
|
|
|
public void SetUITopCameraOpen(bool isOpen = false)
|
|
|
|
|
{
|
|
|
|
|
UITopCamera.gameObject.SetActive(isOpen);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 17:47:50 +08:00
|
|
|
private void _SwitchCameraPurely(Camera camera)
|
2024-01-15 14:20:22 +08:00
|
|
|
{
|
|
|
|
|
if (camera == null || _mainCamera == camera)
|
|
|
|
|
return;
|
2024-02-01 17:47:50 +08:00
|
|
|
var currCamera = _mainCamera;
|
|
|
|
|
if (currCamera != null)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
foreach (var camera in _usedMainCameraStack)
|
|
|
|
|
{
|
|
|
|
|
//可能会出现之前使用过的相机已经销毁的情况
|
|
|
|
|
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-02-01 17:47:50 +08:00
|
|
|
private void ActiveMainCamera(Camera cam)
|
2024-01-15 14:20:22 +08:00
|
|
|
{
|
2024-02-01 17:47:50 +08:00
|
|
|
cam.gameObject.SetActive(true);
|
|
|
|
|
cam.targetTexture = DefaultRenderTarget;
|
2024-01-15 14:20:22 +08:00
|
|
|
}
|
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
|
|
|
public void Release()
|
|
|
|
|
{
|
2024-02-01 17:47:50 +08:00
|
|
|
_usedMainCameraStack.Clear();
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|