Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Alven4 · Aug 28, 2015 at 09:52 PM · meshdestroycreateram

Creating and Destroying mesh GameObjects quickly fill up RAM

I need to destroy and recreate mesh quickly but everytime I destroy and recreate mesh Unity allocates more RAM and won't let it go. Example of destroying: m_mesh = m_voxelChunk[2,0,2].RequestMesh(); Destroy(m_mesh);

Function request mesh: public GameObject RequestMesh(){ return m_mesh; }

Function recreating mesh: public void CreateMesh(Material mat) { //float startTime = Time.realtimeSinceStartup;

         Mesh mesh = MarchingCubes.CreateMesh(m_voxels,2,2);
         if(mesh == null) return;
         
         int size = mesh.vertices.Length;
         
         if(m_normals != null)
         {
             Vector3[] normals = new Vector3[size];
             Vector3[] verts = mesh.vertices;
             
             //Each verts in the mesh generated is its position in the voxel array
             //and you can use this to find what the normal at this position.
             //The verts are not at whole numbers though so you need to use trilinear interpolation
             //to find the normal for that position
             
             for(int i = 0; i < size; i++)
                 normals[i] = TriLinearInterpNormal(verts[i]);
             
             mesh.normals = normals;
         }
         else
         {
             mesh.RecalculateNormals();
         }
         
         Color[] control = new Color[size];
         Vector3[] meshNormals = mesh.normals;
             
         for(int i = 0; i < size; i++)
         {
             //This creates a control map used to texture the mesh based on the slope
             //of the vert. Its very basic and if you modify how this works yoou will
             //you will probably need to modify the shader as well.
             float dpUp = Vector3.Dot(meshNormals[i], Vector3.up);
             
             //Red channel is the sand on flat areas
             float R = (Mathf.Max(0.0f, dpUp) < 0.8f) ? 0.0f : 1.0f;
             //Green channel is the gravel on the sloped areas
             float G = Mathf.Pow(Mathf.Abs(dpUp), 2.0f);
             
             //Whats left end up being the rock face on the vertical areas
 
             control[i] = new Color(R,G,0,0);
         }
 
         //May as well store in colors 
         mesh.colors = control;
         
         m_mesh = new GameObject("Voxel Mesh " + m_pos.x.ToString() + " " + m_pos.y.ToString() + " " + m_pos.z.ToString());
         m_mesh.AddComponent<MeshFilter>();
         m_mesh.AddComponent<MeshRenderer>();
         m_mesh.GetComponent<Renderer>().material = mat;
         m_mesh.GetComponent<MeshFilter>().mesh = mesh;
         m_mesh.transform.localPosition = m_pos;
         
         MeshCollider collider = m_mesh.AddComponent<MeshCollider>();
         collider.sharedMesh = mesh;
         
         //Debug.Log("Create mesh time = " + (Time.realtimeSinceStartup-startTime).ToString() );
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by hexagonius · Aug 29, 2015 at 09:41 AM

instead of instantiating a whole new Gameobject every time, alter just the mesh data of the existing Mesh.

http://docs.unity3d.com/ScriptReference/Mesh.html

under 3. the page explains how to recreate a mesh by calling Clear and assigning new data. if done right you won't generate garbage after all meshes have been created once.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alven4 · Aug 29, 2015 at 11:15 AM 0
Share

Thanks, it no longer fills up RA$$anonymous$$.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Keep GameObjects After Re Run? 1 Answer

Merge two gameobjects and create a new one unity2D! 0 Answers

Create a mesh with a scriptableObject directly from the editor. 0 Answers

toggling ragdoll as new object 0 Answers

I want to make the DepthImage of Kinect.V2 Renderer Mesh Skinned. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges