不用 AssetBundle呢
时间: 2023-10-23 15:08:31 浏览: 95
如果你不想使用 AssetBundle,也可以直接从 StreamingAssets 文件夹中读取模型文件并实例化。下面是一个简单的示例代码,可以加载一个名为“myModel.obj”的模型文件,并实例化其中的一个模型:
```c#
IEnumerator LoadModel(string path)
{
// 获取模型文件的完整路径
string uri = System.IO.Path.Combine(Application.streamingAssetsPath, path);
// 创建一个 UnityWebRequest 对象,用于从文件中读取模型数据
UnityWebRequest www = UnityWebRequest.Get(uri);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
// 将obj格式的模型文件转换为Unity的Mesh对象
Mesh mesh = OBJLoader.LoadOBJ(www.downloadHandler.text);
// 创建GameObject来显示模型
GameObject obj = new GameObject();
obj.AddComponent<MeshFilter>().mesh = mesh;
obj.AddComponent<MeshRenderer>().material = new Material(Shader.Find("Standard"));
// 将GameObject放在场景中的合适位置
obj.transform.position = new Vector3(0, 0, 0);
}
}
```
注意,上面的示例代码使用了一个名为OBJLoader的脚本来将obj格式的模型文件转换为Unity的Mesh对象。你需要自己编写这个脚本或者使用现成的开源代码库。另外,需要注意的是,从文件中读取模型数据也可能会比较耗时,所以最好将加载过程放在协程中,以免阻塞主线程。
阅读全文
相关推荐












