OnBecameVisible():表示当该物体上挂载的脚本中有这个函数时,进入当前摄像机渲染的画面时调用该函数
OnBecameInvisible():表示当该物体上挂载的脚本中有这个函数时,离开当前摄像机渲染的画面时调用该函数
测试代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class OnB : MonoBehaviour
{
//这个是TextMeshPro组件,用来测试
private TMP_Text textMesh;
private void Awake()
{
textMesh = transform.GetComponentInChildren<TMP_Text>();
}
public void OnBecameVisible()
{
Debug.Log("进");
textMesh.text = "A";
}
public void OnBecameInvisible()
{
Debug.Log("出");
textMesh.text = "B";
}
}
注意事项:
1.在 Editor (编辑器)中运行时,Scene 视图摄像机也会导致调用该函数,如果物体已被该摄像机渲染则其他摄像机将无法触发调用