0
\$\begingroup\$

In my app I have requirement that I will be given a url and using this url I have to download a fbx model and then show it in the scene.

I am able to download the model from the url using the below code.

IEnumerator loadModel(string modelName)
{
    if (!isDownloaded)
    {

        string targetUrl = "http://XXX.XXX.X.XX:3000/" + modelName;
        Debug.Log("targetUrl" + targetUrl);
        WWW www = new WWW(targetUrl);


        yield return www;
        if (www.isDone)
        {
            Debug.Log("downloaded " + www.bytesDownloaded);
            var path = System.IO.Path.Combine(Application.dataPath+"/Resources", modelName);

            System.IO.File.WriteAllBytes(path, www.bytes);
            isDownloaded = true;

            LoadAndAttachModel(imageTargetTransform, modelName);
        }
        else
        {
            Debug.Log(www.bytesDownloaded);
        }
    }
}

And with the below code I tried using loading the model in the scene

public void LoadAndAttachModel(Transform parent, String modelName)
{
    Debug.Log(modelName);
    var path = System.IO.Path.Combine(Application.dataPath + "/Resources", modelName);
    String name = modelName.Substring(0, modelName.IndexOf("."));
    GameObject load = Resources.Load(modelName) as GameObject;
    Debug.Log(load.name);
    if (load != null)
        {
           // load.transform.parent = parent;
            load.transform.localPosition = Vector3.zero;
            load.transform.localRotation = Quaternion.Euler(new Vector3(90, 90, 0));
            load.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        }

    else
    {
        Debug.LogWarning("Can't find boneName : " + parent);
    }
}

but this code always gives exception that the gameobject load is null. I tried with the obj model and that was working fine but with fbx models this is giving exception every time.

So how can I instantiate the downloaded gameobject in my scene ?

\$\endgroup\$
10
  • \$\begingroup\$ I know this probably doesn't help your problem, but this sounds terribly insecure. \$\endgroup\$
    – Bálint
    Sep 19, 2017 at 5:52
  • \$\begingroup\$ insecure in what means? \$\endgroup\$ Sep 19, 2017 at 5:53
  • \$\begingroup\$ You're downloading something off a website. It introduces a dozen of security hole candidates. The website could be seized, the traffic could get changed by an MITM, etc. \$\endgroup\$
    – Bálint
    Sep 19, 2017 at 5:58
  • \$\begingroup\$ that is not a website, that is my app backend and I am downloading it from backend server not any third party sites. \$\endgroup\$ Sep 19, 2017 at 6:00
  • 1
    \$\begingroup\$ AFAIK, you can't download models on the fly like this and put them in your game. However, it would be possible with asset bundles . \$\endgroup\$
    – Hellium
    Sep 19, 2017 at 7:20

0

You must log in to answer this question.

Browse other questions tagged .