MegaPrefabMaker.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. #if false
  6. // May want a window for this for options
  7. public class MegaPrefabMaker : MonoBehaviour
  8. {
  9. [MenuItem("GameObject/Prefab Maker")]
  10. static void PrefabMaker()
  11. {
  12. GameObject from = Selection.activeGameObject;
  13. if ( from != null )
  14. {
  15. if ( !Directory.Exists("Assets/MegaPrefabs") ) // Have a path option
  16. {
  17. AssetDatabase.CreateFolder("Assets", "MegaPrefabs");
  18. }
  19. GameObject prefab = PrefabUtility.CreatePrefab("Assets/MegaPrefabs/" + Selection.activeGameObject.name + ".prefab", from);
  20. // So we need to replace all the meshes, and then have modifier methods for duplicating data
  21. // Check prefab folder exists, if not make it
  22. // Create the prefab object from the from object
  23. // clone any components on the object
  24. // run through every child and clone what we find
  25. }
  26. }
  27. void AddChildren(GameObject child, GameObject parent)
  28. {
  29. if ( child )
  30. {
  31. // Clone the object to a prefab, parent is the prefab parent
  32. GameObject prefab = null;
  33. for ( int i = 0; i < child.transform.childCount; i++ )
  34. {
  35. // Clone the object to a prefab
  36. AddChildren(child.transform.GetChild(i).gameObject, prefab);
  37. }
  38. }
  39. }
  40. static public GameObject CloneMeshes(GameObject from, GameObject to)
  41. {
  42. //GameObject clone = null;
  43. if ( from && to )
  44. {
  45. //clone = (GameObject)GameObject.Instantiate(subject);
  46. SkinnedMeshRenderer[] fromskinmeshes = from.GetComponentsInChildren<SkinnedMeshRenderer>();
  47. SkinnedMeshRenderer[] toskinmeshes = to.GetComponentsInChildren<SkinnedMeshRenderer>();
  48. int l = fromskinmeshes.Length;
  49. for ( int i = 0; i < l; i++ )
  50. {
  51. Mesh mesh = fromskinmeshes[i].sharedMesh;
  52. Mesh clonemesh = new Mesh();
  53. clonemesh.vertices = mesh.vertices;
  54. clonemesh.uv1 = mesh.uv1;
  55. clonemesh.uv2 = mesh.uv2;
  56. clonemesh.uv = mesh.uv;
  57. clonemesh.normals = mesh.normals;
  58. clonemesh.tangents = mesh.tangents;
  59. clonemesh.colors = mesh.colors;
  60. clonemesh.subMeshCount = mesh.subMeshCount;
  61. for ( int s = 0; s < mesh.subMeshCount; s++ )
  62. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  63. clonemesh.boneWeights = mesh.boneWeights;
  64. clonemesh.bindposes = mesh.bindposes;
  65. clonemesh.name = mesh.name + "_copy";
  66. clonemesh.RecalculateBounds();
  67. toskinmeshes[i].sharedMesh = clonemesh;
  68. AssetDatabase.AddObjectToAsset(clonemesh, to);
  69. }
  70. MeshFilter[] mfs = from.GetComponentsInChildren<MeshFilter>();
  71. MeshFilter[] clonemfs = to.GetComponentsInChildren<MeshFilter>();
  72. MeshCollider[] mcs = from.GetComponentsInChildren<MeshCollider>();
  73. MeshCollider[] clonemcs = to.GetComponentsInChildren<MeshCollider>();
  74. for ( int i = 0; i < mfs.Length; i++ )
  75. {
  76. MeshFilter mf = mfs[i];
  77. MeshFilter clonemf = clonemfs[i];
  78. Mesh mesh = mf.sharedMesh;
  79. Mesh clonemesh = new Mesh();
  80. clonemesh.vertices = mesh.vertices;
  81. clonemesh.uv1 = mesh.uv1;
  82. clonemesh.uv2 = mesh.uv2;
  83. clonemesh.uv = mesh.uv;
  84. clonemesh.normals = mesh.normals;
  85. clonemesh.tangents = mesh.tangents;
  86. clonemesh.colors = mesh.colors;
  87. clonemesh.subMeshCount = mesh.subMeshCount;
  88. for ( int s = 0; s < mesh.subMeshCount; s++ )
  89. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  90. clonemesh.boneWeights = mesh.boneWeights;
  91. clonemesh.bindposes = mesh.bindposes;
  92. clonemesh.name = mesh.name + "_copy";
  93. clonemesh.RecalculateBounds();
  94. clonemf.sharedMesh = clonemesh;
  95. AssetDatabase.AddObjectToAsset(clonemesh, to);
  96. for ( int j = 0; j < mcs.Length; j++ )
  97. {
  98. MeshCollider mc = mcs[j];
  99. if ( mc.sharedMesh = mesh )
  100. clonemcs[j].sharedMesh = clonemesh;
  101. }
  102. }
  103. MegaModifyObject[] modobjs = to.GetComponentsInChildren<MegaModifyObject>();
  104. for ( int i = 0; i < modobjs.Length; i++ )
  105. {
  106. modobjs[i].MeshUpdated();
  107. }
  108. }
  109. }
  110. }
  111. #endif