「Unity/Csharp/画面遷移/非同期」の版間の差分
提供: 初心者エンジニアの簡易メモ
(同じ利用者による、間の4版が非表示) | |||
行1: | 行1: | ||
+ | ==非同期画面遷移サンプル== | ||
+ | |||
<pre> | <pre> | ||
public class SampleAsyncScene : MonoBehaviour | public class SampleAsyncScene : MonoBehaviour | ||
行14: | 行16: | ||
{ | { | ||
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName); | AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName); | ||
− | |||
asyncLoad.allowSceneActivation = false; | asyncLoad.allowSceneActivation = false; | ||
− | + | Debug.Log("asyncLoad.progress=" + asyncLoad.progress + " asyncLoad.isDone=" + asyncLoad.isDone); | |
+ | // 遷移後にasyncLoad.isDoneがtrueになる | ||
while (!asyncLoad.isDone) | while (!asyncLoad.isDone) | ||
{ | { | ||
− | + | // asyncLoad.progressは0.9以上上がらないので・・ | |
− | asyncLoad.allowSceneActivation = true; | + | if (asyncLoad.progress >= 0.9f) |
+ | { | ||
+ | asyncLoad.allowSceneActivation = true; | ||
+ | } | ||
yield return null; | yield return null; | ||
} | } | ||
行27: | 行32: | ||
</pre> | </pre> | ||
− | ==参考== | + | ===参考=== |
+ | 公式:https://docs.unity3d.com/jp/current/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html | ||
+ | |||
https://nosystemnolife.com/unity_loading/ | https://nosystemnolife.com/unity_loading/ | ||
+ | |||
+ | https://joyplot.com/documents/unity-load-scene-time/ |
2022年6月30日 (木) 10:38時点における最新版
非同期画面遷移サンプル
public class SampleAsyncScene : MonoBehaviour { [SerializeField] Button button; void Start() { button.onClick.AddListener(OnClickButton); } void OnClickButton() { StartCoroutine(LoadSceneAsync("SubScene")); } IEnumerator LoadSceneAsync(string sceneName) { AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName); asyncLoad.allowSceneActivation = false; Debug.Log("asyncLoad.progress=" + asyncLoad.progress + " asyncLoad.isDone=" + asyncLoad.isDone); // 遷移後にasyncLoad.isDoneがtrueになる while (!asyncLoad.isDone) { // asyncLoad.progressは0.9以上上がらないので・・ if (asyncLoad.progress >= 0.9f) { asyncLoad.allowSceneActivation = true; } yield return null; } } }
参考
公式:https://docs.unity3d.com/jp/current/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html