在运行时将.OBJ加载到Unity中 [英] Loading a .OBJ into Unity at runtime
问题描述
我的工作是编写代码,以便在运行时将.OBJ加载到Unity中. Unity在 Wiki 页面中提供了示例代码.我使用以下代码来使用链接中给出的类:
My job is to write a code which loads a .OBJ into Unity in runtime. Unity has provided a sample code in it's wiki page. I used the following code to use the class given in the link:
public class Main : MonoBehaviour {
// Use this for initialization
void Start () {
Mesh holderMesh = new Mesh ();
ObjImporter newMesh = new ObjImporter();
holderMesh = newMesh.ImportFile("C:/Users/cvpa2/Desktop/ng/output.obj");
}
我在Unity Monodevelop中没有收到任何错误,但是没有加载任何模型. 可能是什么解决方案?
I'm not getting any errors in Unity Monodevelop, but neither is the model loaded. What may be the probable solution?
推荐答案
仅创建Mesh对象是不够的.您将至少还要做两件事:
Just creating a Mesh object is not enough. You will have to do at least two more things:
- 创建一个MeshRenderer组件
- 创建一个MeshFilter组件
因此,如果将代码更改为以下代码,则至少应看到网格(如果已成功创建).
So if you change your code to the following you should at least see your mesh if it has been successfully created.
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour
{
// Use this for initialization
void Start()
{
Mesh holderMesh = new Mesh();
ObjImporter newMesh = new ObjImporter();
holderMesh = newMesh.ImportFile("C:/Users/cvpa2/Desktop/ng/output.obj");
MeshRenderer renderer = gameObject.AddComponent<MeshRenderer>();
MeshFilter filter = gameObject.AddComponent<MeshFilter>();
filter.mesh = holderMesh;
}
}
从那里开始,您仍然必须分配材料(如果已加载/创建)和其他类似的东西,但这只是一个开始.
From there on out you'd still have to assign a material (if loaded/created) and other such things, but it would be a start.
这篇关于在运行时将.OBJ加载到Unity中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!