// Save Mesh resources public static void DuplicateResources(Uni2DSprite a_rSprite, bool a_bPrefab = false) { // Sprite mesh Mesh rSpriteMesh = a_rSprite.SpriteData.renderMesh; if (rSpriteMesh != null) { Mesh rDuplicatedMesh = Uni2DEditorResourceCopyUtils.DuplicateMesh(rSpriteMesh); MeshFilter rMeshFilterComponent = a_rSprite.GetComponent <MeshFilter>( ); SkinnedMeshRenderer rSkinnedMeshRendererComponent = a_rSprite.GetComponent <SkinnedMeshRenderer>( ); // Apply the new generated mesh to the mesh filter component // and save it as generated data if (rMeshFilterComponent != null) { rMeshFilterComponent.sharedMesh = rDuplicatedMesh; a_rSprite.SpriteData.renderMesh = rDuplicatedMesh; } if (rSkinnedMeshRendererComponent != null) { rSkinnedMeshRendererComponent.sharedMesh = rDuplicatedMesh; } } // Duplicate the generated material if (a_rSprite.SpriteData.generatedMaterial != null) { // Duplicate generated mesh a_rSprite.SpriteData.generatedMaterial = new Material(a_rSprite.SpriteData.generatedMaterial); // Change the render mesh material ref to the duplicate a_rSprite.SpriteData.renderMeshMaterial = a_rSprite.SpriteData.generatedMaterial; // Update the renderer material a_rSprite.GetComponent <Renderer>().sharedMaterial = a_rSprite.SpriteData.generatedMaterial; } // Mesh collider(s) List <Mesh> rMeshCollidersList = a_rSprite.SpriteData.meshCollidersList; List <MeshCollider> rMeshColliderComponentsList = a_rSprite.SpriteData.meshColliderComponentsList; for (int iMeshIndex = 0, iMeshCount = rMeshCollidersList.Count; iMeshIndex < iMeshCount; ++iMeshIndex) { Mesh rDuplicatedMeshCollider = Uni2DEditorResourceCopyUtils.DuplicateMesh(rMeshCollidersList[iMeshIndex]); MeshCollider rMeshCollider = rMeshColliderComponentsList[iMeshIndex]; if (rMeshCollider != null) { rMeshCollider.sharedMesh = rDuplicatedMeshCollider; } rMeshCollidersList[iMeshIndex] = rDuplicatedMeshCollider; } #if UNITY_EDITOR EditorUtility.SetDirty(a_rSprite); #endif }
// Ensure Mesh Validity private bool EnsureMeshValidity(Mesh a_rSourceMesh, Mesh a_rUsedMesh, ref Mesh a_rPoseMesh, ref ReferenceContainer a_rRefManipulatedMesh) { if (a_rSourceMesh != null) { Object rManipulatedMesh = a_rRefManipulatedMesh.Reference; if (a_rPoseMesh == null || a_rPoseMesh != a_rSourceMesh || rManipulatedMesh == null || rManipulatedMesh != a_rUsedMesh) { a_rPoseMesh = a_rSourceMesh; if (rManipulatedMesh != null) { DestroyImmediate(rManipulatedMesh); } a_rRefManipulatedMesh.Reference = Uni2DEditorResourceCopyUtils.DuplicateMesh(a_rPoseMesh); } return(true); } else { return(false); } }