using System; using System.Collections; using System.Collections.Generic; using Cysharp.Threading.Tasks; using Quadtree; #if UNITY_EDITOR using UnityEditor.SceneManagement; #endif using UnityEngine; using UnityEngine.SceneManagement; using YooAsset; namespace BITKit.OpenWorld { public class WorldTerrainBehaviour : MonoBehaviour,IWorldChunkObject { public static UniTaskCompletionSource WaitUntilInitialized = new(); [SerializeReference, SubclassSelector] private IReference sceneName; [SerializeField] private Vector3 size; [SerializeField] private Vector3 position; [SerializeField, ReadOnly] private int lod = -1; private SceneHandle _sceneHandle; [BIT] public async void Load() { if (_sceneHandle is not null) return; var stopWatcher = new System.Diagnostics.Stopwatch(); stopWatcher.Start(); _sceneHandle = YooAssets.LoadSceneAsync(sceneName.Value,LoadSceneMode.Additive,priority:8); try { await _sceneHandle.WithCancellation(destroyCancellationToken); } catch (OperationCanceledException) { } stopWatcher.Stop(); Debug.Log($"加载场景 {sceneName.Value} 耗时 {stopWatcher.ElapsedMilliseconds}ms"); } [BIT] public async void Unload() { if (_sceneHandle is null) return; var stopWatcher = new System.Diagnostics.Stopwatch(); stopWatcher.Start(); await _sceneHandle.UnloadAsync(); _sceneHandle = null; stopWatcher.Stop(); Debug.Log($"卸载场景 {sceneName.Value} 耗时 {stopWatcher.ElapsedMilliseconds}ms"); } public Bounds GetBounds()=> new Bounds(position, size); public Node ParentNode { get; set; } public void QuadTree_Root_Initialized(IQuadtreeRoot> root) { } public int Id { get; set; } public int Lod { get=>lod; set { lod = value; switch (value) { case 0: Load(); break; default: Unload(); break; } } } private void Start() { WorldTerrainService.Singleton.Register(this); destroyCancellationToken.Register(() => WorldTerrainService.Singleton.Unregister(this)); } private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireCube(position, size); } } }