MegaCopyObjects.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Reflection;
  4. using System.IO;
  5. #if !UNITY_FLASH && !UNITY_METRO && !UNITY_WP8
  6. public class MegaCopyObjects : MonoBehaviour
  7. {
  8. [MenuItem("GameObject/Mega Instance Object")]
  9. static void InstanceModifiedMesh()
  10. {
  11. GameObject from = Selection.activeGameObject;
  12. MegaCopyObject.InstanceObject(from);
  13. }
  14. //[MenuItem("GameObject/Mega Copy Object")]
  15. static void DoCopyObjects()
  16. {
  17. GameObject from = Selection.activeGameObject;
  18. MegaCopyObject.DoCopyObjects(from);
  19. }
  20. //[MenuItem("GameObject/Mega Copy Hier")]
  21. static void DoCopyObjectsHier()
  22. {
  23. GameObject from = Selection.activeGameObject;
  24. MegaCopyObject.DoCopyObjectsChildren(from);
  25. }
  26. static Mesh CloneMesh(Mesh mesh)
  27. {
  28. Mesh clonemesh = new Mesh();
  29. clonemesh.vertices = mesh.vertices;
  30. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  31. clonemesh.uv2 = mesh.uv2;
  32. clonemesh.uv3 = mesh.uv3;
  33. clonemesh.uv4 = mesh.uv4;
  34. #else
  35. clonemesh.uv1 = mesh.uv1;
  36. clonemesh.uv2 = mesh.uv2;
  37. #endif
  38. clonemesh.uv = mesh.uv;
  39. clonemesh.normals = mesh.normals;
  40. clonemesh.tangents = mesh.tangents;
  41. clonemesh.colors = mesh.colors;
  42. clonemesh.subMeshCount = mesh.subMeshCount;
  43. for ( int s = 0; s < mesh.subMeshCount; s++ )
  44. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  45. clonemesh.boneWeights = mesh.boneWeights;
  46. clonemesh.bindposes = mesh.bindposes;
  47. clonemesh.name = mesh.name + "_copy";
  48. clonemesh.RecalculateBounds();
  49. return clonemesh;
  50. }
  51. [MenuItem("GameObject/Old Create Mega Prefab (Deprec)")]
  52. static void AllNewCreateSimplePrefab()
  53. {
  54. if ( Selection.activeGameObject != null )
  55. {
  56. if ( !Directory.Exists("Assets/MegaPrefabs") )
  57. AssetDatabase.CreateFolder("Assets", "MegaPrefabs");
  58. GameObject obj = Selection.activeGameObject;
  59. // Make a copy?
  60. GameObject newobj = MegaCopyObject.DoCopyObjects(obj);
  61. newobj.name = obj.name;
  62. // Get all modifyObjects in children
  63. MegaModifyObject[] mods = newobj.GetComponentsInChildren<MegaModifyObject>();
  64. for ( int i = 0; i < mods.Length; i++ )
  65. {
  66. // Need method to get the base mesh
  67. GameObject pobj = mods[i].gameObject;
  68. // Get the mesh and make an asset for it
  69. Mesh mesh = MegaUtils.GetSharedMesh(pobj);
  70. if ( mesh )
  71. {
  72. string mname = mesh.name;
  73. int ix = mname.IndexOf("Instance");
  74. if ( ix != -1 )
  75. mname = mname.Remove(ix);
  76. string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab";
  77. AssetDatabase.CreateAsset(mesh, meshpath);
  78. AssetDatabase.SaveAssets();
  79. AssetDatabase.Refresh();
  80. }
  81. }
  82. MegaWrap[] wraps = newobj.GetComponentsInChildren<MegaWrap>();
  83. for ( int i = 0; i < wraps.Length; i++ )
  84. {
  85. // Need method to get the base mesh
  86. GameObject pobj = wraps[i].gameObject;
  87. // Get the mesh and make an asset for it
  88. Mesh mesh = MegaUtils.GetSharedMesh(pobj);
  89. if ( mesh )
  90. {
  91. string mname = mesh.name;
  92. int ix = mname.IndexOf("Instance");
  93. if ( ix != -1 )
  94. mname = mname.Remove(ix);
  95. string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab";
  96. AssetDatabase.CreateAsset(mesh, meshpath);
  97. AssetDatabase.SaveAssets();
  98. AssetDatabase.Refresh();
  99. }
  100. }
  101. #if UNITY_2019 || UNITY_2018_3 || UNITY_2020
  102. Object prefab = PrefabUtility.SaveAsPrefabAsset(newobj, "Assets/MegaPrefabs/" + newobj.name + "_Prefab.prefab");
  103. DestroyImmediate(newobj);
  104. #else
  105. Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/MegaPrefabs/" + newobj.name + "_Prefab.prefab");
  106. //EditorUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab);
  107. PrefabUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab);
  108. DestroyImmediate(newobj);
  109. #endif
  110. }
  111. }
  112. [MenuItem("GameObject/Mega Duplicate Object (old)")]
  113. static void DupObject()
  114. {
  115. GameObject from = Selection.activeGameObject;
  116. MegaCopyObject.DuplicateObject(from);
  117. }
  118. [MenuItem("GameObject/Mega Duplicate Object New")]
  119. static void DupObjectNew()
  120. {
  121. SceneView.lastActiveSceneView.Focus();
  122. if ( Selection.activeGameObject )
  123. {
  124. MegaModifyObject[] oldmods = (MegaModifyObject[])Selection.activeGameObject.GetComponentsInChildren<MegaModifyObject>();
  125. bool[] enabled = new bool[oldmods.Length];
  126. for ( int i = 0; i < oldmods.Length; i++ )
  127. {
  128. enabled[i] = oldmods[i].enabled;
  129. oldmods[i].enabled = false;
  130. oldmods[i].ResetMeshInfo();
  131. }
  132. EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent("Duplicate"));
  133. GameObject go = Selection.activeGameObject;
  134. MegaModifyObject[] mods = (MegaModifyObject[])go.GetComponentsInChildren<MegaModifyObject>();
  135. for ( int i = 0; i < mods.Length; i++ )
  136. {
  137. mods[i].enabled = enabled[i];
  138. mods[i].MeshUpdated();
  139. oldmods[i].enabled = enabled[i];
  140. }
  141. }
  142. }
  143. [MenuItem("GameObject/Create Mega Prefab")]
  144. static void CreatePrefab()
  145. {
  146. if ( Selection.activeGameObject != null )
  147. {
  148. if ( !Directory.Exists("Assets/MegaPrefabs") )
  149. AssetDatabase.CreateFolder("Assets", "MegaPrefabs");
  150. GameObject obj = Selection.activeGameObject;
  151. // Make a copy?
  152. GameObject newobj = MegaCopyObject.DuplicateObjectForPrefab(obj);
  153. newobj.name = obj.name;
  154. // Get all modifyObjects in children
  155. MegaModifyObject[] mods = newobj.GetComponentsInChildren<MegaModifyObject>();
  156. int id = 0;
  157. for ( int i = 0; i < mods.Length; i++ )
  158. {
  159. // Need method to get the base mesh
  160. GameObject pobj = mods[i].gameObject;
  161. // Get the mesh and make an asset for it
  162. //Mesh mesh = MegaUtils.GetSharedMesh(pobj);
  163. GameObject inobj = null;
  164. Mesh mesh = MegaModifyObject.FindMesh(pobj, out inobj);
  165. if ( mesh )
  166. {
  167. //mods[i].ResetMeshInfo();
  168. //mesh.vertices = mods[i].verts; //_verts; // mesh.vertices = GetVerts(true);
  169. //mesh.uv = mods[i].uvs; //GetUVs(true);
  170. //mods[i].sverts = mods[i].verts;
  171. //if ( mods[i].recalcnorms )
  172. //mods[i].RecalcNormals();
  173. //if ( mods[i].recalcbounds )
  174. //mesh.RecalculateBounds();
  175. if ( !AssetDatabase.Contains(mesh) )
  176. {
  177. string mname = mesh.name;
  178. int ix = mname.IndexOf("Instance");
  179. if ( ix != -1 )
  180. mname = mname.Remove(ix);
  181. string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab";
  182. id++;
  183. AssetDatabase.CreateAsset(mesh, meshpath);
  184. AssetDatabase.SaveAssets();
  185. AssetDatabase.Refresh();
  186. }
  187. }
  188. }
  189. MegaWrap[] wraps = newobj.GetComponentsInChildren<MegaWrap>();
  190. for ( int i = 0; i < wraps.Length; i++ )
  191. {
  192. // Need method to get the base mesh
  193. GameObject pobj = wraps[i].gameObject;
  194. // Get the mesh and make an asset for it
  195. //Mesh mesh = MegaUtils.GetSharedMesh(pobj);
  196. GameObject inobj = null;
  197. Mesh mesh = MegaModifyObject.FindMesh(pobj, out inobj);
  198. if ( mesh )
  199. {
  200. if ( !AssetDatabase.Contains(mesh) )
  201. {
  202. string mname = mesh.name;
  203. int ix = mname.IndexOf("Instance");
  204. if ( ix != -1 )
  205. mname = mname.Remove(ix);
  206. string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab";
  207. id++;
  208. AssetDatabase.CreateAsset(mesh, meshpath);
  209. AssetDatabase.SaveAssets();
  210. AssetDatabase.Refresh();
  211. }
  212. }
  213. }
  214. #if UNITY_2019 || UNITY_2018_3 || UNITY_2020
  215. Object prefab = PrefabUtility.SaveAsPrefabAsset(newobj, "Assets/MegaPrefabs/" + newobj.name + "_Prefab.prefab");
  216. DestroyImmediate(newobj);
  217. #else
  218. Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/MegaPrefabs/" + newobj.name + "_Prefab.prefab");
  219. //EditorUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab);
  220. PrefabUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab);
  221. DestroyImmediate(newobj, true);
  222. #endif
  223. }
  224. }
  225. }
  226. #endif