Вы можете использовать "objImporter.cs" из unity wiki для загрузки файла obj во время выполнения.
Чтобы загрузить объект из URL, используйте этот код:
void LoadObjFromUrl(string url){
//you can do more test here to check if the url correct
if (String.IsNullOrEmpty(url)) {
return;
}
StartCoroutine(LoadObjFromUrlCoroutine(url));
}
IEnumerator LoadObjFromUrlCoroutine(string url) {
Mesh mesh;
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
mesh = ObjImporter.Instance.ImportFile(www.downloadHandler.text, true);
GameObject obj = new GameObject();
MeshFilter meshFilter = obj.AddComponent<MeshFilter>();
MeshRenderer meshRenderer = obj.AddComponent<MeshRenderer>();
meshFilter.mesh = mesh;
//add a BoxCollider to the loaded object
obj.AddComponent<BoxCollider>();
//this method will set the scale of the loaded model equale to 1 unity units (like the size of the default unity cube)
ConvertMeshToUnityScale(obj,1);
}
public void ConvertMeshToUnityScale(GameObject theGameObject, float newSize,string axis="y") {
Renderer renderer = theGameObject.GetComponent<Renderer>();
float size = renderer.bounds.size.y;
if (axis.ToLower() == "x") {
size = renderer.bounds.size.x;
} else if (axis.ToLower() == "z") {
size = renderer.bounds.size.z;
}
Vector3 rescale = theGameObject.transform.localScale;
rescale.x = newSize * rescale.x / size;
rescale.y = newSize * rescale.y / size;
rescale.z = newSize * rescale.z / size;
theGameObject.transform.localScale = rescale;
}