在运行时将 .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屋!