Advertisement
Suzukaze

UnityCustomMesh Script

Dec 21st, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. public class SkinnedMeshComponentWizard : ScriptableWizard
  8. {
  9. public SkinnedMeshRenderer baseMesh;
  10. public List<SkinnedMeshRenderer> components;
  11.  
  12. [MenuItem("Tools/Mesh/Generate Skinned Mesh Component")]
  13. protected static void CreateWizard()
  14. {
  15. DisplayWizard<SkinnedMeshComponentWizard>("Skinned Mesh Component Generation", "Generate");
  16. }
  17.  
  18. protected void OnWizardCreate()
  19. {
  20. foreach (SkinnedMeshRenderer component in components)
  21. {
  22. Dictionary<int, int> boneMapping = new Dictionary<int, int>();
  23. for (int i = 0; i < component.bones.Length; i++)
  24. {
  25. Transform compBone = component.bones[i];
  26. for (int j = 0; j < baseMesh.bones.Length; j++)
  27. {
  28. Transform baseBone = baseMesh.bones[j];
  29. if (compBone.name == baseBone.name)
  30. {
  31. boneMapping.Add(i, j);
  32. break;
  33. }
  34. }
  35. }
  36.  
  37. //remap the bones
  38. BoneWeight[] weights = component.sharedMesh.boneWeights;
  39. for (int i = 0; i < weights.Length; i++)
  40. {
  41. BoneWeight weight = weights[i];
  42. weights[i].boneIndex0 = boneMapping[weight.boneIndex0];
  43. weights[i].boneIndex1 = boneMapping[weight.boneIndex1];
  44. weights[i].boneIndex2 = boneMapping[weight.boneIndex2];
  45. weights[i].boneIndex3 = boneMapping[weight.boneIndex3];
  46. }
  47.  
  48. Vector3[] vertices = component.sharedMesh.vertices;
  49. Vector3 scale = component.transform.lossyScale;
  50. for (int i = 0; i < vertices.Length; i++)
  51. {
  52. Vector3 vertex = vertices[i];
  53. vertex.x *= scale.x;
  54. vertex.y *= scale.y;
  55. vertex.z *= scale.z;
  56. vertices[i] = vertex;
  57. }
  58.  
  59. Mesh mesh = new Mesh
  60. {
  61. name = component.sharedMesh.name,
  62. vertices = vertices,
  63. uv = component.sharedMesh.uv,
  64. triangles = component.sharedMesh.triangles,
  65. bindposes = baseMesh.sharedMesh.bindposes,
  66. boneWeights = weights,
  67. normals = component.sharedMesh.normals,
  68. colors = component.sharedMesh.colors,
  69. tangents = component.sharedMesh.tangents
  70. };
  71.  
  72. AssetDatabase.CreateAsset(mesh, Path.Combine("Assets", "EXPORT", $"{mesh.name}.asset"));
  73. }
  74. }
  75.  
  76. protected void OnWizardUpdate()
  77. {
  78. helpString = "Creates a new mesh based off the bone structure of a base mesh and ensures everything is in the correct order in the new mesh.";
  79. isValid = (baseMesh != null) && (components != null && components.Count > 0);
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement