Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created January 7, 2016 15:25
Show Gist options
  • Select an option

  • Save tsubaki/a965f3bf69d76cbf14fd to your computer and use it in GitHub Desktop.

Select an option

Save tsubaki/a965f3bf69d76cbf14fd to your computer and use it in GitHub Desktop.

Revisions

  1. tsubaki created this gist Jan 7, 2016.
    63 changes: 63 additions & 0 deletions CullingGroupSample.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    using UnityEngine;
    using System.Collections;

    public class CullingGroupSample : MonoBehaviour {

    private CullingGroup group = null;
    private BoundingSphere[] bounds;

    [SerializeField] Transform[] targets = null;

    void Start()
    {
    group = new CullingGroup ();

    // カリングを行うカメラを設定
    group.targetCamera = Camera.main;

    // 距離を測る用の中心となる座標と、距離のレベルを設定
    // 1:1m 2:5m 3:10m, 4:30m, 5:100m それ以上:見えない扱い
    group.SetDistanceReferencePoint (Camera.main.transform);
    group.SetBoundingDistances (new float[]{ 1, 5, 10, 30, 100});

    // 視界判定を行う一覧をセットアップ
    bounds = new BoundingSphere[targets.Length];
    for (int i = 0; i < bounds.Length; i++) {
    bounds [i].radius = 1.5f;
    }

    // 一覧への参照を登録
    group.SetBoundingSpheres (bounds);
    group.SetBoundingSphereCount (targets.Length);

    // オブジェクトの視認状態が変化した際のコールバックを登録
    group.onStateChanged = OnChange;
    }

    void Update()
    {
    // 登録したオブジェクトの座標を更新
    for (int i = 0; i < bounds.Length; i++) {
    bounds [i].position = targets [i].position;
    }
    }

    void OnDestroy()
    {
    // 終了時は必ずdispose
    group.onStateChanged -= OnChange;
    group.Dispose ();
    group = null;
    }

    void OnChange(CullingGroupEvent ev ){
    // 視界外内のオブジェクトのみアクティブにする
    targets [ev.index].gameObject.SetActive(ev.isVisible);

    // レンジが2以上の場合は非アクティブにする
    if (ev.currentDistance > 2) {
    targets [ev.index].gameObject.SetActive (false);
    }
    }

    }