BITKit/Src/Unity/Scripts/WorldChunk/ColliderChunkObject.cs

73 lines
2.0 KiB
C#
Raw Normal View History

2024-03-31 23:31:00 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using Quadtree;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace BITKit.OpenWorld
{
/// <summary>
/// 这个应该载入后就销毁,托管给纯class
/// </summary>
public sealed class ColliderChunkObject : MonoBehaviour
{
[SerializeField] private Collider[] colliders;
[SerializeField] private ChunkBehaviour chunkBehaviour;
2024-05-31 01:23:15 +08:00
[SerializeField] private bool getCollidersOnStart;
2024-03-31 23:31:00 +08:00
private void Start()
{
2024-04-16 04:06:46 +08:00
try
{
2024-05-31 01:23:15 +08:00
if (getCollidersOnStart)
{
GetCollidersInChildren();
}
2024-04-16 04:06:46 +08:00
chunkBehaviour.OnLodChangedEvent += OnLodChanged;
if(colliders is {Length:0})colliders = GetComponentsInChildren<Collider>();
}
catch (Exception e)
{
BIT4Log.Warning<ColliderChunkObject>(gameObject.name);
BIT4Log.LogException(e);
}
2024-03-31 23:31:00 +08:00
}
private void OnLodChanged(int arg1, int arg2)
{
var enabledCollider = arg2 is 0;
foreach (var x in colliders)
{
2024-04-06 16:33:32 +08:00
try
{
x.enabled = enabledCollider;
}
2024-04-16 04:06:46 +08:00
catch (UnassignedReferenceException)
{
GetCollidersInChildren();
OnLodChanged(arg1, arg2);
return;
}
2024-04-06 16:33:32 +08:00
catch (Exception e)
{
BIT4Log.Warning<ColliderChunkObject>(gameObject.name);
BIT4Log.LogException(e);
}
2024-04-16 04:06:46 +08:00
2024-03-31 23:31:00 +08:00
}
}
[BIT]
private void GetCollidersInChildren()
{
colliders = GetComponentsInChildren<Collider>();
2024-04-16 04:06:46 +08:00
#if UNITY_EDITOR
2024-03-31 23:31:00 +08:00
EditorUtility.SetDirty(this);
2024-04-16 04:06:46 +08:00
#endif
2024-03-31 23:31:00 +08:00
}
}
}