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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Generating meshes during run-time: how to call the code to properly save the mesh for viewing

Discussion in 'General Graphics' started by JGattes, Nov 18, 2015.

  1. JGattes

    JGattes

    Joined:
    Nov 18, 2015
    Posts:
    10
    Hi all, that's my first time playing around with mesh generation during runtime. I want to generate a IcoSphere, I have a very basic question it actually.

    I found the code to implement that here: http://wiki.unity3d.com/index.php/ProceduralPrimitives

    I understand the geometry stuff going on in the code. But I don't know how to exactly "call' the code within my game. It is, the code creates a class. Fine. But how can I use such class in my game code to generate the mesh in a way I can see it, chose its world location, etc. In other words, how can I use that code to properly have my icosphere showing on the screen?

    Many thanks!
     
  2. JGattes

    JGattes

    Joined:
    Nov 18, 2015
    Posts:
    10
    I just notice that I double-posted the question without wanting to. But the forum system does not allow me to fully delete the thread. So, for the sake of organization, please consider only THIS one, not the other. And sorry for the unintentional double-posting.
     
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    The example's code is a mess. It looks like code from a monobehaviour converted badly into a static class.
    Some things to maybe make this work:

    Add an argument passing GameObject gameObject.
    Change Mesh mesh = filter.mesh at line 2-3 to Mesh mesh = new Mesh ();
    Add filter.mesh = mesh; to the end.
    Probably add something like this: MeshRenderer renderer = gameObject.AddComponent<Renderer>(); renderer.material = materialhere;

    Then you should be able to call it from a script attached to a gameObject within a function like;
    void Start () { IcoSphere.Create(gameObject); }
     
    JGattes likes this.
  4. JGattes

    JGattes

    Joined:
    Nov 18, 2015
    Posts:
    10
    Thanks for your reply! By coincidence, that was exactly what I did even before reading your reply, with one or two tinny differences. Your solution is better than mine. Thanks again