freezy / VisualPinball.Engine

:video_game: Visual Pinball Engine for Unity
https://docs.visualpinball.org
GNU General Public License v3.0
396 stars 62 forks source link

Fix mesh duplication #442

Closed freezy closed 1 year ago

freezy commented 1 year ago

This PR addresses two annoyances:

  1. Mesh duplication in the editor: When you duplicate an object with a procedurally generated mesh such as rubbers, Unity keeps the reference to the mesh, meaning both items share the same mesh, which is clearly not desired. A workaround was to set the mesh object of the duplicate to null, then regenerate it, which wasn't ideal. This PR serialized the GameObject's instance ID, and compares it on Awake() against the "real" instance ID from GetInstanceID(). If the object was duplicated, it's different, and we clear and re-generate the mesh to break the reference. However there is a drawback: Instance IDs don't persist across scenes, which results in all meshes being regenerated when the scene is loaded.
  2. For some reason, prefabs store the last mesh along with the current mesh. So all items that store their mesh in the scene would have their mesh in the scene twice. See also Unity Forum. This PR unpacks the prefabs for items that have a procedurally generated mesh. TBH I don't see any drawback keeping those as GameObjects instead of Prefabs in the scene.