接着上一篇“Unity资源打包Addressable AA包”
还是先贴代码
LoadPath.cs 中定义一个远程服务器的地址,这个地址将来在代码中会修改
namespace HFS
{public class LoadPath{public static string Path = "http://172.18.216.168:6565/HFS/1/";}
}
远程服务器HFS文件夹下创建两个子文件夹,用来切换不同的资源
Load.cs 增加一个InputField输入框用来输入不同远程路径
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.UI;public class Load : MonoBehaviour
{public InputField inputField;public Button button;private GameObject cubeObj;private UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> handle;void Start(){inputField.text = HFS.LoadPath.Path;inputField.onEndEdit.AddListener((text) =>{HFS.LoadPath.Path = inputField.text;});button.onClick.AddListener(() =>{// 加载资源handle = Addressables.LoadAssetAsync<GameObject>("Assets/Prefab/Cube.prefab");handle.Completed += (obj) =>{// 实例化cubeObj = Instantiate(obj.Result,transform);};});}// 释放资源public void Release_Resource(){if (null != cubeObj)Destroy(cubeObj);Addressables.Release(handle);}
}
Addressables工具对远程路径的配置
Remote.LoadPath中用大括号包含静态变量的地址
{HFS.LoadPath.Path}
替换不同的预制体,打两个包。资源分别放在1和2文件夹中。
运行程序,InputField中输入不同的地址会加载出不同的模型。