Unity3d发布Android版通过WWW加载本地AssetBundle资源
2015-05-06 17:42
477 查看
不久之前买了小米平板,就想着用Unity3d做个小游戏放在平板上玩玩,匆匆做了个资源加载界面就发布apk到平板上,结果提示资源加载失败,找不到资源文件。在网上找了半天资料都没解决问题,最后发现自己犯了个最愚蠢的问题,就是StreamingAssets文件夹名称有问题,我写成了StreamingAsset,真是被自己给坑到了。
下面总结下Unity3d发布Android版通过WWW加载本地AssetBundle资源的注意事项:
(1)一定要将需要加载的AssetBundle资源文件放在StreamingAssets目录下,因为Android只能加载StreamingAssets中的内容。
(2)打包AssetBundle资源包时需要打包平台为BuildTarget.Android
Windows : path = = Application.dataPath + "/StreamingAssets";
On iOS: path = Application.dataPath + "/Raw";
Android:path = "jar:file://" + Application.dataPath + "!/assets/";
Application.platform == RuntimePlatform.Android
下面是我的代码:
using System;
using System.Collections;
using UnityEngine;
namespace HKScripts
{
class HKLoadResource : MonoBehaviour
{
public static float progress = 0;
private WWW www = null;
void Awake()
{
StartCoroutine("loadEnvironment");
}
void Update()
{
if (www!=null)
{
progress = www.progress;
}
}
IEnumerator loadEnvironment()
{
string url = "";
if (Application.platform == RuntimePlatform.Android)
url = "jar:file://" + Application.dataPath + "!/assets/Environment.unity3d";
else
url = "file:///" + Application.dataPath + "/../Environment.unity3d";
Debug.LogError(url);
www = new WWW(url);
yield return www;
if (www.error != null)
{
GameObject.FindGameObjectWithTag("TipLabel").GetComponent<UILabel>().text = "路径:" + url + "错误:" + www.error;
Debug.LogError(www.error);
yield return null;
}
}
}
}
下面总结下Unity3d发布Android版通过WWW加载本地AssetBundle资源的注意事项:
(1)一定要将需要加载的AssetBundle资源文件放在StreamingAssets目录下,因为Android只能加载StreamingAssets中的内容。
(2)打包AssetBundle资源包时需要打包平台为BuildTarget.Android
using UnityEngine; using System.Collections; using UnityEditor;//必须引用此类 public class ExportAssetBundles : MonoBehaviour { /// <summary> /// 将所选择的的物体和物体有依赖关系的对象一起打包 /// </summary> [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")] static void ExportResource() { // Bring up save panel string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d"); if (path.Length != 0) { // Build the resource file from the active selection. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android); Selection.objects = selection; } } /// <summary> /// 只打包选择的物体 /// </summary> [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")] static void ExportResourceNoTrack() { // Bring up save panel string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d"); if (path.Length != 0) { // Build the resource file from the active selection. BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path); } } }(3)注意加载文件的路径,判断运行平台请用Application.platform == RuntimePlatform.Android这种方式。
Windows : path = = Application.dataPath + "/StreamingAssets";
On iOS: path = Application.dataPath + "/Raw";
Android:path = "jar:file://" + Application.dataPath + "!/assets/";
Application.platform == RuntimePlatform.Android
下面是我的代码:
using System;
using System.Collections;
using UnityEngine;
namespace HKScripts
{
class HKLoadResource : MonoBehaviour
{
public static float progress = 0;
private WWW www = null;
void Awake()
{
StartCoroutine("loadEnvironment");
}
void Update()
{
if (www!=null)
{
progress = www.progress;
}
}
IEnumerator loadEnvironment()
{
string url = "";
if (Application.platform == RuntimePlatform.Android)
url = "jar:file://" + Application.dataPath + "!/assets/Environment.unity3d";
else
url = "file:///" + Application.dataPath + "/../Environment.unity3d";
Debug.LogError(url);
www = new WWW(url);
yield return www;
if (www.error != null)
{
GameObject.FindGameObjectWithTag("TipLabel").GetComponent<UILabel>().text = "路径:" + url + "错误:" + www.error;
Debug.LogError(www.error);
yield return null;
}
}
}
}
相关文章推荐
- 【Unity3d】关于AssetBundle.CreateFromFile()方法加载资源失败解决方法
- Unity3d Android无法加载assetbundle的问题
- Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解
- Unity3d 发布WebGL 并加载AssetBundle
- Unity3d-WWW实现图片资源显示以及保存和本地加载
- Unity3D 关于资源加载(Resources和AssetBundle)和内存管
- Unity3D之AssetBundle资源加载封装
- Unity3d热更新全书-资源加载(一):从AssetBundle说起
- Unity3D之AssetBundle资源加载封装
- Unity3D基础学习之AssetBundle 资源包创建与加载
- Unity3D之AssetBundle【3】AssetBundle资源加载管理器
- C# Unity用AssetBundle加载本地资源 (1)
- Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解
- Unity3D基础学习之AssetBundle 资源包创建与加载
- unity5 通过 WWW 加载 Assetbundle 中的 texture
- Unity3d-WWW实现图片资源显示以及保存和本地加载
- 详谈 Unity3D AssetBundle 资源加载,结合实际项目开发实例
- Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解
- Unity3D 关于资源加载(Resources和AssetBundle)和内存管理
- Unity3D AssetBundle 资源加载-IOS