Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created January 9, 2016 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/d2df0b90b08dc1acd3c9 to your computer and use it in GitHub Desktop.
Save tsubaki/d2df0b90b08dc1acd3c9 to your computer and use it in GitHub Desktop.
CullingGroupで視界外の敵を生成しない
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class SpawnTest : MonoBehaviour
{
private IEnumerator spawnFlow = null;
private CullingGroup cullingGroup = null;
[SerializeField] Transform[] targetPositions = null;
[SerializeField] GameObject spawnObject = null;
void Awake()
{
spawnFlow = SpawnCoroutine();
}
void OnEnable()
{
// cullinggroupを生成してコルーチンを開始
SetupCullinggroup ();
StartCoroutine (spawnFlow);
}
void OnDisable()
{
// コルーチンを停止してcullinggroupを破棄
StopCoroutine (spawnFlow);
cullingGroup.Dispose ();
cullingGroup = null;
}
void SetupCullinggroup()
{
// culling groupの初期化
cullingGroup = new CullingGroup ();
cullingGroup.targetCamera = Camera.main;
// 敵を生成する座標の登録
BoundingSphere[] bounds = new BoundingSphere[targetPositions.Length];
for (int i = 0; i < targetPositions.Length; i++) {
bounds [i].position = targetPositions [i].position;
bounds [i].radius = 1;
}
cullingGroup.SetBoundingSpheres (bounds);
cullingGroup.SetBoundingSphereCount (targetPositions.Length);
}
IEnumerator SpawnCoroutine()
{
List<int> countList = new List<int> ();
while (true) {
// 視界外にある生成ポイント一覧を取得
countList.Clear ();
for (int i = 0; i < targetPositions.Length; i++) {
if (cullingGroup.IsVisible (i) == false ) {
countList.Add (i);
}
}
if (countList.Count != 0) {
var newPos = countList[Random.Range (0, countList.Count)];
GameObject.Instantiate (spawnObject, targetPositions [newPos].position, Quaternion.identity);
}
// 0.5秒待つ
yield return new WaitForSeconds (0.5f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment