BITFALL/Assets/Artists/Scripts/Props/Prop_Optimize.cs

51 lines
961 B
C#
Raw Normal View History

2024-03-18 18:20:23 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using NGS.AdvancedCullingSystem.Dynamic;
using UnityEngine;
namespace BITFALL.Props
{
public class Prop_Optimize : MonoBehaviour
{
[SerializeField] private new Rigidbody rigidbody;
[SerializeField] private Collider[] colliders;
private bool isSleeping;
2024-04-06 16:33:57 +08:00
private bool isInitialized;
2024-03-18 18:20:23 +08:00
private void OnBecameInvisible()
{
2024-04-06 16:33:57 +08:00
EnsureInitialized();
if (rigidbody)
2024-03-18 18:20:23 +08:00
{
2024-04-06 16:33:57 +08:00
rigidbody.Sleep();
2024-03-18 18:20:23 +08:00
rigidbody.useGravity = false;
2024-04-06 16:33:57 +08:00
}
2024-03-18 18:20:23 +08:00
foreach (var x in colliders)
{
x.enabled = false;
}
}
private void OnBecameVisible()
{
2024-04-06 16:33:57 +08:00
EnsureInitialized();
2024-03-18 18:20:23 +08:00
if (rigidbody)
{
rigidbody.useGravity = true;
}
foreach (var x in colliders)
{
x.enabled = true;
}
}
2024-04-06 16:33:57 +08:00
private void EnsureInitialized()
{
if (isInitialized) return;
isInitialized = true;
if(colliders?.Length == 0)
colliders = GetComponentsInChildren<Collider>();
}
2024-03-18 18:20:23 +08:00
}
}