1. Create the object first
What I do is:
- First drag a picture to Resources/img, which is the folder I selected.
2. Drag the picture to the Hierarchy to become an object, and then drag the object to Resources/prefabs. The word in the picture is wrong. Hahahaha.
In this way, the image becomes a prefab, and then the instance in the Hierarchy is deleted and the object is created using Addressables using C# code.
3. Drag the image and prefab into the addressables group I created, named Remote
Package it and put it under SpringBoot
Remote group settings
A ServerData folder will be generated in the local project directory. This folder will not be displayed in the Unity editor. You can find it yourself in the directory. Below is a screenshot of my own directory.
The following is placed under the WebGL of SpringBoot. For details, check out my first post for the settings corresponding to SpringBoot. If you have Tomcat or something, you can do it.
First post: Unity Addressables study notes (1)—Create a remote server to load resources
These 3 files are the 3 files I generated. One of them should be in my next post. Anyway, just copy them all.
code
In the list is the name of the object I want to load. According to the screenshot above, I am loading Layer 1 and Layer 8.
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()
{
}
}