103 lines
2.9 KiB
C#
103 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
namespace Thousandto.Core.Base
|
|
{
|
|
/// <summary>
|
|
/// 对象可视化检测
|
|
/// 根据如果调用了OnWillRenderObject表示,当前正在被渲染
|
|
/// </summary>
|
|
public class ObjectVisibleChecker
|
|
{
|
|
//是否可用
|
|
private bool _isEnable = false;
|
|
//物体是否正在被渲染
|
|
private bool _isRendering = true;
|
|
//最后的时间
|
|
private float _lastTime = 1;
|
|
//当前时间
|
|
private float _curtTime = 0;
|
|
//设置为可用的帧数
|
|
private int _frameCount = 0;
|
|
|
|
//所属对象
|
|
private GameObject _go;
|
|
private Renderer _render;
|
|
private bool _isTempAdd = false;
|
|
|
|
//是否正在被绘制
|
|
public bool IsRendering
|
|
{
|
|
get
|
|
{
|
|
//已经检测10帧后才返回真正的是否渲染
|
|
if (_frameCount >= 10)
|
|
{
|
|
return _isRendering;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
//是否启动检测
|
|
public bool IsEnable
|
|
{
|
|
get { return _isEnable; }
|
|
set {
|
|
if (_isEnable != value)
|
|
{
|
|
_lastTime = 1;
|
|
_curtTime = 0;
|
|
_frameCount = 0;
|
|
_isRendering = true;
|
|
_isEnable = value;
|
|
|
|
if (_isEnable)
|
|
{
|
|
//这里来判断是否有Renderer,如果没有则添加上
|
|
if (_render == null)
|
|
{
|
|
_isTempAdd = true;
|
|
_render = _go.AddComponent<MeshRenderer>();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//初始化
|
|
public void Init(GameObject go)
|
|
{
|
|
_go = go;
|
|
_isRendering = false;
|
|
_render = _go.GetComponent<Renderer>();
|
|
}
|
|
|
|
//由Update来调用
|
|
public void Update()
|
|
{
|
|
if (_isEnable)
|
|
{
|
|
_isRendering = (_curtTime != _lastTime ? true : false);
|
|
_lastTime = _curtTime;
|
|
if (_frameCount < 10) _frameCount++;
|
|
}
|
|
}
|
|
|
|
//由OnWillRenderObject来调用
|
|
public void OnWillRenderObject()
|
|
{
|
|
_curtTime = Time.time;
|
|
}
|
|
|
|
//判断某个Collider是否在某个摄像机的范围之内 -- 通过AABB盒子类测试
|
|
public bool I_Can_See(Camera camera,Collider collider)
|
|
{
|
|
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
|
|
return GeometryUtility.TestPlanesAABB(planes, collider.bounds);
|
|
}
|
|
}
|
|
}
|