0
\$\begingroup\$

I am developing a desktop application that want to load the 3D model from desktop. Using file explorer user wants to choose the 3d model then it want to show in the application scene.I tried to find the object, the path is detecting but if i tried to Instantiate it on unity it showing this error.

ArgumentException: The Object you want to instantiate is null.

here the code is used

public class ReadData : MonoBehaviour {
string path;
GameObject go;
public void OpenExplorer(){
    path = EditorUtility.OpenFilePanel("Overright with model", "", "dae");
    Debug.Log(path);
    GetObj();
}

void GetObj()
{
    if(path!=null)
    {
        UpdateObject();
    }
}

void UpdateObject()
{
    WWW www = new WWW("file:///" + path);
    go = GameObject.Find(path);
    Instantiate(go);
}}

Anybody please help me to do this. Thanks in Advance.

\$\endgroup\$
1
\$\begingroup\$

GameObject.Find(path) searches the currently loaded scenes for an existing GameObject with a specific name. It does not create a new GameObject using data loaded from a file path.

So, this line would only do something useful if, inside your scene, you had a GameObject named "file://C:/path/to/my/model/file.dae" - then you'd instantiate a copy of that existing scene object, not create a new mesh using the data at that file path on your hard drive.

The more fundamental problem is this: Unity's model loaders are part of the Editor, not part of the runtime. When you build your application, Unity "bakes" its assets into an internal binary format that's convenient for runtime use, so when you start the application it can drop the assets straight into video memory without taking extra time to parse a complicated text or xml-based file format.

So,if you want to load a new model file from a non-pre-digested format at runtime, it's up to you to parse the file's contents and populate an appropriate instance of the Mesh class.

Your spawning method would then be a coroutine that looks a bit like this:

IEnumerator SpawnMeshFromPath(string path) {

   WWW www = new WWW("file:///" + path);

   // Wait until the file is loaded before trying to read it.
   yield return www;

   // Use some library code to parse the COLLADA XML into Unity data types.
   var colladaData = SomeColladaLibrary.Parse(www.text);
   var colladaMesh = colladaData.GetMesh(0);

   // Populate a new Unity Mesh instance with the data loaded from the file.
   Mesh mesh = new Mesh();
   mesh.vertices = colladaMesh.vertices;
   mesh.uv = colladaMesh.textureCoordinates;
   mesh.triangles = colladaMesh.triangleIndices;

   // Create a GameObject in the scene to use to render this mesh.
   var go = new GameObject("Loaded mesh");
   var filter = go.AddComponent<MeshFilter>();
   var renderer = go.AddComponent<MeshRenderer>();

   // Reference the mesh to render from the MeshFilter component.
   filter.sharedMesh = mesh;
}

The tough part is that SomeColladaLibrary.Parse(www.text) line - this is a placeholder for whatever you use to do the heavy lifting of reading the XML data structures in the COLLADA file you're opening and digesting them to arrays of Vector3s and the like that you can use in Unity.

You may be able to find existing libraries for Unity, or port one from one of the open source COLLADA-reading libraries out there, being mindful of the licence the code has been released under. You can find the complete spec for the file format here if you want to write your own loader routine from scratch, but it will be a significant undertaking, as COLLADA files are very complex and flexible.

\$\endgroup\$
6
  • \$\begingroup\$ Thanks for the reply. i hopefully wait surely you have some solution for my question. I tried to followed your instructions but still getting some errors. i imported this "github.com/bghgary/glTF-Tools-for-Unity" gITF package still "var colladaData = SomeColladaLibrary.Parse(www.text);" this line is not accepting. \$\endgroup\$ Oct 17 '18 at 5:12
  • \$\begingroup\$ and it's showing an error as "error CS0246: The type or namespace name `GLTF' could not be found. Are you missing an assembly reference?" \$\endgroup\$ Oct 17 '18 at 5:21
  • 1
    \$\begingroup\$ That's a different question. Ask that separately, showing the exact steps you took to include your chosen library in your project. Note that the example code above is just a rough pattern and should not be copied literally (since of course you won't find a real type called SomeColladaLibrary) \$\endgroup\$
    – DMGregory
    Oct 17 '18 at 8:09
  • \$\begingroup\$ Please don't literally write "SomeColladaLibrary" in your code. As I said above, that's a placeholder you should replace with an actual library that does this task. There's no real thing called "SomeColladaLibrary" \$\endgroup\$
    – DMGregory
    Oct 17 '18 at 9:11
  • 1
    \$\begingroup\$ No. I am not an expert with the library you've chosen, so I can't tell you how to use it. Read its documentation. \$\endgroup\$
    – DMGregory
    Oct 17 '18 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.