Preface
Unity Addressables study notes—summary
text
1. Create a new scene
I created it in the Resources/Scenes directory, as shown in the figure:
2. Create a Group of Addressables for the scene
Game1Group is a newly created Group that I use to store all the resources of Scenario 1. The configuration of the group is the same as Remote, both are remote. The loading address is the address of my locally started web server. The WebGL directory in the URL is not necessary. According to your own reality.
3. Drag the new scene and all the resources used in the scene into Game1Group
The picture above is the state after dragging.
4. Add code to the initialization scene and load the new scene Game1 using Addressables.
I added a click event to the start button in Picture 1 in the initial scene to trigger the loading of the Game1 scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
private Sprite sprite;
public Button btn;
public List<string> list;
void Start()
{
//Addressables.Instantiate("Play Button").Result.transform.position = new Vector3(2f, 2f);
foreach (string name in list)
{
Addressables.LoadAssetAsync<GameObject>(name).Completed += SpriteLoadedObj;
}
//PlayerPrefs.DeleteKey(Addressables.kAddressablesRuntimeDataPath);
Addressables.LoadAssetAsync<Sprite>("Play Button Img").Completed += SpriteLoaded;
}
private void SpriteLoaded(AsyncOperationHandle<Sprite> obj)
{
switch (obj.Status)
{
case AsyncOperationStatus.Succeeded:
sprite = obj.Result;
Debug.Log(sprite);
btn.image.sprite = sprite;
break;
case AsyncOperationStatus.Failed:
Debug.LogError("Sprite load failed.");
break;
default:
//case AsyncOperationStatus.None:
break;
}
}
private void SpriteLoadedObj(AsyncOperationHandle<GameObject> obj)
{
switch (obj.Status)
{
case AsyncOperationStatus.Succeeded:
GameObject a = Instantiate(obj.Result);
a.transform.position = new Vector2(2f, 2f);
break;
case AsyncOperationStatus.Failed:
Debug.LogError("Sprite load failed.");
break;
default:
//case AsyncOperationStatus.None:
break;
}
}
// Update is called once per frame
void Update()
{
}
public void onClick()
{
Addressables.LoadSceneAsync("Game1");
}
}
5. Scenario 1: Create an object using Addressables and drag an object into the Hierarchy, a total of 2 objects.
Below is an object dragged into scene 1.
Below is a C# script randomly bound to the GameObject to create an object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class Game1Controller : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Addressables.LoadAssetAsync<GameObject>("Layer 10").Completed += SpriteLoadedObj;
}
// Update is called once per frame
private void SpriteLoadedObj(AsyncOperationHandle<GameObject> obj)
{
switch (obj.Status)
{
case AsyncOperationStatus.Succeeded:
GameObject a = Instantiate(obj.Result);
a.transform.position = new Vector2(2f, 2f);
break;
case AsyncOperationStatus.Failed:
Debug.LogError("Sprite load failed.");
break;
default:
//case AsyncOperationStatus.None:
break;
}
}
}