0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity > 粒子を倍々にしていく (成功編)

Last updated at Posted at 2015-08-09

http://qiita.com/7of9/items/a6e7cc154fe8901942b1
の修正版

動作確認
Unity 5.1.1-f on MacOS X 10.8.5

倍々ゲームをしたい。
最初1つの粒子がある。そこに1つ足す。
次は2つ足す。
次は4つ足す。
。。。

準備

  1. Main Cameraに以下を足す
  2. Main CameraのMy Prefabに追加したいオブジェクトを関連づける。

今回はCubeをMy Prefabに関連付けた。

code

CameraScript.cs
using UnityEngine;
using System.Collections;
using UnityEditor;

public class CameraScript : MonoBehaviour {

	public GameObject myPrefab; // relate to the monomer
	private GameObject myParent;
	
	void AddMonomer() {
		Vector3 pos;
		pos.x = Random.Range (-3.0f, 3.0f);
		pos.y = Random.Range (-3.0f, 3.0f);
		pos.z = Random.Range (-3.0f, 3.0f);

		GameObject GOmonomer = Instantiate(myPrefab, pos, Quaternion.identity) as GameObject;
		GOmonomer.name = "monomer1";
		GOmonomer.transform.parent = myParent.transform;
	}

	void Start () {
		myParent = new GameObject ();
		myParent.name = "Parent";

		// 2 monomers at the beginning
		AddMonomer ();
		AddMonomer ();
	}

	void OnGUI() {
		if (GUI.Button (new Rect (10, 10, 100, 30), "Generate")) {
			int numChild = myParent.transform.childCount;
			print (numChild.ToString());

			for(int loop=0; loop < numChild; loop++) {
				AddMonomer();
			}

			// ERROR: unlimited loop
//			foreach(Transform child in myParent.transform) {
//				AddMonomer(new Vector3(0, 0, 0));
//			}

		}
	}
}

ちなみにforeach()でやると粒子をたしながらループ上限が増えるためか無限ループになるのでやめておいた方がいい。

結果

16個の粒子を生成した所。

Scene_unity_-150809_replacePrefab-PC__Mac___Linux_Standalone__Personal.jpg

256個の粒子を生成した所。
ボーグ・キューブみたいになってきた。

Scene_unity_-150809_replacePrefab-PC__Mac___Linux_Standalone__Personal.jpg

これでcluster-cluster aggregate生成をできると思ったが、粒子数が増えるにつれ、やはり処理が重くなっていく。

1.3GHz Core i5のMacbook Airにて2048の粒子の表示が重い。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?