Created
May 20, 2017 10:31
-
-
Save brovador/f65e574801554104da7ecf0e30fe41ac to your computer and use it in GitHub Desktop.
Unity-CameraBoundsChecker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CameraBoundsChecker : MonoBehaviour { | |
public event System.Action<CameraBoundsChecker,bool> OnCameraVisibilityChanged; | |
public Camera cameraTarget; | |
public Vector3 extraOffset = Vector3.one; | |
private Vector3 minPoint; | |
private Vector3 maxPoint; | |
private Vector3 cameraMinPoint; | |
private Vector3 cameraMaxPoint; | |
private List<Transform> cachedChildren; | |
private bool visible = true; | |
private bool Visible { | |
get { | |
return visible; | |
} | |
set { | |
if (visible != value) { | |
visible = value; | |
if (OnCameraVisibilityChanged != null) { | |
OnCameraVisibilityChanged(this, visible); | |
} | |
} | |
} | |
} | |
void Awake() | |
{ | |
CacheChildren(); | |
} | |
void Update() | |
{ | |
CheckVisibility(); | |
} | |
void OnDrawGizmos() | |
{ | |
CacheChildren(); | |
CheckVisibility(); | |
Gizmos.color = Visible ? Color.green : Color.red; | |
Gizmos.DrawLine(minPoint, new Vector3(maxPoint.x, minPoint.y, minPoint.z)); | |
Gizmos.DrawLine(new Vector3(maxPoint.x, minPoint.y, minPoint.z), maxPoint); | |
Gizmos.DrawLine(maxPoint, new Vector3(minPoint.x, maxPoint.y, maxPoint.z)); | |
Gizmos.DrawLine(new Vector3(minPoint.x, maxPoint.y, maxPoint.z), minPoint); | |
} | |
void CacheChildren() | |
{ | |
minPoint = new Vector3(float.MaxValue, float.MinValue, float.MaxValue); | |
maxPoint = new Vector3(float.MinValue, float.MaxValue, float.MinValue); | |
cachedChildren = new List<Transform>(this.transform.childCount); | |
foreach (Transform child in this.transform) { | |
cachedChildren.Add(child); | |
minPoint.x = Mathf.Min(child.transform.position.x, minPoint.x); | |
minPoint.y = Mathf.Max(child.transform.position.y, minPoint.y); | |
minPoint.z = Mathf.Min(child.transform.position.z, minPoint.z); | |
maxPoint.x = Mathf.Max(child.transform.position.x, maxPoint.x); | |
maxPoint.y = Mathf.Min(child.transform.position.y, maxPoint.y); | |
maxPoint.z = Mathf.Max(child.transform.position.z, maxPoint.z); | |
} | |
minPoint += new Vector3(-extraOffset.x, extraOffset.y, extraOffset.z); | |
maxPoint += new Vector3(extraOffset.x, -extraOffset.y, extraOffset.z); | |
if (cameraTarget) { | |
cameraMinPoint = cameraTarget.WorldToViewportPoint(minPoint); | |
cameraMaxPoint = cameraTarget.WorldToViewportPoint(maxPoint); | |
} | |
} | |
void CheckVisibility() | |
{ | |
Visible = !(cameraMinPoint.x < 0.0f && cameraMaxPoint.x < 0.0f | |
|| (cameraMinPoint.x > 1.0f && cameraMaxPoint.x > 1.0f) | |
|| (cameraMinPoint.y < 0.0f && cameraMaxPoint.y < 0.0f) | |
|| (cameraMinPoint.y > 1.0f && cameraMaxPoint.y > 1.0f) | |
|| (cameraMinPoint.z < 0.0f && cameraMaxPoint.z < 0.0f) | |
|| (cameraMinPoint.z > cameraTarget.farClipPlane && cameraMaxPoint.z > cameraTarget.farClipPlane) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment