teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

実験結果を追記

2018/03/04 00:00

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -6,4 +6,73 @@
6
6
  > You do not have access to the DontDestroyOnLoad scene and it is not available at runtime.
7
7
 
8
8
  とのことで、DontDestroyOnLoadは特別扱いらしいため、もしかするとダメかもしれません。後で私も試してみようかと思いますが、ダメだった場合、もし適当な代替手法を思い付いたら追記します...
9
- DontDestroyOnLoadからオブジェクトを移動させたい理由もご説明いただけるとヒントになるかもしれません。
9
+ DontDestroyOnLoadからオブジェクトを移動させたい理由もご説明いただけるとヒントになるかもしれません。
10
+
11
+ ### [さらに追記]
12
+ どうやらPlayerPrefsの方が適していたご様子ですが、どこか別の場面で使う機会があるかもしれませんので、試してみた結果を追記します。
13
+ シーンAにあるキューブをDontDestroyOnLoad指定してから別のシーンBをロード、キューブをシーンBに移動してみたところ、どうやらちゃんと移動してくれるようでした。ここからさらに別のシーンCをロードすると、キューブも一緒に消えました。
14
+ ![実行結果](ee54fa64429d5c5c4dc2d19d7e63890f.gif)
15
+ なお、制御のためにシーンAには下記のスクリプトを持つオブジェクトを配置し、
16
+ ```C#
17
+ using System.Collections;
18
+ using UnityEngine;
19
+ using UnityEngine.SceneManagement;
20
+
21
+ public class SceneAController : MonoBehaviour
22
+ {
23
+ public GameObject Cube;
24
+
25
+ private IEnumerator Start()
26
+ {
27
+ Debug.Log("Hit Space to continue...");
28
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
29
+
30
+ Debug.LogFormat("Move {0} to DontDestroyOnLoad.", this.Cube.name);
31
+ DontDestroyOnLoad(this.Cube);
32
+ yield return new WaitForEndOfFrame();
33
+
34
+ Debug.Log("Hit Space to continue...");
35
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
36
+
37
+ Debug.Log("Go to SceneB...");
38
+ yield return SceneManager.LoadSceneAsync("SceneB");
39
+ }
40
+ }
41
+ ```
42
+ シーンBには下記のスクリプトを持つオブジェクトを配置しています。
43
+ ```C#
44
+ using System.Collections;
45
+ using UnityEngine;
46
+ using UnityEngine.SceneManagement;
47
+
48
+ public class SceneBController : MonoBehaviour
49
+ {
50
+ private IEnumerator Start()
51
+ {
52
+ var cube = GameObject.Find("Cube");
53
+ var currentScene = SceneManager.GetActiveScene();
54
+
55
+ Debug.LogFormat("Found cube: {0}", cube);
56
+
57
+ Debug.Log("Hit Space to continue...");
58
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
59
+
60
+ Debug.LogFormat("Trying to move {0} from {1} to {2}...", cube.name, cube.scene.name, currentScene.name);
61
+ SceneManager.MoveGameObjectToScene(cube, currentScene);
62
+ yield return new WaitForEndOfFrame();
63
+
64
+ Debug.Log("Hit Space to continue...");
65
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
66
+
67
+ Debug.Log("Go to SceneC...");
68
+ yield return SceneManager.LoadSceneAsync("SceneC");
69
+ }
70
+ }
71
+ ```
72
+
73
+ ※ちなみに、先の追記で挙げたマニュアル「Multi-Scene editing」の「Tips and tricks」の節に
74
+ > It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads. Instead, create a manager scene that has all your managers and use SceneManager.LoadScene(<path>, LoadSceneMode.Additive) and SceneManager.UnloadScene to manage your game progress.
75
+
76
+ __シーンロードをまたいで生存させたいマネージャーGameObjectを残すために、DontDestroyOnLoadを使用するのは避けることが推奨されます。代わりに、すべてのマネージャーを配置したマネージャーシーンを作り、SceneManager.LoadScene(<path>, LoadSceneMode.Additive)とSceneManager.UnloadSceneを使ってゲーム進行を管理します。__
77
+
78
+ とあったので、ゲーム中ずっと残るオブジェクトの配置先としては、DontDestroyOnLoadよりもマニュアルのアドバイス通りマネージャーシーンを作った方がいいかもしれませんね。

1

ダメかもしれないため追記

2018/03/04 00:00

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -1,1 +1,9 @@
1
- Unityが手元にないためまだ試していませんが、[【Unity】俺はまだSceneManagerを全力で使っていない! - うら干物書き](http://www.urablog.xyz/entry/2017/04/09/180155)によるとルート直下のオブジェクトなら[SceneManager](https://docs.unity3d.com/jp/current/ScriptReference/SceneManagement.SceneManager.html)の[MoveGameObjectToScene](https://docs.unity3d.com/jp/current/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html)でシーン間を移動できるそうです。これを使ってみてはいかがでしょうか?
1
+ Unityが手元にないためまだ試していませんが、[【Unity】俺はまだSceneManagerを全力で使っていない! - うら干物書き](http://www.urablog.xyz/entry/2017/04/09/180155)によるとルート直下のオブジェクトなら[SceneManager](https://docs.unity3d.com/jp/current/ScriptReference/SceneManagement.SceneManager.html)の[MoveGameObjectToScene](https://docs.unity3d.com/jp/current/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html)でシーン間を移動できるそうです。これを使ってみてはいかがでしょうか?
2
+
3
+ ### [追記]
4
+ 実験せずに先走って書き込んでしまいましたが、[マニュアル](https://docs.unity3d.com/Manual/MultiSceneEditing.html)によると
5
+
6
+ > You do not have access to the DontDestroyOnLoad scene and it is not available at runtime.
7
+
8
+ とのことで、DontDestroyOnLoadは特別扱いらしいため、もしかするとダメかもしれません。後で私も試してみようかと思いますが、ダメだった場合、もし適当な代替手法を思い付いたら追記します...
9
+ DontDestroyOnLoadからオブジェクトを移動させたい理由もご説明いただけるとヒントになるかもしれません。