回答編集履歴

2

実験結果を追記

2018/03/04 00:00

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -15,3 +15,141 @@
15
15
  とのことで、DontDestroyOnLoadは特別扱いらしいため、もしかするとダメかもしれません。後で私も試してみようかと思いますが、ダメだった場合、もし適当な代替手法を思い付いたら追記します...
16
16
 
17
17
  DontDestroyOnLoadからオブジェクトを移動させたい理由もご説明いただけるとヒントになるかもしれません。
18
+
19
+
20
+
21
+ ### [さらに追記]
22
+
23
+ どうやらPlayerPrefsの方が適していたご様子ですが、どこか別の場面で使う機会があるかもしれませんので、試してみた結果を追記します。
24
+
25
+ シーンAにあるキューブをDontDestroyOnLoad指定してから別のシーンBをロード、キューブをシーンBに移動してみたところ、どうやらちゃんと移動してくれるようでした。ここからさらに別のシーンCをロードすると、キューブも一緒に消えました。
26
+
27
+ ![実行結果](ee54fa64429d5c5c4dc2d19d7e63890f.gif)
28
+
29
+ なお、制御のためにシーンAには下記のスクリプトを持つオブジェクトを配置し、
30
+
31
+ ```C#
32
+
33
+ using System.Collections;
34
+
35
+ using UnityEngine;
36
+
37
+ using UnityEngine.SceneManagement;
38
+
39
+
40
+
41
+ public class SceneAController : MonoBehaviour
42
+
43
+ {
44
+
45
+ public GameObject Cube;
46
+
47
+
48
+
49
+ private IEnumerator Start()
50
+
51
+ {
52
+
53
+ Debug.Log("Hit Space to continue...");
54
+
55
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
56
+
57
+
58
+
59
+ Debug.LogFormat("Move {0} to DontDestroyOnLoad.", this.Cube.name);
60
+
61
+ DontDestroyOnLoad(this.Cube);
62
+
63
+ yield return new WaitForEndOfFrame();
64
+
65
+
66
+
67
+ Debug.Log("Hit Space to continue...");
68
+
69
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
70
+
71
+
72
+
73
+ Debug.Log("Go to SceneB...");
74
+
75
+ yield return SceneManager.LoadSceneAsync("SceneB");
76
+
77
+ }
78
+
79
+ }
80
+
81
+ ```
82
+
83
+ シーンBには下記のスクリプトを持つオブジェクトを配置しています。
84
+
85
+ ```C#
86
+
87
+ using System.Collections;
88
+
89
+ using UnityEngine;
90
+
91
+ using UnityEngine.SceneManagement;
92
+
93
+
94
+
95
+ public class SceneBController : MonoBehaviour
96
+
97
+ {
98
+
99
+ private IEnumerator Start()
100
+
101
+ {
102
+
103
+ var cube = GameObject.Find("Cube");
104
+
105
+ var currentScene = SceneManager.GetActiveScene();
106
+
107
+
108
+
109
+ Debug.LogFormat("Found cube: {0}", cube);
110
+
111
+
112
+
113
+ Debug.Log("Hit Space to continue...");
114
+
115
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
116
+
117
+
118
+
119
+ Debug.LogFormat("Trying to move {0} from {1} to {2}...", cube.name, cube.scene.name, currentScene.name);
120
+
121
+ SceneManager.MoveGameObjectToScene(cube, currentScene);
122
+
123
+ yield return new WaitForEndOfFrame();
124
+
125
+
126
+
127
+ Debug.Log("Hit Space to continue...");
128
+
129
+ yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
130
+
131
+
132
+
133
+ Debug.Log("Go to SceneC...");
134
+
135
+ yield return SceneManager.LoadSceneAsync("SceneC");
136
+
137
+ }
138
+
139
+ }
140
+
141
+ ```
142
+
143
+
144
+
145
+ ※ちなみに、先の追記で挙げたマニュアル「Multi-Scene editing」の「Tips and tricks」の節に
146
+
147
+ > 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.
148
+
149
+
150
+
151
+ __シーンロードをまたいで生存させたいマネージャーGameObjectを残すために、DontDestroyOnLoadを使用するのは避けることが推奨されます。代わりに、すべてのマネージャーを配置したマネージャーシーンを作り、SceneManager.LoadScene(<path>, LoadSceneMode.Additive)とSceneManager.UnloadSceneを使ってゲーム進行を管理します。__
152
+
153
+
154
+
155
+ とあったので、ゲーム中ずっと残るオブジェクトの配置先としては、DontDestroyOnLoadよりもマニュアルのアドバイス通りマネージャーシーンを作った方がいいかもしれませんね。

1

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

2018/03/04 00:00

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -1 +1,17 @@
1
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
+
5
+ ### [追記]
6
+
7
+ 実験せずに先走って書き込んでしまいましたが、[マニュアル](https://docs.unity3d.com/Manual/MultiSceneEditing.html)によると
8
+
9
+
10
+
11
+ > You do not have access to the DontDestroyOnLoad scene and it is not available at runtime.
12
+
13
+
14
+
15
+ とのことで、DontDestroyOnLoadは特別扱いらしいため、もしかするとダメかもしれません。後で私も試してみようかと思いますが、ダメだった場合、もし適当な代替手法を思い付いたら追記します...
16
+
17
+ DontDestroyOnLoadからオブジェクトを移動させたい理由もご説明いただけるとヒントになるかもしれません。