57 lines
1.1 KiB
C#
57 lines
1.1 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using BITKit;
|
||
|
using MeshCombineStudio;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace BITFALL.OpenWorld
|
||
|
{
|
||
|
public class RuntimeChunkCombineService<T> : MonoBehaviour
|
||
|
{
|
||
|
public static RuntimeChunkCombineService<T> Singleton { get; private set; }
|
||
|
public void Register(GameObject root)
|
||
|
{
|
||
|
_Roots.Add(root);
|
||
|
_IsDirty = true;
|
||
|
}
|
||
|
|
||
|
public void UnRegister(GameObject root)
|
||
|
{
|
||
|
_Roots.Remove(root);
|
||
|
_IsDirty = true;
|
||
|
}
|
||
|
|
||
|
private readonly List<GameObject> _Roots = new();
|
||
|
[SerializeField] private MeshCombiner meshCombiner;
|
||
|
private bool _IsDirty;
|
||
|
|
||
|
[SerializeReference, SubclassSelector] private ITicker ticker;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
Singleton = this;
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
ticker.Add(Tick);
|
||
|
destroyCancellationToken.Register(Dispose);
|
||
|
_Roots.AddRange(meshCombiner.searchOptions.parentGOs);
|
||
|
}
|
||
|
|
||
|
private void Dispose()
|
||
|
{
|
||
|
ticker.Remove(Tick);
|
||
|
}
|
||
|
|
||
|
private void Tick(float obj)
|
||
|
{
|
||
|
if (!_IsDirty) return;
|
||
|
meshCombiner.ClearMeshCombineJobs();
|
||
|
meshCombiner.searchOptions.parentGOs = _Roots.ToArray();
|
||
|
meshCombiner.CombineAll();
|
||
|
}
|
||
|
}
|
||
|
}
|