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

154 lines
3.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using Gameplay;
using UnityEngine;
using Sirenix.OdinInspector;
using TGS;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.Serialization;
using Application = UnityEngine.Device.Application;
public class NLDSceneManager : MonoBehaviour
{
public const string ART_NAME = "Art";
public const string TGS_PREVIEW_NAME = "TGSPreview";
public static NLDSceneManager Instance;
[FormerlySerializedAs("_sceneObjects")]
[ReadOnly]
[HideInInspector]
[SerializeField]
public List<SceneObject> sceneObjects = new();
[ReadOnly]
[SerializeField]
[HideInInspector]
public List<SceneSurfaceData> allSceneSurfaceData = new();
[HideInInspector]
[SerializeField]
private bool _allSurface;
public bool UseAllSurface
{
get => _allSurface;
set
{
if (_allSurface != value)
{
_allSurface = value;
foreach (var sceneObj in sceneObjects)
{
if (sceneObj)
sceneObj.UseSurface = value;
}
}
}
}
[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);
#if UNITY_EDITOR
if (artGo == null)
{
artGo = ObjectFactory.CreateGameObject(scene, HideFlags.None, ART_NAME);
}
#endif
_art = artGo.transform;
}
return _art;
}
}
public Camera SceneCamera => CommonUtils.GetSceneRootComponent<Camera>(gameObject.scene);
private Dictionary<ESceneObjectType, List<SceneObject>> _sceneObjTypeDic = new();
private Dictionary<SceneObjectRenderInfo, SceneSurfaceData> _sceneSurfaceDataDic = new();
public void Refresh()
{
_sceneObjTypeDic.Clear();
foreach (var sObj in sceneObjects)
{
if (sObj == null)
continue;
var type = sObj.sceneObjectType;
if (_sceneObjTypeDic.TryGetValue(type, out var list))
{
list.Add(sObj);
}
else
{
list = new();
list.Add(sObj);
_sceneObjTypeDic.Add(type, list);
}
}
_sceneSurfaceDataDic.Clear();
foreach (var surfaceData in allSceneSurfaceData)
{
if (surfaceData.renderInfo)
_sceneSurfaceDataDic.TryAdd(surfaceData.renderInfo, surfaceData);
}
}
private void Awake()
{
Instance = this;
Refresh();
// if (CommonUtils.IsInGame())
// {
// DeActiveSceneCamera();
// }
}
private void DeActiveSceneCamera()
{
var allCamera = FindObjectsOfType<Camera>();
foreach (var k_camera in allCamera)
{
if (k_camera.gameObject.scene == gameObject.scene)
{
k_camera.gameObject.SetActive(false);
}
}
}
private void OnDestroy()
{
Instance = null;
}
public List<SceneObject> GetSceneObjectsByType(ESceneObjectType type)
{
return _sceneObjTypeDic.GetValueOrDefault(type);
}
public SceneSurfaceData GetSurfaceData(SceneObjectRenderInfo renderInfo)
{
return _sceneSurfaceDataDic.GetValueOrDefault(renderInfo);
}
}