1
This commit is contained in:
85
Src/Unity/Scripts/WorldChunk/WorldTerrainBehaviour.cs
Normal file
85
Src/Unity/Scripts/WorldChunk/WorldTerrainBehaviour.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Quadtree;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using YooAsset;
|
||||
|
||||
namespace BITKit.OpenWorld
|
||||
{
|
||||
public class WorldTerrainBehaviour : MonoBehaviour,IWorldChunkObject
|
||||
{
|
||||
[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,suspendLoad:true,priority:8);
|
||||
await _sceneHandle;
|
||||
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<IWorldChunkObject> ParentNode { get; set; }
|
||||
public void QuadTree_Root_Initialized(IQuadtreeRoot<IWorldChunkObject, Node<IWorldChunkObject>> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user