赞
踩
记录一下Addressables-Sample项目中的脚本和学习的笔记,主要是熟悉API及其使用方式.方便日后工作查询.
项目地址:
Unity官方教程
此示例演示了通过Addressables加载场景
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.UI; public class NextScene : MonoBehaviour { public string NextSceneAddress; Button m_NextButton; // Use this for initialization void Start () { m_NextButton = GetComponent<Button>(); m_NextButton.onClick.AddListener(OnButtonClick); } void OnButtonClick() { Addressables.LoadSceneAsync(NextSceneAddress); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.UI; public class AddObject : MonoBehaviour { public string addressToAdd; Button m_AddButton; void Start() { m_AddButton = GetComponent<Button>(); m_AddButton.onClick.AddListener(OnButtonClick); } void OnButtonClick() { var randSpot = new Vector3(Random.Range(-5, 1), Random.Range(-10, 10), Random.Range(0, 100)); Addressables.InstantiateAsync("ball", randSpot, Quaternion.identity); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.ResourceProviders; using UnityEngine.SceneManagement; using UnityEngine.Serialization; using UnityEngine.UI; public class AddScenes : MonoBehaviour { public string addressToAdd; Button m_AddButton; public TMPro.TMP_Text textArea; SceneInstance m_LoadedScene; bool m_ReadyToLoad = true; // Use this for initialization void Start () { m_AddButton = GetComponent<Button>(); m_AddButton.onClick.AddListener(OnButtonClick); textArea.text = "Load " + addressToAdd; } void OnButtonClick() { if(string.IsNullOrEmpty(addressToAdd)) Debug.LogError("Address To Add not set."); else { if (m_ReadyToLoad) Addressables.LoadSceneAsync(addressToAdd, LoadSceneMode.Additive).Completed += OnSceneLoaded; else { Addressables.UnloadSceneAsync(m_LoadedScene).Completed += OnSceneUnloaded; } } } void OnSceneUnloaded(AsyncOperationHandle<SceneInstance> obj) { if (obj.Status == AsyncOperationStatus.Succeeded) { textArea.text = "Reload " + addressToAdd; m_ReadyToLoad = true; m_LoadedScene = new SceneInstance(); } else { Debug.LogError("Failed to unload scene at address: " + addressToAdd); } } void OnSceneLoaded(AsyncOperationHandle<SceneInstance> obj) { if (obj.Status == AsyncOperationStatus.Succeeded) { textArea.text = "Unload " + addressToAdd; m_LoadedScene = obj.Result; m_ReadyToLoad = false; } else { Debug.LogError("Failed to load scene at address: " + addressToAdd); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。