オブジェクトの破壊時にイベントを起こす:OnApplicationQuit/OnDestroy
なにかのオブジェクトが破壊された時に、自動的にイベントを起こしたい場合。
http://answers.unity3d.com/questions/169656/instantiate-ondestroy.html
そのオブジェクトをDestroyする時にコードを実行するのでなく、破壊される時に勝手に実行してほしい場合。
以下のサンプルコードをコンポーネントとして追加した場合、対象オブジェクトが破壊された時にアイテムを生成する。
結果的にアイテムを吐き出して破壊されることになる。
void OnApplicationQuit() { isQuitting = true; } // OnDestroyだけだとGame内で生成したオブジェクトが作業中のSceneに残る. // OnApplicationQuitでフラグ立てしそれに従い制御する. void OnDestroy () { if(!isQuitting) { Vector3 spawnPosition = transform.position; GameObject tgtItem = (GameObject)Instantiate(spawnInstance, Vector3.zero, Quaternion.identity); Transform plateTrns = tgtItem.transform.FindChild("Plate").gameObject.transform; plateTrns.position = spawnPosition + (Vector3.up * 1.0f); } }