MegaCopyObject.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. using UnityEngine;
  2. using System;
  3. using System.Reflection;
  4. using System.Collections.Generic;
  5. #if !UNITY_FLASH //&& !UNITY_METRO && !UNITY_WP8
  6. public class MegaCopyObject
  7. {
  8. static GameObject CopyMesh(GameObject subject)
  9. {
  10. GameObject clone = (GameObject)GameObject.Instantiate(subject);
  11. MeshFilter[] mfs = subject.GetComponentsInChildren<MeshFilter>();
  12. MeshFilter[] clonemfs = clone.GetComponentsInChildren<MeshFilter>();
  13. MeshCollider[] mcs = clone.GetComponentsInChildren<MeshCollider>();
  14. MeshCollider[] clonemcs = clone.GetComponentsInChildren<MeshCollider>();
  15. int l = mfs.Length;
  16. for ( int i = 0; i < l; i++ )
  17. {
  18. MeshFilter mf = mfs[i];
  19. MeshFilter clonemf = clonemfs[i];
  20. Mesh mesh = mf.sharedMesh;
  21. Mesh clonemesh = new Mesh();
  22. clonemesh.vertices = mesh.vertices;
  23. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  24. clonemesh.uv2 = mesh.uv2;
  25. clonemesh.uv3 = mesh.uv3;
  26. clonemesh.uv4 = mesh.uv4;
  27. #else
  28. clonemesh.uv1 = mesh.uv1;
  29. clonemesh.uv2 = mesh.uv2;
  30. #endif
  31. clonemesh.uv = mesh.uv;
  32. clonemesh.normals = mesh.normals;
  33. clonemesh.tangents = mesh.tangents;
  34. clonemesh.colors = mesh.colors;
  35. clonemesh.subMeshCount = mesh.subMeshCount;
  36. for ( int s = 0; s < mesh.subMeshCount; s++ )
  37. {
  38. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  39. }
  40. //clonemesh.triangles = mesh.triangles;
  41. #if UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  42. CopyBlendShapes(mesh, clonemesh);
  43. #if false
  44. int bcount = mesh.blendShapeCount; //GetBlendShapeFrameCount();
  45. Vector3[] deltaverts = new Vector3[mesh.vertexCount];
  46. Vector3[] deltanorms = new Vector3[mesh.vertexCount];
  47. Vector3[] deltatans = new Vector3[mesh.vertexCount];
  48. for ( int j = 0; j < bcount; j++ )
  49. {
  50. int frames = mesh.GetBlendShapeFrameCount(j);
  51. string bname = mesh.GetBlendShapeName(j);
  52. for ( int f = 0; f < frames; f++ )
  53. {
  54. mesh.GetBlendShapeFrameVertices(j, f, deltaverts, deltanorms, deltatans);
  55. float weight = mesh.GetBlendShapeFrameWeight(j, f);
  56. clonemesh.AddBlendShapeFrame(bname, weight, deltaverts, deltanorms, deltatans);
  57. }
  58. }
  59. #endif
  60. #endif
  61. clonemesh.boneWeights = mesh.boneWeights;
  62. clonemesh.bindposes = mesh.bindposes;
  63. clonemesh.name = mesh.name + "_copy";
  64. clonemesh.RecalculateBounds();
  65. clonemf.sharedMesh = clonemesh;
  66. for ( int j = 0; j < mcs.Length; j++ )
  67. {
  68. MeshCollider mc = mcs[j];
  69. if ( mc.sharedMesh == mesh )
  70. clonemcs[j].sharedMesh = clonemesh;
  71. }
  72. }
  73. return clone;
  74. }
  75. static void CopyBlendShapes(Mesh mesh, Mesh clonemesh)
  76. {
  77. #if UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  78. int bcount = mesh.blendShapeCount; //GetBlendShapeFrameCount();
  79. Vector3[] deltaverts = new Vector3[mesh.vertexCount];
  80. Vector3[] deltanorms = new Vector3[mesh.vertexCount];
  81. Vector3[] deltatans = new Vector3[mesh.vertexCount];
  82. for ( int j = 0; j < bcount; j++ )
  83. {
  84. int frames = mesh.GetBlendShapeFrameCount(j);
  85. string bname = mesh.GetBlendShapeName(j);
  86. for ( int f = 0; f < frames; f++ )
  87. {
  88. mesh.GetBlendShapeFrameVertices(j, f, deltaverts, deltanorms, deltatans);
  89. float weight = mesh.GetBlendShapeFrameWeight(j, f);
  90. clonemesh.AddBlendShapeFrame(bname, weight, deltaverts, deltanorms, deltatans);
  91. }
  92. }
  93. #endif
  94. }
  95. static GameObject CopyMesh(GameObject subject, MegaModifyObject mod)
  96. {
  97. GameObject clone = new GameObject(); //(GameObject)GameObject.Instantiate(subject);
  98. MeshFilter newmf = clone.AddComponent<MeshFilter>();
  99. SkinnedMeshRenderer oldsmr = subject.GetComponent<SkinnedMeshRenderer>();
  100. SkinnedMeshRenderer newsmr = null;
  101. if ( oldsmr )
  102. {
  103. newsmr = clone.AddComponent<SkinnedMeshRenderer>();
  104. newsmr.sharedMaterials = oldsmr.sharedMaterials;
  105. }
  106. else
  107. {
  108. MeshRenderer oldmr = subject.GetComponent<MeshRenderer>();
  109. MeshRenderer newmr = clone.AddComponent<MeshRenderer>();
  110. newmr.sharedMaterials = oldmr.sharedMaterials;
  111. }
  112. MeshFilter[] mfs = subject.GetComponentsInChildren<MeshFilter>();
  113. MeshCollider[] mcs = clone.GetComponentsInChildren<MeshCollider>();
  114. MeshCollider[] clonemcs = clone.GetComponentsInChildren<MeshCollider>();
  115. int l = mfs.Length;
  116. for ( int i = 0; i < l; i++ )
  117. {
  118. MeshFilter mf = mfs[i];
  119. Mesh mesh = mf.sharedMesh;
  120. Mesh clonemesh = new Mesh();
  121. clonemesh.vertices = mod.verts; //mesh.vertices;
  122. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  123. clonemesh.uv2 = mesh.uv2;
  124. clonemesh.uv3 = mesh.uv3;
  125. clonemesh.uv4 = mesh.uv4;
  126. #else
  127. clonemesh.uv1 = mesh.uv1;
  128. clonemesh.uv2 = mesh.uv2;
  129. #endif
  130. clonemesh.uv = mod.uvs; //mesh.uv;
  131. if ( mod.NormalMethod == MegaNormalMethod.Mega && mod.norms != null && mod.norms.Length > 0 )
  132. clonemesh.normals = mod.norms; //mesh.normals;
  133. else
  134. clonemesh.normals = mesh.normals;
  135. clonemesh.tangents = mesh.tangents;
  136. clonemesh.colors = mesh.colors;
  137. clonemesh.subMeshCount = mesh.subMeshCount;
  138. for ( int s = 0; s < mesh.subMeshCount; s++ )
  139. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  140. CopyBlendShapes(mesh, clonemesh);
  141. clonemesh.boneWeights = mesh.boneWeights;
  142. clonemesh.bindposes = mesh.bindposes;
  143. clonemesh.name = mesh.name + "_copy";
  144. clonemesh.RecalculateBounds();
  145. newmf.sharedMesh = clonemesh;
  146. for ( int j = 0; j < mcs.Length; j++ )
  147. {
  148. MeshCollider mc = mcs[j];
  149. if ( mc.sharedMesh == mesh )
  150. clonemcs[j].sharedMesh = clonemesh;
  151. }
  152. if ( newsmr && oldsmr )
  153. {
  154. newsmr.sharedMesh = clonemesh;
  155. #if UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  156. for ( int b = 0; b < mesh.blendShapeCount; b++ )
  157. {
  158. newsmr.SetBlendShapeWeight(b, oldsmr.GetBlendShapeWeight(b));
  159. }
  160. #endif
  161. }
  162. }
  163. return clone;
  164. }
  165. static void CopyModObj(MegaModifyObject from, MegaModifyObject to)
  166. {
  167. if ( from && to )
  168. {
  169. to.Enabled = from.Enabled;
  170. to.recalcbounds = from.recalcbounds;
  171. to.recalcCollider = from.recalcCollider;
  172. to.recalcnorms = from.recalcnorms;
  173. //to.DoLateUpdate = from.DoLateUpdate;
  174. to.UpdateMode = from.UpdateMode;
  175. //to.GrabVerts = from.GrabVerts;
  176. to.dynamicMesh = from.dynamicMesh;
  177. to.DrawGizmos = from.DrawGizmos;
  178. to.NormalMethod = from.NormalMethod;
  179. }
  180. }
  181. static public GameObject DoCopyObjects(GameObject from)
  182. {
  183. MegaModifyObject fromMod = from.GetComponent<MegaModifyObject>();
  184. GameObject to;
  185. if ( fromMod )
  186. to = CopyMesh(from, fromMod);
  187. else
  188. to = CopyMesh(from);
  189. MegaModifyObject mo = to.AddComponent<MegaModifyObject>();
  190. CopyModObj(fromMod, mo);
  191. MegaModifier[] mods = from.GetComponents<MegaModifier>();
  192. for ( int i = 0; i < mods.Length; i++ )
  193. {
  194. Component com = CopyComponent(mods[i], to);
  195. // TODO: Add method to modifiers so can deal with any special cases
  196. if ( com )
  197. {
  198. MegaModifier mod = (MegaModifier)com;
  199. mod.PostCopy(mods[i]);
  200. }
  201. }
  202. MegaWrap wrap = from.GetComponent<MegaWrap>();
  203. if ( wrap )
  204. CopyComponent(wrap, to);
  205. if ( mo )
  206. mo.MeshUpdated();
  207. to.name = from.name + " - Copy";
  208. return to;
  209. }
  210. static public GameObject DoCopyObjectsChildren(GameObject from)
  211. {
  212. GameObject parent = DoCopyObjects(from);
  213. for ( int i = 0; i < from.transform.childCount; i++ )
  214. {
  215. GameObject cobj = from.transform.GetChild(i).gameObject;
  216. GameObject newchild = DoCopyObjectsChildren(cobj);
  217. newchild.transform.parent = parent.transform;
  218. }
  219. return parent;
  220. }
  221. #if UNITY_EDITOR
  222. static Component CopyComponent(Component from, GameObject to)
  223. {
  224. bool en = false;
  225. Type tp = from.GetType();
  226. if ( tp.IsSubclassOf(typeof(Behaviour)) )
  227. {
  228. en = (from as Behaviour).enabled;
  229. }
  230. else
  231. {
  232. if ( tp.IsSubclassOf(typeof(Component)) && tp.GetProperty("enabled") != null )
  233. en = (bool)tp.GetProperty("enabled").GetValue(from, null);
  234. else
  235. en = true;
  236. }
  237. FieldInfo[] fields = tp.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly);
  238. PropertyInfo[] properties = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly);
  239. Component c = to.GetComponent(tp);
  240. if ( c == null )
  241. c = to.AddComponent(tp);
  242. if ( tp.IsSubclassOf(typeof(Behaviour)) )
  243. (c as Behaviour).enabled = en;
  244. else
  245. {
  246. if ( tp.IsSubclassOf(typeof(Component)) && tp.GetProperty("enabled") != null )
  247. tp.GetProperty("enabled").SetValue(c, en, null);
  248. }
  249. for ( int j = 0; j < fields.Length; j++ )
  250. fields[j].SetValue(c, fields[j].GetValue(from));
  251. for ( int j = 0; j < properties.Length; j++ )
  252. {
  253. if ( properties[j].CanWrite )
  254. properties[j].SetValue(c, properties[j].GetValue(from, null), null);
  255. }
  256. return c;
  257. }
  258. #else
  259. static Component CopyComponent(Component from, GameObject to)
  260. {
  261. return null;
  262. }
  263. #endif
  264. #if false
  265. static public void CopyFromTo1(GameObject obj, GameObject to)
  266. {
  267. Component[] components = obj.GetComponents<Component>();
  268. for ( int i = 0; i < components.Length; i++ )
  269. {
  270. bool en = false;
  271. Type tp = components[i].GetType();
  272. if ( tp.IsSubclassOf(typeof(Behaviour)) )
  273. {
  274. en = (components[i] as Behaviour).enabled;
  275. }
  276. else
  277. {
  278. if ( tp.IsSubclassOf(typeof(Component)) && tp.GetProperty("enabled") != null )
  279. en = (bool)tp.GetProperty("enabled").GetValue(components[i], null);
  280. else
  281. en = true;
  282. }
  283. FieldInfo[] fields = tp.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly);
  284. PropertyInfo[] properties = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly);
  285. Component c = to.GetComponent(tp);
  286. if ( c == null )
  287. c = to.AddComponent(tp);
  288. if ( tp.IsSubclassOf(typeof(Behaviour)) )
  289. (c as Behaviour).enabled = en;
  290. else
  291. {
  292. if ( tp.IsSubclassOf(typeof(Component)) && tp.GetProperty("enabled") != null )
  293. tp.GetProperty("enabled").SetValue(c, en, null);
  294. }
  295. for ( int j = 0; j < fields.Length; j++ )
  296. fields[j].SetValue(c, fields[j].GetValue(tp));
  297. for ( int j = 0; j < properties.Length; j++ )
  298. {
  299. if ( properties[j].CanWrite )
  300. properties[j].SetValue(c, properties[j].GetValue(tp, null), null);
  301. }
  302. }
  303. }
  304. #endif
  305. static public GameObject DeepCopy(GameObject subject)
  306. {
  307. GameObject clone = null;
  308. if ( subject )
  309. {
  310. clone = (GameObject)GameObject.Instantiate(subject);
  311. SkinnedMeshRenderer[] skinmesh = subject.GetComponentsInChildren<SkinnedMeshRenderer>();
  312. SkinnedMeshRenderer[] cskinmesh = clone.GetComponentsInChildren<SkinnedMeshRenderer>();
  313. int l = skinmesh.Length;
  314. for ( int i = 0; i < l; i++ )
  315. {
  316. Mesh mesh = skinmesh[i].sharedMesh;
  317. Mesh clonemesh = new Mesh();
  318. clonemesh.vertices = mesh.vertices;
  319. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  320. clonemesh.uv2 = mesh.uv2;
  321. clonemesh.uv3 = mesh.uv3;
  322. clonemesh.uv4 = mesh.uv4;
  323. #else
  324. clonemesh.uv1 = mesh.uv1;
  325. clonemesh.uv2 = mesh.uv2;
  326. #endif
  327. clonemesh.uv = mesh.uv;
  328. clonemesh.normals = mesh.normals;
  329. clonemesh.tangents = mesh.tangents;
  330. clonemesh.colors = mesh.colors;
  331. clonemesh.subMeshCount = mesh.subMeshCount;
  332. for ( int s = 0; s < mesh.subMeshCount; s++ )
  333. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  334. CopyBlendShapes(mesh, clonemesh);
  335. clonemesh.boneWeights = mesh.boneWeights;
  336. clonemesh.bindposes = mesh.bindposes;
  337. clonemesh.name = mesh.name + "_copy";
  338. clonemesh.RecalculateBounds();
  339. cskinmesh[i].sharedMesh = clonemesh;
  340. }
  341. MeshFilter[] mfs = subject.GetComponentsInChildren<MeshFilter>();
  342. MeshFilter[] clonemfs = clone.GetComponentsInChildren<MeshFilter>();
  343. MeshCollider[] mcs = clone.GetComponentsInChildren<MeshCollider>();
  344. MeshCollider[] clonemcs = clone.GetComponentsInChildren<MeshCollider>();
  345. for ( int i = 0; i < mfs.Length; i++ )
  346. {
  347. MeshFilter mf = mfs[i];
  348. MeshFilter clonemf = clonemfs[i];
  349. Mesh mesh = mf.sharedMesh;
  350. Mesh clonemesh = new Mesh();
  351. clonemesh.vertices = mesh.vertices;
  352. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  353. clonemesh.uv2 = mesh.uv2;
  354. clonemesh.uv3 = mesh.uv3;
  355. clonemesh.uv4 = mesh.uv4;
  356. #else
  357. clonemesh.uv1 = mesh.uv1;
  358. clonemesh.uv2 = mesh.uv2;
  359. #endif
  360. clonemesh.uv = mesh.uv;
  361. clonemesh.normals = mesh.normals;
  362. clonemesh.tangents = mesh.tangents;
  363. clonemesh.colors = mesh.colors;
  364. clonemesh.subMeshCount = mesh.subMeshCount;
  365. for ( int s = 0; s < mesh.subMeshCount; s++ )
  366. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  367. CopyBlendShapes(mesh, clonemesh);
  368. clonemesh.boneWeights = mesh.boneWeights;
  369. clonemesh.bindposes = mesh.bindposes;
  370. clonemesh.name = mesh.name + "_copy";
  371. clonemesh.RecalculateBounds();
  372. clonemf.sharedMesh = clonemesh;
  373. for ( int j = 0; j < mcs.Length; j++ )
  374. {
  375. MeshCollider mc = mcs[j];
  376. if ( mc.sharedMesh = mesh )
  377. clonemcs[j].sharedMesh = clonemesh;
  378. }
  379. }
  380. MegaModifyObject[] modobjs = clone.GetComponentsInChildren<MegaModifyObject>();
  381. for ( int i = 0; i < modobjs.Length; i++ )
  382. {
  383. modobjs[i].MeshUpdated();
  384. }
  385. }
  386. return clone;
  387. }
  388. static public GameObject InstanceObject(GameObject obj)
  389. {
  390. GameObject newobj = null;
  391. if ( obj )
  392. {
  393. MeshFilter mf = obj.GetComponent<MeshFilter>();
  394. MeshRenderer mr = obj.GetComponent<MeshRenderer>();
  395. if ( mf )
  396. {
  397. newobj = new GameObject();
  398. newobj.name = obj.name + " MegaInstance";
  399. MeshRenderer newmr = newobj.AddComponent<MeshRenderer>();
  400. MeshFilter newmf = newobj.AddComponent<MeshFilter>();
  401. newmf.sharedMesh = mf.sharedMesh;
  402. newmr.sharedMaterials = mr.sharedMaterials;
  403. }
  404. }
  405. return newobj;
  406. }
  407. public static Mesh DupMesh(Mesh mesh)
  408. {
  409. Mesh clonemesh = new Mesh();
  410. clonemesh.vertices = mesh.vertices;
  411. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  412. clonemesh.uv2 = mesh.uv2;
  413. clonemesh.uv3 = mesh.uv3;
  414. clonemesh.uv4 = mesh.uv4;
  415. #else
  416. clonemesh.uv1 = mesh.uv1;
  417. clonemesh.uv2 = mesh.uv2;
  418. #endif
  419. clonemesh.uv = mesh.uv;
  420. clonemesh.normals = mesh.normals;
  421. clonemesh.tangents = mesh.tangents;
  422. clonemesh.colors = mesh.colors;
  423. clonemesh.subMeshCount = mesh.subMeshCount;
  424. for ( int s = 0; s < mesh.subMeshCount; s++ )
  425. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  426. CopyBlendShapes(mesh, clonemesh);
  427. clonemesh.boneWeights = mesh.boneWeights;
  428. clonemesh.bindposes = mesh.bindposes;
  429. clonemesh.name = mesh.name + "_copy";
  430. clonemesh.RecalculateBounds();
  431. return clonemesh;
  432. }
  433. public static Mesh DupMesh(Mesh mesh, MegaModifyObject mod)
  434. {
  435. Mesh clonemesh = new Mesh();
  436. clonemesh.vertices = mod.verts; //mesh.vertices;
  437. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  438. clonemesh.uv2 = mesh.uv2;
  439. clonemesh.uv3 = mesh.uv3;
  440. clonemesh.uv4 = mesh.uv4;
  441. #else
  442. clonemesh.uv1 = mesh.uv1;
  443. clonemesh.uv2 = mesh.uv2;
  444. #endif
  445. clonemesh.uv = mod.uvs; //mesh.uv;
  446. clonemesh.normals = mesh.normals;
  447. clonemesh.tangents = mesh.tangents;
  448. clonemesh.colors = mesh.colors;
  449. clonemesh.subMeshCount = mesh.subMeshCount;
  450. for ( int s = 0; s < mesh.subMeshCount; s++ )
  451. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  452. CopyBlendShapes(mesh, clonemesh);
  453. clonemesh.boneWeights = mesh.boneWeights;
  454. clonemesh.bindposes = mesh.bindposes;
  455. clonemesh.name = mesh.name + "_copy";
  456. mod.RecalcNormals();
  457. clonemesh.RecalculateBounds();
  458. return clonemesh;
  459. }
  460. public static Mesh DupMesh(Mesh mesh, string suffix)
  461. {
  462. Mesh clonemesh = new Mesh();
  463. clonemesh.vertices = mesh.vertices;
  464. #if UNITY_5_0 || UNITY_5_1 || UNITY_5 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  465. clonemesh.uv2 = mesh.uv2;
  466. clonemesh.uv3 = mesh.uv3;
  467. clonemesh.uv4 = mesh.uv4;
  468. #else
  469. clonemesh.uv1 = mesh.uv1;
  470. clonemesh.uv2 = mesh.uv2;
  471. #endif
  472. clonemesh.uv = mesh.uv;
  473. clonemesh.normals = mesh.normals;
  474. clonemesh.tangents = mesh.tangents;
  475. clonemesh.colors = mesh.colors;
  476. clonemesh.subMeshCount = mesh.subMeshCount;
  477. for ( int s = 0; s < mesh.subMeshCount; s++ )
  478. clonemesh.SetTriangles(mesh.GetTriangles(s), s);
  479. CopyBlendShapes(mesh, clonemesh);
  480. clonemesh.boneWeights = mesh.boneWeights;
  481. clonemesh.bindposes = mesh.bindposes;
  482. clonemesh.name = mesh.name + suffix;
  483. clonemesh.RecalculateBounds();
  484. return clonemesh;
  485. }
  486. static public GameObject DuplicateObject(GameObject from)
  487. {
  488. GameObject newobj = null;
  489. if ( from )
  490. {
  491. newobj = (GameObject)GameObject.Instantiate(from);
  492. if ( newobj )
  493. {
  494. MeshFilter[] mfil = newobj.GetComponentsInChildren<MeshFilter>();
  495. for ( int i = 0; i < mfil.Length; i++ )
  496. mfil[i].sharedMesh = DupMesh(mfil[i].sharedMesh);
  497. SkinnedMeshRenderer[] skin = newobj.GetComponentsInChildren<SkinnedMeshRenderer>();
  498. for ( int i = 0; i < skin.Length; i++ )
  499. skin[i].sharedMesh = DupMesh(skin[i].sharedMesh);
  500. MegaModifyObject[] mobjs = newobj.GetComponentsInChildren<MegaModifyObject>();
  501. for ( int i = 0; i < mobjs.Length; i++ )
  502. mobjs[i].MeshUpdated();
  503. MegaModifier[] frommods = from.GetComponentsInChildren<MegaModifier>();
  504. MegaModifier[] tomods = newobj.GetComponentsInChildren<MegaModifier>();
  505. for ( int i = 0; i < frommods.Length; i++ )
  506. tomods[i].PostCopy(frommods[i]);
  507. MegaWrap[] wraps = newobj.GetComponentsInChildren<MegaWrap>();
  508. for ( int i = 0; i < wraps.Length; i++ )
  509. wraps[i].SetMesh();
  510. newobj.name = from.name + " - Copy";
  511. }
  512. }
  513. return newobj;
  514. }
  515. static public GameObject DuplicateObjectForPrefabOld(GameObject from)
  516. {
  517. GameObject newobj = null;
  518. if ( from )
  519. {
  520. newobj = (GameObject)GameObject.Instantiate(from);
  521. if ( newobj )
  522. {
  523. MegaModifyObject[] mobjs = newobj.GetComponentsInChildren<MegaModifyObject>();
  524. MeshFilter[] mfil = newobj.GetComponentsInChildren<MeshFilter>();
  525. for ( int i = 0; i < mfil.Length; i++ )
  526. mfil[i].sharedMesh = DupMesh(mfil[i].sharedMesh, mobjs[i]);
  527. SkinnedMeshRenderer[] skin = newobj.GetComponentsInChildren<SkinnedMeshRenderer>();
  528. for ( int i = 0; i < skin.Length; i++ )
  529. skin[i].sharedMesh = DupMesh(skin[i].sharedMesh, mobjs[i]);
  530. for ( int i = 0; i < mobjs.Length; i++ )
  531. mobjs[i].MeshUpdated();
  532. MegaModifier[] frommods = from.GetComponentsInChildren<MegaModifier>();
  533. MegaModifier[] tomods = newobj.GetComponentsInChildren<MegaModifier>();
  534. for ( int i = 0; i < frommods.Length; i++ )
  535. tomods[i].PostCopy(frommods[i]);
  536. MegaWrap[] wraps = newobj.GetComponentsInChildren<MegaWrap>();
  537. for ( int i = 0; i < wraps.Length; i++ )
  538. wraps[i].SetMesh();
  539. newobj.name = from.name; // + " - Copy";
  540. }
  541. }
  542. return newobj;
  543. }
  544. static public GameObject DuplicateObjectForPrefab(GameObject from)
  545. {
  546. GameObject newobj = null;
  547. if ( from )
  548. {
  549. newobj = (GameObject)GameObject.Instantiate(from);
  550. if ( newobj )
  551. {
  552. MegaModifyObject[] mobjs = newobj.GetComponentsInChildren<MegaModifyObject>();
  553. //MeshFilter[] mfil = newobj.GetComponentsInChildren<MeshFilter>();
  554. for ( int i = 0; i < mobjs.Length; i++ )
  555. {
  556. Mesh m = MegaModifyObject.FindMesh(mobjs[i].gameObject);
  557. if ( m )
  558. {
  559. Mesh newmesh = DupMesh(m, mobjs[i]);
  560. MegaModifyObject.SetMeshNew(mobjs[i].gameObject, newmesh);
  561. }
  562. }
  563. //for ( int i = 0; i < mfil.Length; i++ )
  564. //mfil[i].sharedMesh = DupMesh(mfil[i].sharedMesh, mobjs[i]);
  565. //SkinnedMeshRenderer[] skin = newobj.GetComponentsInChildren<SkinnedMeshRenderer>();
  566. //for ( int i = 0; i < skin.Length; i++ )
  567. //skin[i].sharedMesh = DupMesh(skin[i].sharedMesh, mobjs[i]);
  568. for ( int i = 0; i < mobjs.Length; i++ )
  569. mobjs[i].MeshUpdated();
  570. MegaModifier[] frommods = from.GetComponentsInChildren<MegaModifier>();
  571. MegaModifier[] tomods = newobj.GetComponentsInChildren<MegaModifier>();
  572. for ( int i = 0; i < frommods.Length; i++ )
  573. tomods[i].PostCopy(frommods[i]);
  574. MegaWrap[] wraps = newobj.GetComponentsInChildren<MegaWrap>();
  575. for ( int i = 0; i < wraps.Length; i++ )
  576. wraps[i].SetMesh();
  577. newobj.name = from.name; // + " - Copy";
  578. }
  579. }
  580. return newobj;
  581. }
  582. }
  583. #endif