Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ipsquiggle · Jan 10, 2011 at 07:31 PM · meshprefabmeshfiltergeneratedsharedmesh

Sharing a generated mesh between multiple game objects

So I have a component script, "GeneratedMesh", which I am attaching to a prefab. Basically, I would like every instance of a prefab to use the same generated mesh, but as I have it right now, the mesh is regenerated for every instance. So it looks something like this right now:

MyPrefabA has GeneratedMesh script

MyPrefabB has GeneratedMesh script

Instantiate MyPrefabA, MyPrefabA, MyPrefabB, MyPrefabB.

What happens:

GeneratedMesh generates 4 times, once for each instance.

What I would like to happen:

GeneratedMesh generates a mesh for MyPrefabA, then the second time it realizes MyPrefabA already has a mesh generated, and reuses it for the second instance. Then it generates a mesh for the first instance of MyPrefabB, then for the second MyPrefabB, it realizes there is already a mesh generated and uses that.

In the end, it only generates 2 times.


Leads I have so far:

I notice that MeshFilter has a 'sharedMesh' property. Presumably if I set the sharedMesh of the second instance to be the same as the first, Unity will consider them identical geometry and handle batching and all that.

So what I really need are suggestions for determining which prefab a mesh was generated for (really, it's all based on the parameters of the GeneratedMesh script on the prefab, as well as the material on the prefab), and then using that to coordinate sharing the mesh between instances...

Any thoughts?

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

2 Replies

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

Answer by DaveA · Jan 10, 2011 at 10:52 PM

Could you put the mesh generator on a different object, of which there is only one, so your script runs once. Then when you instantiate more objects, Find (or keep a reference to) that first object, then assign it's mesh out of there?

Comment
Add comment · Show 2 · 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 Ipsquiggle · Jan 11, 2011 at 12:33 AM 0
Share

Hmmm, yes, I should have thought of that. ;) The wheels are turning, I'll check back and let you know what I ended up with.

avatar image Ipsquiggle · Mar 14, 2011 at 06:40 PM 0
Share

This ended up being a working solution. I have a static '$$anonymous$$eshes' class that keeps a canonical reference to the generated mesh, and all the other objects which need the mesh reference it here.

avatar image
2

Answer by Jessy · Jan 10, 2011 at 11:42 PM

It's tough to tell, without code. But it's sounds like you're onto the solution. If you keep a reference to a mesh you created, then you can assign that to as many MeshFilter.mesh-es or MeshFilter.sharedMesh-es as you want, and the result will be the same: each MeshFilter will use the same (instanced one time only) mesh. However, if you ever get a mesh reference using a MeshFilter.mesh, then that mesh will be instanced.

Also, be aware that MeshFilter.mesh is only designed for runtime use; MeshFilter.sharedMesh is what you need to use in the Editor. This is a nice indicator of good practice: if your mesh generation scripting doesn't require multiple instances, run your code in the Editor. If you don't see errors, you're probably making nice, efficient code for runtime use.

Comment
Add comment · Show 3 · 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 Ipsquiggle · Jan 11, 2011 at 12:32 AM 0
Share

This sounds like exactly what I want. I'm capable of searching of course, but if you have a quick summary of what "Run your code in the Editor" means, I'd appreciate it. :)

avatar image Jessy · Jan 11, 2011 at 07:34 AM 0
Share

Sure: http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$enuItem.html http://unity3d.com/support/documentation/ScriptReference/Editor.html

I tend to run as little setup code as possible, in-game. I use a fair amount of procedural meshes, but I generally don't have to wait for runtime to create them. Of course this can lear to file size increase, but if the meshes are small, they're probably smaller than the code itself. Here's an example: http://forum.unity3d.com/threads/58536-reveal-a-texture?p=375739&viewfull=1#post375739

avatar image Jessy · Jan 11, 2011 at 07:36 AM 0
Share

"lead", not "lear". $#%^*&@ I wish you could just edit comments!!! :-P

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

No one has followed this question yet.

Related Questions

Generated meshes/materials cannot be made into prefabs? 1 Answer

Mesh memory leak error 0 Answers

difference between sharedMesh and Mesh 1 Answer

Mesh Filters randomly self deleted 0 Answers

Is there any way to reset the default meshes? 3 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