前言
Unity Addressables學(xué)習(xí)筆記—匯總
例子1:加載一個圖片
1.首先創(chuàng)建一個UI Image,空白圖片,資源打包方式選擇真是部署的
2.修改遠(yuǎn)程發(fā)布和加載配置
Bulid Path選擇RemoteBuildPath
Load Path我選擇了custom,地址是http://localhost:8080/WebGL/
遇坑1 :最開始我選擇的Build Path 是 LocalBuildPath,Load Path是custom的時候報錯如下:
BuildPath for group '***' is set to the dynamic-lookup version of StreamingAssets, but LoadPath is not.
解決辦法:把Build Path 改為RemoteBuildPath后才好,我也不知道為什么不能把本地的資源放到遠(yuǎn)程資源上,可能是有區(qū)別吧,必須把遠(yuǎn)程資源打包到遠(yuǎn)程服務(wù)器上也能好理解。
3.打包放服務(wù)器里
Bulid Path選擇RemoteBuildPath,打包的文件會出現(xiàn)在ServerData文件夾里,這個文件夾在Unity里是加載不出來的,里邊是對應(yīng)的是你要打包的程序類型,我的是網(wǎng)頁所以是WebGL.我的打包文件名是:remote_assets_all_9cd0b0249adf18fa843683539bbac8b9.bundle
之后我拷貝到了一個本地的服務(wù)器上,我用的Spring Boot,因為我本身是搞Java的
pom.xml依賴就一個
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
</dependencies>
啟動函數(shù)
@SpringBootApplication
public class CDNBackend {
private static ApplicationContext context;
public static void main(String[] args) {
context = SpringApplication.run(CDNBackend.class, args);
}
public static ApplicationContext getApplicationContext() {
return context;
遇坑1.放到服務(wù)器上的spring boot靜態(tài)資源不能訪問。
我在static下邊創(chuàng)建了一個WebGL目錄當(dāng)然沒有也行,這個路徑要跟上邊的Load Path 對應(yīng),主要是.bundle這個文件下載不了,所以添加了一個配置類,解決不能訪問的問題
@Configuration
public class ShowImage extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//注:addResourceHandler("/WebGL/**")這個是配置你的URL
//addResourceLocations("classpath:/static/WebGL/")這個是你的文件目錄
registry.addResourceHandler("/WebGL/**").addResourceLocations("classpath:/static/WebGL/");
super.addResourceHandlers(registry);
}
}
4. Unity C#加載遠(yuǎn)程圖片
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 Image img;
void Start()
{
//Addressables.InstantiateAsync("Play Button");
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;
#加載完成的時候給img設(shè)置sprite
img.sprite = sprite;
break;
case AsyncOperationStatus.Failed:
Debug.LogError("Sprite load failed.");
break;
default:
//case AsyncOperationStatus.None:
break;
}
}
}
遇坑1.因為是WebGL游戲,所以是WEB訪問WEB,存在一個跨域問題。WebGL游戲報錯:
from origin ‘http://localhost:57545’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
p://localhost:8080/WebGL/remote_assets_all_9cd0b0249adf18fa843683539bbac8b9.bundle' from origin 'http://localhost:57545' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
解決方式:
在Spring Boot的服務(wù)器端添加跨域白名單類文章來源:http://www.zghlxwxcb.cn/news/detail-634904.html
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) {
//CorsRegistration addMapping(String pathPattern): 添加路徑映射,如 /admin/info,或者 /admin/**
registry.addMapping("/**")
//是否發(fā)送Cookie
.allowCredentials(true)
//放行哪些原始域, * 表示所有
.allowedOrigins(new String[]{"http://127.0.0.1:57545", "http://localhost:57545"})
//放行哪些請求方式
.allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
//放行哪些原始請求頭部信息
.allowedHeaders("*");
//暴露哪些頭部信息,不能設(shè)置為 * : .exposedHeaders();
}
}
最終效果 加載圖片成功
文章來源地址http://www.zghlxwxcb.cn/news/detail-634904.html
到了這里,關(guān)于Unity Addressables學(xué)習(xí)筆記(1)---創(chuàng)建遠(yuǎn)程服務(wù)器加載資源的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!