将服务器的obj文件放入场景Unity中

时间:2018-04-24 19:18:57

标签: database unity3d server

我在服务器中有一个obj文件,我想下载它并通过代码将其导入我的场景中,我该怎么办?

2 个答案:

答案 0 :(得分:1)

我刚开始团结一致,但根据我迄今为止的研究是不可能的,你可以做的是在Unity中加载你的obj,将它转换成资产包,上传到你的服务器,然后下载它并根据需要将其实例化到您的应用程序

https://unity3d.com/es/learn/tutorials/topics/scripting/assetbundles-and-assetbundle-manager

https://www.youtube.com/watch?v=GHOu7EJKgkE

https://www.youtube.com/watch?v=qv7KYpxFQxs

https://www.youtube.com/watch?v=Ghb3f6dwU5E

答案 1 :(得分:0)

您可以使用unity wiki中的“objImporter.cs”在运行时加载obj文件。

要从网址加载obj,请使用以下代码:

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;

}