「Unity/Addressable/シーンロード」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→シーンをロード) |
(→シーンをロード) |
||
(同じ利用者による、間の2版が非表示) | |||
行8: | 行8: | ||
==シーンをロード== | ==シーンをロード== | ||
<pre> | <pre> | ||
− | public class | + | using System.Collections; |
+ | using UnityEngine; | ||
+ | using UnityEngine.UI; | ||
+ | using UnityEngine.AddressableAssets; | ||
+ | using UnityEngine.ResourceManagement.AsyncOperations; | ||
+ | using UnityEngine.ResourceManagement.ResourceProviders; | ||
+ | |||
+ | public class SceneLoadScene : MonoBehaviour | ||
{ | { | ||
[SerializeField] | [SerializeField] | ||
Button loadButton; | Button loadButton; | ||
+ | [SerializeField] | ||
+ | Button unloadButton; | ||
+ | AsyncOperationHandle<SceneInstance> handle; | ||
void Start() | void Start() | ||
{ | { | ||
loadButton.onClick.AddListener(() => { | loadButton.onClick.AddListener(() => { | ||
− | + | StartCoroutine(LoadScene()); | |
+ | }); | ||
+ | unloadButton.onClick.AddListener(() => { | ||
+ | Addressables.UnloadSceneAsync(handle); | ||
}); | }); | ||
} | } | ||
IEnumerator LoadScene() | IEnumerator LoadScene() | ||
{ | { | ||
− | + | handle = Addressables.LoadSceneAsync("Assets/Scenes/SceneLoadSample2Scene.unity", UnityEngine.SceneManagement.LoadSceneMode.Additive); | |
yield return handle; | yield return handle; | ||
} | } | ||
行27: | 行40: | ||
参考:https://light11.hatenadiary.com/entry/2019/12/30/222302 | 参考:https://light11.hatenadiary.com/entry/2019/12/30/222302 | ||
+ | |||
+ | ==シーンを追加でなく遷移させる場合== | ||
+ | handle = Addressables.LoadSceneAsync("Assets/Scenes/SceneLoadSample2Scene.unity"); |
2022年9月7日 (水) 19:04時点における最新版
シーンを作成してAddressable登録
- Project内でSceneを作成する
- Sceneを選択し、Inspectorで、Addressableにチェックを付ける
- Addressableをビルドする(Unityメインメニュー/Window/Asset Management/Addressables/Groups/Build/New Build/Default Build Script
参考:https://soft-rime.com/post-3267/
シーンをロード
using System.Collections; using UnityEngine; using UnityEngine.UI; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.ResourceProviders; public class SceneLoadScene : MonoBehaviour { [SerializeField] Button loadButton; [SerializeField] Button unloadButton; AsyncOperationHandle<SceneInstance> handle; void Start() { loadButton.onClick.AddListener(() => { StartCoroutine(LoadScene()); }); unloadButton.onClick.AddListener(() => { Addressables.UnloadSceneAsync(handle); }); } IEnumerator LoadScene() { handle = Addressables.LoadSceneAsync("Assets/Scenes/SceneLoadSample2Scene.unity", UnityEngine.SceneManagement.LoadSceneMode.Additive); yield return handle; } }
参考:https://light11.hatenadiary.com/entry/2019/12/30/222302
シーンを追加でなく遷移させる場合
handle = Addressables.LoadSceneAsync("Assets/Scenes/SceneLoadSample2Scene.unity");