Last active
August 29, 2015 14:01
-
-
Save tsubaki/d28a79bbb25d5155c7c1 to your computer and use it in GitHub Desktop.
次のシーンまで破棄されないオブジェクトを登録する
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
namespace UnityEngineExtra | |
{ | |
public class DontDestroyParent : MonoBehaviour | |
{ | |
#region UNITY_EVENT | |
void Awake () | |
{ | |
if (Instance == this) { | |
DontDestroyOnLoad (gameObject); | |
} else { | |
Destroy (gameObject); | |
} | |
} | |
void OnLevelWasLoaded (int level) | |
{ | |
transform.DetachChildren (); | |
Destroy (gameObject); | |
instance = null; | |
} | |
#endregion | |
public static void Register (GameObject obj) | |
{ | |
obj.transform.parent = Instance.transform; | |
} | |
public static void Register (MonoBehaviour component) | |
{ | |
Register (component.gameObject); | |
} | |
private static DontDestroyParent instance = null; | |
public static DontDestroyParent Instance { | |
get { | |
if (instance == null) { | |
instance = FindObjectOfType<DontDestroyParent> (); | |
if (instance == null) { | |
GameObject obj = new GameObject ("DontDestroyParent"); | |
instance = obj.AddComponent<DontDestroyParent> (); | |
} | |
instance.gameObject.hideFlags = HideFlags.NotEditable; | |
} | |
return instance; | |
} | |
} | |
} | |
} | |
static class DontDestroyParentEX | |
{ | |
public static void DontDestroyOnNextLoad (this GameObject self, GameObject target) | |
{ | |
UnityEngineExtra.DontDestroyParent.Register (target); | |
} | |
public static void DontDestroyOnNextLoad (this GameObject self, MonoBehaviour target) | |
{ | |
UnityEngineExtra.DontDestroyParent.Register (target); | |
} | |
public static void DontDestroyOnNextLoad (this MonoBehaviour self, GameObject target) | |
{ | |
UnityEngineExtra.DontDestroyParent.Register (target); | |
} | |
public static void DontDestroyOnNextLoad (this MonoBehaviour self, MonoBehaviour target) | |
{ | |
UnityEngineExtra.DontDestroyParent.Register (target); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment