• 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 /
avatar image
0
Question by Jeston · Apr 12, 2011 at 04:31 PM · meshinstance

All meshes are instances?

I have an issue where I wrote a small editor script that rebuilt my scene graph and is listed as below:

public void RecreateTransform( Transform t ) { GameObject g = t.gameObject; string name = g.name; Transform parent = g.transform.parent; Vector3 localPos = g.transform.localPosition; Quaternion localRot = g.transform.localRotation; Vector3 localScale = g.transform.localScale; bool bRecreate = false;

 MeshFilter filter = null;
 MeshRenderer renderer = null;
 PlanePart oldPart = null;
 Mesh mesh = null;
 Material[] mats = null;
 bool hasWireFrame = false;






 Component[] components = g.GetComponents<Component>();

for (int j = 0; j < components.Length; j++) { if (components[j] == null) { bRecreate = true;
}
else if( components[j] is MeshFilter ) { filter = (MeshFilter)components[j]; mesh = filter.mesh; } else if( components[j] is PlanePart ) { oldPart = (PlanePart)components[j]; } else if( components[j] is MeshRenderer ) { renderer = (MeshRenderer)components[j]; mats = renderer.materials; } else if( components[j] is WireFrameLineRenderer ) { hasWireFrame = true; }

}

 if( bRecreate || !hasWireFrame)
 {           
     List&lt;Transform&gt; oldChildren = new List&lt;Transform&gt;();
     for( int i = 0; i&lt;t.childCount; i++ )
     {
         oldChildren.Add( t.GetChild(i) );
     }

     GameObject recreated = new GameObject();
     recreated.name = name;
     recreated.transform.parent = parent;
     recreated.transform.localPosition = localPos;
     recreated.transform.localRotation = localRot;
     recreated.transform.localScale = localScale;            


     foreach( Transform oldchild in oldChildren )
     {
         oldchild.parent = recreated.transform;  
         RecreateTransform( oldchild );
     }



     if( oldPart != null )
     {
         PlanePart newPart = recreated.AddComponent&lt;PlanePart&gt;();
         newPart.IconTexture = oldPart.IconTexture;
         newPart.Category = oldPart.Category;
         newPart.PartName = oldPart.PartName;
         newPart.ID = oldPart.ID;
     }

     if( mesh != null )
     {
         MeshFilter newFilter = recreated.AddComponent&lt;MeshFilter&gt;();    
         newFilter.mesh = mesh;

         MeshRenderer newRenderer = recreated.AddComponent&lt;MeshRenderer&gt;();
         newRenderer.materials = mats;

         WireFrameLineRenderer lineRender = recreated.AddComponent&lt;WireFrameLineRenderer&gt;();
         lineRender.Fidelity = 2;
         lineRender.LineColor = new Color( 0, 204f/255f, 1, 1);                          
     }

     GameObject.DestroyImmediate(g);
 }           

}

public void OnGUI() {

     if (GUI.Button(new Rect(0.3f, 60.0f, 100.0f, 20.0f), "Rebuild"))
     {
        Undo.RegisterSceneUndo("RECREATE");
        GameObject[] go = Selection.gameObjects;
        for( int i = 0; i<go.Length;i++)
        {
            RecreateTransform( go[i].transform );
        }
     }
}

After this processed my scene graph all the meshes were turned to 'Instances' rather than mantaining their original references to 20 meshes, I now have 1000+ meshes being loaded which is blowing out the ram usage on the iPhone....

Does anyone have a suggestion on pointing these meshes back to their asset reference? I would hate to have to go through 900+ scene objects just to re-wire 20 mesh instances....

Should I do sharedMesh on the renderer instead of mesh ?

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 DaveA · Apr 12, 2011 at 04:44 PM

Yes that's the first thing I'd try, sharedMesh

Comment
Add comment · Show 4 · 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 Jeston · Apr 12, 2011 at 05:06 PM 0
Share

Yeah I did, but all the references are still instances, it would have been good if I hadn't saved my scene, and unfortunately I didn't publish to the iphone in a while and so I have done too much work to roll back lol. But I just hand wired some objects and doing the shared$$anonymous$$esh did not create instances. I am tempted to use the name to find the sub mesh asset and use that but I gotta dig through sdk docs to find out how to find a sub mesh by name

avatar image Jeston · Apr 12, 2011 at 05:07 PM 0
Share

Considering I only have about 10 root models I might create 10 models on the scene and when I go to assign the mesh, loop through all the children looking for meshes with the same local bounds or vert counts or use some metrics of finding the shared mesh

avatar image Bunny83 · Apr 12, 2011 at 06:05 PM 0
Share

Editor scripts should always work on shared$$anonymous$$esh, otherwise the mesh gets duplicated and leeks memory (You should get a warning). I don't get it. Every mesh is an instance what else do you expect? The $$anonymous$$esh that get's imported from a fbx is generated at import time. This $$anonymous$$esh get saved into the model prefab. At the moment the prefab is loaded there is an instance of this mesh. You can't have a reference to the data stored in the .prefab asset. The $$anonymous$$esh gets loaded when you load the prefab.

avatar image Jeston · Apr 12, 2011 at 06:39 PM 0
Share

Aye that makes sense, since it wasn't at run time I expected the refereches in the mesh[] to be pointers to the mesh asset and not instantiated copies.

However, I found a usefull way of remapping them with a little more scripting. I recreated fresh instances of the models and scanned an ArtReference node for the original shared$$anonymous$$esh's since the instantiated ones will have the same vertex data, so where I set the mesh, I just look for its shared$$anonymous$$esh now,

Thanks for the clarification, ill have to pay attention with the shared vrs. self copies Same with the materials

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Does using the same Mesh variable for multiple Mesh Filters take up more memory? 1 Answer

Prefabs / meshes / instances / performance / size 0 Answers

Alter Meshes in Editor by Script without warning 2 Answers

How to setup collision with freshly generated mesh and prefabs 1 Answer

change pivot after Mesh.CombineMeshes? 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