我使用名为简单Obj 的资源,这样我就可以导入obj,他们的材质和纹理关联。这在我的编辑器中工作正常,但不在我的独立版中。 我的 OBJ不在我的资源文件中,我从另一个使用WWW方法的文件中获取它。
这里是我如何做到的,t 他下载我的OBJ,创建一个Gameobject并将其放置在我的场景中:
private IEnumerator DownloadAndImportAllInBackground(string url, Plane newPlane)
{
string objString = null;
string mtlString = null;
Hashtable textures = null;
GameObject planeObject = null;
bool gameObjectPerGroup = false;
bool subMeshPerGroup = false;
bool usesRightHanded = true;
yield return StartCoroutine(DownloadFile(url, retval => objString = retval));
yield return StartCoroutine(DownloadFile(url.Substring(0, url.Length - 4) + ".mtl", retval => mtlString = retval));
if (mtlString != null && mtlString.Length > 0)
{
string path = url;
int lastSlash = path.LastIndexOf('/', path.Length - 1);
if (lastSlash >= 0) path = path.Substring(0, lastSlash + 1);
Hashtable[] mtls = ObjImporter.ImportMaterialSpecs(mtlString);
for (int i = 0; i < mtls.Length; i++)
{
if (mtls[i].ContainsKey("mainTexName"))
{
Texture2D texture = null;
string texUrl = path + mtls[i]["mainTexName"];
yield return StartCoroutine(DownloadTexture(texUrl, retval => texture = retval));
if (texture != null)
{
if (textures == null) textures = new Hashtable();
textures[mtls[i]["mainTexName"]] = texture;
}
}
}
}
yield return StartCoroutine(DownloadFile(url, retval => objString = retval));
if (objString != null && objString.Length > 0)
{
yield return StartCoroutine(ObjImporter.ImportInBackground(objString, mtlString, textures, retval => planeObject = retval, gameObjectPerGroup, subMeshPerGroup, usesRightHanded));
planeObject.transform.localScale = new Vector3(0.0005f, 0.0005f, 0.0005f);
if (planeObject == null)
{
Debug.Log("Null gameobject");
}
planeObject.name = newPlane.Callsign;
planeObject.transform.position = new Vector3((float)newPlane.X, (float)newPlane.Afl / (3.2808f * 1852f), (float)newPlane.Y);
planeObject.transform.eulerAngles = new Vector3(0, -180 + newPlane.Heading, 0);
planeId_Object_Dictionnary.Add(newPlane.Flight, planeObject);
}
}