Unity Addressables study notes (3)---Loading remote scene scenes

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:
Insert image description here

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.
Insert image description here

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.
Insert image description here
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;
        }
    }
}

6. Key point: Use Addressables to load remote scenes, so do not package this scene together during build. The picture below only has an initial scene without the Game1 scene.

Insert image description here

7. Package it with Addressables, and then copy all the files under ServerData to the Web server. If you don’t know what to say, look at the first two notes. Then directly Unity Build And Run, the rendering:

Enter the initialization scene:

Insert image description here

Click the small green button to enter Scene 2 (scene name in the example: Game1). The two objects, one is the prefab dragged into the Hierarchy from the Game1 scene, and the other is the object created through Addressables using C# code, are both out!

Insert image description here

Guess you like

Origin blog.csdn.net/lwb314/article/details/131018375