2017-05-03 36 views
0

我使用ObjLoader類(http://wiki.unity3d.com/index.php?title=ObjImporter導入的obj模型爲Unity3D

不幸的是這並沒有爲我工作中發現的其他幾個職位有類似的問題,也是一個代碼,就可以(Loading a .OBJ into Unity at runtime)因爲我實際上收到了一個錯誤,而其他人卻沒有。

我得到了「IndexOutOfRangeException」,這通常是一個簡單的初學者使用數組的錯誤,但由於代碼似乎適用於其他人,我猜這是誤導,問題更深。

我想問題是,我想附加網格到一個實際的遊戲對象,但我想不出,如何正確地做到這一點。

任何建議表示讚賞。

我的代碼

 // 1. Create an controller (this will be the pivot point later) 
     GameObject controller = new GameObject(bo.objectName + " Controller"); 

     // 2. Create an primitive object, and set it as child 
     GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); 
     sphere.transform.position = new Vector3(0, 0, 0); 
     sphere.transform.SetParent(controller.transform); 
     sphere.name = bo.objectName; 

     // 3. Give it a custom mesh 
     Mesh holderMesh = sphere.GetComponent<Mesh>(); 
     ObjImporter newMesh = new ObjImporter(); 
     holderMesh = newMesh.ImportFile(bo.modelPath);//"C:/Users/Fuby/Desktop/Test Object.obj" 
     sphere.GetComponent<MeshFilter>().mesh = holderMesh; 

ObjImporter代碼

List<int> intArray = new List<int>(); 

while (j < brokenString.Length && ("" + brokenString[j]).Length > 0) 
{ 
    Vector3 temp = new Vector3(); 
    brokenBrokenString = brokenString[j].Split(splitIdentifier2, 3); //Separate the face into individual components (vert, uv, normal) 
    temp.x = System.Convert.ToInt32(brokenBrokenString[0]); 
    if (brokenBrokenString.Length > 1)         //Some .obj files skip UV and normal 
    { 
    if (brokenBrokenString[1] != "")         //Some .obj files skip the uv and not the normal 
    { 
     temp.y = System.Convert.ToInt32(brokenBrokenString[1]); 
    } 
    temp.z = System.Convert.ToInt32(brokenBrokenString[2]); //IndexOutOfRangeException 
    } 
    j++; 

    mesh.faceData[f2] = temp; 
    intArray.Add(f2); 
    f2++; 
} 
+1

猜測,您的.obj是否包含位置,法線和UV的頂點?我認爲如果其中一個屬性丟失,就會發生錯誤。 – Gusman

+0

我只是在Cinema4D中創建了一個多維數據集,並將其導出爲.obj ...我不確定.obj包含的是什麼,我該如何檢查? – Fuby

+0

https://en.wikipedia.org/wiki/Wavefront_.obj_file – Gusman

回答

0

解決。上面的代碼工作。就像古斯曼所說的,.objs需要一張紫外線貼圖。不過,問題在於.objs的C4D導出。只需使用Blender進行輸出。它會添加一個.mtl文件,它會工作!

感謝古斯曼的支持。