如何加载数据库Fbx文件,不是assetbundle

发布网友 发布时间:2022-04-26 02:14

我来回答

2个回答

热心网友 时间:2022-04-09 22:43

由于我们要将模型资源放在远程的服务器端,但如果直接放fbx模型是不可以加载的,所以我们可以将fbx做成预设或者是直接将其打包成assetbundle格式的,然后通过www来加载获取。

1.首先要讲一下不同平台下的一个StreamingAssets路径,这是不同的。

//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL =
#if UNITY_ANDROID //安卓
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE //iPhone
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR //windows平台和web平台
"file://" + Application.dataPath + "/StreamingAssets/";
#else
string.Empty;
#endif

这就获取到了不同平台的一个路径,我们可以将打包的文件放在这些路径下,然后再从这路径去读取资源。

2.关于打包assetbundle的脚本

using UnityEngine;
using System.Collections;
using UnityEditor;

public class Test : Editor
{
//打包单个
[MenuItem("Custom Editor/Create AssetBunldes Main")]
static void CreateAssetBunldesMain ()
{
//获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);

//遍历所有的游戏对象
foreach (Object obj in SelectedAsset)
{
//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
//StreamingAssets是只读路径,不能写入
//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
if (BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)) {
Debug.Log(obj.name +"资源打包成功");
}
else
{
Debug.Log(obj.name +"资源打包失败");
}
}
//刷新编辑器
AssetDatabase.Refresh ();

}

[MenuItem("Custom Editor/Create AssetBunldes ALL")]
static void CreateAssetBunldesALL ()
{

Caching.CleanCache ();

string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";

Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);

foreach (Object obj in SelectedAsset)
{
Debug.Log ("Create AssetBunldes name :" + obj);
}

//这里注意第二个参数就行
if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {
AssetDatabase.Refresh ();
} else {

}
}

[MenuItem("Custom Editor/Create Scene")]
static void CreateSceneALL ()
{
//清空一下缓存
Caching.CleanCache();
string Path = Application.dataPath + "/MyScene.unity3d";
string []levels = {"Assets/Level.unity"};
//打包场景
BuildPipeline.BuildPlayer( levels, Path,BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh ();
}

}

前提要新手动创建一个StreamingAssets文件夹
3.读取资源,这里只举例从本地读取,跟从网络读取是一样的,可以参考官方文档:

using UnityEngine;
using System.Collections;

public class RunScript : MonoBehaviour
{

//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";
#else
string.Empty;
#endif

void OnGUI()
{
if(GUILayout.Button("Main Assetbundle"))
{
//StartCoroutine(LoadMainGameObject(PathURL + "Prefab0.assetbundle"));
//StartCoroutine(LoadMainGameObject(PathURL + "Prefab1.assetbundle"));

StartCoroutine(LoadMainCacheGameObject(PathURL + "Prefab0.assetbundle"));
StartCoroutine(LoadMainCacheGameObject(PathURL + "Prefab1.assetbundle"));
}

if(GUILayout.Button("ALL Assetbundle"))
{
StartCoroutine(LoadALLGameObject(PathURL + "ALL.assetbundle"));
}

if(GUILayout.Button("Open Scene"))
{
StartCoroutine(LoadScene());
}

}

//读取一个资源

private IEnumerator LoadMainGameObject(string path)
{
WWW bundle = new WWW(path);

yield return bundle;

//加载到游戏中
yield return Instantiate(bundle.assetBundle.mainAsset);

bundle.assetBundle.Unload(false);
}

//读取全部资源

private IEnumerator LoadALLGameObject(string path)
{
WWW bundle = new WWW(path);

yield return bundle;

//通过Prefab的名称把他们都读取出来
Object obj0 = bundle.assetBundle.Load("Prefab0");
Object obj1 = bundle.assetBundle.Load("Prefab1");

//加载到游戏中
yield return Instantiate(obj0);
yield return Instantiate(obj1);
bundle.assetBundle.Unload(false);
}

private IEnumerator LoadMainCacheGameObject(string path)
{
WWW bundle = (path,5);

yield return bundle;

//加载到游戏中
yield return Instantiate(bundle.assetBundle.mainAsset);

bundle.assetBundle.Unload(false);
}

private IEnumerator LoadScene()
{
WWW download = ("file://"+Application.dataPath + "/MyScene.unity3d", 1);

yield return download;
var bundle = download.assetBundle;
Application.LoadLevel ("Level");
}
}

热心网友 时间:2022-04-10 00:01

用插件可以解决

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com