引入 Cinemachine 和 CameraModule,添加摄像机跟随缓动效果

This commit is contained in:
SepComet
2026-06-10 09:55:57 +08:00
parent ee69efe965
commit 7a3a2cf17b
27 changed files with 999 additions and 269 deletions
@@ -3,6 +3,7 @@ using SepCore.DamageText;
using SepCore.EnemyManager;
using SepCore.HPBar;
using SepCore.InputModule.Runtime;
using SepCore.CameraModule;
using SepCore.SpriteCache;
using SepCore.UIRouter;
using SepCore.Simulation;
@@ -28,6 +29,8 @@ public partial class GameEntry
public static InputModuleComponent InputModule { get; private set; }
public static CameraModuleComponent CameraModule { get; private set; }
private static void InitCustomComponents()
{
BuiltinData = UnityGameFramework.Runtime.GameEntry.GetComponent<BuiltinDataComponent>();
@@ -38,5 +41,6 @@ public partial class GameEntry
SpriteCache = UnityGameFramework.Runtime.GameEntry.GetComponent<SpriteCacheComponent>();
UIRouter = UnityGameFramework.Runtime.GameEntry.GetComponent<UIRouterComponent>();
InputModule = UnityGameFramework.Runtime.GameEntry.GetComponent<InputModuleComponent>();
CameraModule = UnityGameFramework.Runtime.GameEntry.GetComponent<CameraModuleComponent>();
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2c8aaad9a99f4e65a6ef8c47bc18873e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,188 @@
using UnityEngine;
using UnityGameFramework.Runtime;
using Object = UnityEngine.Object;
namespace SepCore.CameraModule
{
/// <summary>
/// UnityGameFramework 侧的相机模块组件。
/// 作为 GameEntry.CameraModule 访问入口,统一持有并转发 CameraModule 控制器能力。
/// </summary>
[DisallowMultipleComponent]
public class CameraModuleComponent : GameFrameworkComponent
{
[SerializeField] private CinemachineCameraController _cinemachineController;
[SerializeField] private bool _autoSearchController = true;
[SerializeField] private bool _logMissingControllerWarnings = true;
private ICameraController _controller;
private bool _hasLoggedMissingController;
public ICameraController Controller
{
get
{
ResolveController(false);
return IsControllerValid(_controller) ? _controller : null;
}
}
public bool HasController => Controller != null;
public Transform Target => Controller?.Target;
public CameraEffectSettings CurrentSettings => Controller?.CurrentSettings ?? CameraEffectSettings.Default;
protected override void Awake()
{
base.Awake();
ResolveController(false);
}
public void RegisterController(ICameraController controller)
{
if (!IsControllerValid(controller))
{
Log.Warning("CameraModuleComponent.RegisterController() controller is invalid.");
return;
}
_controller = controller;
_hasLoggedMissingController = false;
if (controller is CinemachineCameraController cinemachineController)
{
_cinemachineController = cinemachineController;
}
}
public void UnregisterController(ICameraController controller)
{
if (controller != null && !ReferenceEquals(_controller, controller))
{
return;
}
_controller = null;
_cinemachineController = null;
}
public void SetTarget(Transform target)
{
if (!EnsureController())
{
return;
}
_controller.SetTarget(target);
}
public void ApplySettings(CameraEffectSettings settings)
{
if (!EnsureController())
{
return;
}
_controller.ApplySettings(settings);
}
public void EnableEffect(CameraEffectType effect)
{
if (!EnsureController())
{
return;
}
_controller.EnableEffect(effect);
}
public void DisableEffect(CameraEffectType effect)
{
if (!EnsureController())
{
return;
}
_controller.DisableEffect(effect);
}
public bool IsEffectEnabled(CameraEffectType effect)
{
return EnsureController() && _controller.IsEffectEnabled(effect);
}
private bool EnsureController()
{
ResolveController(_logMissingControllerWarnings);
return IsControllerValid(_controller);
}
private void ResolveController(bool logWarning)
{
if (IsControllerValid(_controller))
{
return;
}
if (_cinemachineController != null)
{
_controller = _cinemachineController;
_hasLoggedMissingController = false;
return;
}
if (!_autoSearchController)
{
LogMissingController(logWarning);
return;
}
_cinemachineController = GetComponentInChildren<CinemachineCameraController>(true);
if (_cinemachineController == null)
{
#if UNITY_2023_1_OR_NEWER
_cinemachineController = FindFirstObjectByType<CinemachineCameraController>(FindObjectsInactive.Include);
#else
_cinemachineController = FindObjectOfType<CinemachineCameraController>(true);
#endif
}
if (_cinemachineController != null)
{
_controller = _cinemachineController;
_hasLoggedMissingController = false;
return;
}
LogMissingController(logWarning);
}
private void LogMissingController(bool logWarning)
{
if (!logWarning || _hasLoggedMissingController)
{
return;
}
_hasLoggedMissingController = true;
Log.Warning(
"CameraModuleComponent requires a CinemachineCameraController. " +
"Assign one in Inspector or place one in the loaded scene.");
}
private static bool IsControllerValid(ICameraController controller)
{
if (controller == null)
{
return false;
}
if (controller is Object unityObject && unityObject == null)
{
return false;
}
return true;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b77e5f4f170e44f38f20d5ac2dd287e7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -11,7 +11,9 @@
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:fca0f81bc71f1944887dd65f134c54a0",
"GUID:47a82ffa13c291447ab895cd0bc251cd",
"GUID:d54b9488b03814a44ab937f0aeb738b1"
"GUID:d54b9488b03814a44ab937f0aeb738b1",
"GUID:0e6a2ce06de727c44965fbc35958920f",
"GUID:1ddd9fc6811c1d14bbde99bd57869070"
],
"includePlatforms": [],
"excludePlatforms": [],
@@ -22,4 +24,4 @@
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
}