Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Add mesh in runtime

Discussion in 'Getting Started' started by Haruji, Nov 6, 2017.

  1. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    I'm trying to add mesh from resource folder at runtime.
    But it won't show up in runtime.
    I did it with plain model.
    My model: cat.dae


    Code (CSharp):
    1.  private void loadCharacter(string mesh) {
    2.         Mesh _mesh = Resources.Load("Models/"+ mesh) as Mesh;
    3.      
    4.         Debug.Log(mesh);
    5.         Debug.Log(_mesh);// null  mesh
    6.         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
    7.         go.GetComponent<MeshFilter>().mesh = _mesh;
    8.  
    9.     }
    I don't know why.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,248
    i think from resource folder you can only instantiate unity meshes (that were there when you made build),
    from streaming assets folder you could instantiate meshes from asset bundles and you placed there later.

    to load external model formats, you need to make importer to read/parse them manually,
    like .obj reader https://www.assetstore.unity3d.com/en/#!/content/152 (theres free alternatives also)
    for .dae https://www.assetstore.unity3d.com/en/#!/content/19579 (not sure if theres free .dae importers)
     
  3. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    Since it can be added manually, i really don't want to use importer library.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,248
    oh i think i misread, i though you want to import .dae models at runtime from folder..

    which name you are passing in the 'string mesh', should not add file extension to it.
     
  5. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    its name likes this:
    Code (CSharp):
    1. string mesh = "neko";
     
  6. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    i tried it with the Ethan model too but it won't work too
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,248
    try loading as gameobject, then take meshfilter from that,
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         string name = "box";
    4.         var obj = Resources.Load(name) as GameObject;
    5.         // TODO: check if root has meshfilter..
    6.         var mesh = obj.GetComponent<MeshFilter>().sharedMesh;
    7.         Debug.Log(mesh);
    8.     }
    *attached sample model that has meshfilter in root
     

    Attached Files:

  8. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    I tried this but it won't work :(.
    Code (CSharp):
    1.  private void loadCharacter() {
    2.         string name = "box";
    3.         var obj = Resources.Load(name) as GameObject;
    4.         // TODO: check if root has meshfilter..
    5.         var mesh = obj.GetComponent<MeshFilter>().sharedMesh;
    6.         Debug.Log(mesh);
    7.         Instantiate(mesh);// i tried this :(
    8.     }
    Sorry, i am newbie.
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,815
    Instantiate is for making a clone of an existing GameObject. I've never seen it used on a Mesh (which is not a GameObject, nor a Component). I don't think that's likely to work.

    Can you explain what it is you're trying to do?

    If you just want to load a character from resources, you should be trying to load and clone a GameObject, not a Mesh.
     
  10. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    Yes , i want to load a character in run time from resource(3dmodel file: *.dae )
     
    Last edited: Nov 7, 2017
  11. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    My 3d model's in root resource folder. And i tried as u suggest but it won't work too.

    Code (CSharp):
    1. void loadModel() {
    2.         GameObject obj = Resources.Load("neko") as GameObject;
    3.                 Debug.Log(obj);
    4.         GameObject go = Instantiate(obj);
    5.      
    6.     }
    Drag and drop is easy. But i really don't like it.
     
  12. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,248
    does that print null?

    can you screenshot your resources folder in the project, with the model?
     
  13. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,815
    When you say something doesn't work, please explain how it doesn't work. Do you get an error message? Or does your code run, but doesn't do what you want?

    Please try this:

    Code (CSharp):
    1.  void loadModel() {
    2.          Object obj = Resources.Load("neko");
    3.          Debug.Log("Loaded from Resources: " + obj);
    4.          GameObject go = Instantiate(obj) as GameObject;
    5.          Debug.Log("Instantiated: " + go);
    6. }
    7.  
    ...then look in the Console and tell us what it says.

    (If it would be easier to write in Japanese, PM me and I will do my best. 日本語で書くの方が優しかったら、メッセージを送ってください。頑張ります。)
     
  15. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
  16. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,815
    Very strange. That should work!

    Are you certain the object in your Resources folder is called exactly "neko"? Could it be "neko " or " neko"?

    If it's not that, then perhaps the DAE importer is somehow not compatible with this process. You can work around this as follows:

    1. Move the neko object somewhere else in your project (not in the Resources folder).
    2. Now grab neko from your Project tab, and drag it to your Hierarchy tab. This adds it to the scene as a GameObject.
    3. Use the Inspector tab to reset its position to 0,0,0, and its rotation to 0,0,0 as well.
    4. Now grab that neko object in the Hierarchy, and drag it back to the Project tab, dropping it on the Resources folder. This creates a prefab.
    5. You can now delete the neko object from your Hierarchy, so that it is no longer in the scene.

    Now the above code should (Resources.Load followed by Instantiate) should certainly work. It is no longer accessing the DAE-imported asset directly, but instead just loading a GameObject prefab.
     
  17. Haruji

    Haruji

    Joined:
    Mar 5, 2017
    Posts:
    30
    Sorry, It did not work too.
     
  18. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,815
    I don't understand. I will PM you my email address, so you can send me the project and I can have a look myself. We'll figure it out!
     
  19. kirhgoff

    kirhgoff

    Joined:
    Jan 23, 2021
    Posts:
    1
    Such a cliffhanger, I wonder did it work and what was the case. I bet the folders located in the wrong places