###### tags: `AddressableAssets` `Unity` # WebGL: Addressable Assets * Unity Version: 2020.3 LTS * Addressable Assets Version: 1.18.19 * Support platform: MAC Safari, MAC Chrome, Windows Chrome, Windows Edge, Windows Firefox. * Test Success: Addressable Assets Loader https://hello1334466.000webhostapp.com/loader/index.html Studio https://hello1334466.000webhostapp.com/studio/index.html ## Framework ![](https://i.imgur.com/M1KLrVH.png) ## Addressable Profiles ### Public hosting ![](https://i.imgur.com/xcrDO9M.png) Private hosting ![](https://i.imgur.com/sn7wDO1.png) ![](https://i.imgur.com/LKFiaSb.png) ### Addressable Asset Settings ![](https://i.imgur.com/U6cmUAW.png) Addressable Asset Group ![](https://i.imgur.com/amYLMRd.png) ### Asset Group Templates ![](https://i.imgur.com/MlUYVW5.png) Asset Groups ![](https://i.imgur.com/O3XUFIY.png) ### Set LoadPath to ```StreamingAssets``` ![](https://i.imgur.com/82fnn8w.png) BuildPath: ``` [UnityEngine.AddressableAssets.Addressables.BuildPath] ``` LoadPath: ``` {UnityEngine.AddressableAssets.Addressables.RuntimePath} ``` Ref. * https://docs.unity3d.com/Packages/com.unity.addressables@0.7/manual/AddressableAssetsGettingStarted.html * https://zxt385189207.github.io/2019/08/27/Unity%E5%8F%AF%E5%AF%BB%E5%9D%80%E8%B5%84%E4%BA%A7%E7%B3%BB%E7%BB%9FAddressableAssetSystem/#StreamingAssets%E4%B8%AD%E7%9A%84%E6%9C%AC%E5%9C%B0%E6%95%B0%E6%8D%AE * ## Addressable Load ![](https://i.imgur.com/uX93SMt.png) ``` using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; public class Test : MonoBehaviour { //bundle資源的label public AssetLabelReference characterLabel; public IList<GameObject> characters; // Start is called before the first frame update void Start() { //LoadCubeAssetAsync(); //StartCoroutine(LoadCubeAsset()); Addressables.LoadAssetsAsync<GameObject>(characterLabel, null).Completed += OnLoadDone; } private void OnLoadDone(AsyncOperationHandle<IList<GameObject>> obj) { Debug.Log("加載資源完成"); Debug.Log(obj); Debug.Log("獲得加載資源:"); Debug.Log(obj.Result); // In a production environment, you should add exception handling to catch scenarios such as a null result; if (obj.GetType() != null) { characters = obj.Result; Instantiate(characters[0]); } } private async void LoadCubeAssetAsync() { var cube = Addressables.LoadAssetAsync<GameObject>("Assets/Bear.prefab"); await cube.Task; Instantiate(cube.Result); } private IEnumerator LoadCubeAsset() { var cube = Addressables.LoadAssetAsync<GameObject>("Assets/Bear.prefab"); yield return cube; Instantiate(cube.Result); } } ``` * 注意 - WebGL 無法使用 Task 方法 ![](https://i.imgur.com/Z4kLdys.png) --- ## WebGL: Server configuration code samples https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html * This configuration file should be uploaded to the server as ```"<Application Folder>/Build/.htaccess"``` ![](https://i.imgur.com/kmq2vBf.png) 使用 UnityWebRequest 跨網域無法存取解決辦法: https://docs.unity3d.com/560/Documentation/Manual/webgl-networking.html * Add code to .htaccess ``` AddType application/wasm wasm Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE" Header set Access-Control-Allow-Headers "Content-Type, Authorization" ``` Full code ```.htaccess``` ``` # This configuration file should be uploaded to the server as "<Application Folder>/Build/.htaccess" # NOTE: "mod_mime" Apache module must be enabled for this configuration to work. <IfModule mod_mime.c> AddType application/wasm wasm Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE" Header set Access-Control-Allow-Headers "Content-Type, Authorization" # The following lines are required for builds without decompression fallback, compressed with gzip RemoveType .gz AddEncoding gzip .gz AddType application/octet-stream .data.gz AddType application/wasm .wasm.gz AddType application/javascript .js.gz AddType application/octet-stream .symbols.json.gz # The following lines are required for builds without decompression fallback, compressed with Brotli RemoveType .br RemoveLanguage .br AddEncoding br .br AddType application/octet-stream .data.br AddType application/wasm .wasm.br AddType application/javascript .js.br AddType application/octet-stream .symbols.json.br # The following line improves loading performance for uncompressed builds AddType application/wasm .wasm # Uncomment the following line to improve loading performance for gzip-compressed builds with decompression fallback # AddEncoding gzip .unityweb # Uncomment the following line to improve loading performance for brotli-compressed builds with decompression fallback # AddEncoding br .unityweb </IfModule> ```