MegaCopyObject.cs 20 KB

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