NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/Scene/NLDSceneManager.cs

146 lines
3.5 KiB
C#
Raw Normal View History

2023-12-07 16:41:41 +08:00
using System.Collections.Generic;
2023-12-14 18:07:51 +08:00
using System.Linq;
2025-04-28 15:03:17 +08:00
using Framework.Utils;
2023-12-07 16:41:41 +08:00
using UnityEngine;
using Sirenix.OdinInspector;
2023-12-15 11:42:47 +08:00
#if UNITY_EDITOR
2023-12-14 18:07:51 +08:00
using UnityEditor;
2023-12-15 11:42:47 +08:00
#endif
2023-12-08 20:02:39 +08:00
using UnityEngine.Serialization;
2023-12-07 16:41:41 +08:00
2023-12-08 20:02:39 +08:00
public class NLDSceneManager : MonoBehaviour
2023-12-07 16:41:41 +08:00
{
2023-12-14 18:07:51 +08:00
public const string ART_NAME = "Art";
2023-12-15 11:42:47 +08:00
2024-01-02 14:52:37 +08:00
public static NLDSceneManager Instance;
2023-12-12 20:03:44 +08:00
2023-12-08 20:02:39 +08:00
[FormerlySerializedAs("_sceneObjects")]
[ReadOnly]
2023-12-14 14:35:02 +08:00
[HideInInspector]
2023-12-07 16:41:41 +08:00
[SerializeField]
2023-12-08 20:02:39 +08:00
public List<SceneObject> sceneObjects = new();
2023-12-12 20:03:44 +08:00
[ReadOnly]
[SerializeField]
2023-12-13 14:20:38 +08:00
[HideInInspector]
2023-12-12 20:03:44 +08:00
public List<SceneSurfaceData> allSceneSurfaceData = new();
2023-12-15 11:42:47 +08:00
2023-12-14 14:33:45 +08:00
[HideInInspector]
[SerializeField]
private bool _allSurface;
2023-12-12 20:03:44 +08:00
2023-12-14 14:33:45 +08:00
public bool UseAllSurface
{
get => _allSurface;
set
{
if (_allSurface != value)
{
_allSurface = value;
foreach (var sceneObj in sceneObjects)
{
2023-12-15 11:42:47 +08:00
if (sceneObj)
2023-12-14 14:33:45 +08:00
sceneObj.UseSurface = value;
}
}
}
}
2023-12-15 11:42:47 +08:00
2023-12-14 18:07:51 +08:00
[SerializeField]
[HideInInspector]
private Transform _art;
public Transform Art
{
get
{
if (_art == null)
{
var scene = gameObject.scene;
var roots = scene.GetRootGameObjects();
var artGo = roots.FirstOrDefault(r => r.name == ART_NAME);
2023-12-15 11:42:47 +08:00
#if UNITY_EDITOR
2023-12-14 18:07:51 +08:00
if (artGo == null)
{
artGo = ObjectFactory.CreateGameObject(scene, HideFlags.None, ART_NAME);
}
2023-12-15 11:42:47 +08:00
#endif
2023-12-14 18:07:51 +08:00
_art = artGo.transform;
}
return _art;
}
}
2023-12-15 11:42:47 +08:00
2025-04-28 15:03:17 +08:00
public Camera SceneCamera => FrameWorkUtils.GetSceneRootComponent<Camera>(gameObject.scene);
2023-12-29 19:58:17 +08:00
2023-12-08 20:02:39 +08:00
private Dictionary<ESceneObjectType, List<SceneObject>> _sceneObjTypeDic = new();
2023-12-12 20:03:44 +08:00
private Dictionary<SceneObjectRenderInfo, SceneSurfaceData> _sceneSurfaceDataDic = new();
2023-12-15 11:42:47 +08:00
2023-12-08 20:02:39 +08:00
public void Refresh()
{
_sceneObjTypeDic.Clear();
foreach (var sObj in sceneObjects)
{
2023-12-12 20:03:44 +08:00
if (sObj == null)
2023-12-09 22:06:08 +08:00
continue;
2023-12-10 15:21:33 +08:00
var type = sObj.sceneObjectType;
2023-12-08 20:02:39 +08:00
if (_sceneObjTypeDic.TryGetValue(type, out var list))
{
list.Add(sObj);
}
else
{
list = new();
list.Add(sObj);
_sceneObjTypeDic.Add(type, list);
}
}
2023-12-12 20:03:44 +08:00
_sceneSurfaceDataDic.Clear();
foreach (var surfaceData in allSceneSurfaceData)
{
2023-12-29 19:58:17 +08:00
if (surfaceData.renderInfo)
2023-12-19 12:26:03 +08:00
_sceneSurfaceDataDic.TryAdd(surfaceData.renderInfo, surfaceData);
2023-12-12 20:03:44 +08:00
}
2023-12-08 20:02:39 +08:00
}
private void Awake()
{
2024-01-02 14:52:37 +08:00
Instance = this;
2023-12-08 20:02:39 +08:00
Refresh();
2023-12-29 19:58:17 +08:00
// if (CommonUtils.IsInGame())
// {
// DeActiveSceneCamera();
// }
2023-12-21 15:47:46 +08:00
}
private void DeActiveSceneCamera()
{
2023-12-19 15:16:51 +08:00
var allCamera = FindObjectsOfType<Camera>();
foreach (var k_camera in allCamera)
{
if (k_camera.gameObject.scene == gameObject.scene)
{
k_camera.gameObject.SetActive(false);
}
}
2023-12-08 20:02:39 +08:00
}
2023-12-29 19:58:17 +08:00
2023-12-12 20:03:44 +08:00
private void OnDestroy()
2023-12-08 20:02:39 +08:00
{
2024-01-02 14:52:37 +08:00
Instance = null;
2023-12-08 20:02:39 +08:00
}
2023-12-12 20:03:44 +08:00
public List<SceneObject> GetSceneObjectsByType(ESceneObjectType type)
2023-12-07 16:41:41 +08:00
{
2023-12-12 20:03:44 +08:00
return _sceneObjTypeDic.GetValueOrDefault(type);
2023-12-07 16:41:41 +08:00
}
2023-12-12 20:03:44 +08:00
public SceneSurfaceData GetSurfaceData(SceneObjectRenderInfo renderInfo)
2023-12-07 16:41:41 +08:00
{
2023-12-12 20:03:44 +08:00
return _sceneSurfaceDataDic.GetValueOrDefault(renderInfo);
2023-12-07 16:41:41 +08:00
}
}