MB3_MBVersionConcrete.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * \brief Hax! DLLs cannot interpret preprocessor directives, so this class acts as a "bridge"
  3. */
  4. using System;
  5. using UnityEngine;
  6. using System.Collections;
  7. namespace DigitalOpus.MB.Core
  8. {
  9. public class MBVersionConcrete : MBVersionInterface
  10. {
  11. public string version()
  12. {
  13. return "3.28.1";
  14. }
  15. public int GetMajorVersion()
  16. {
  17. /*
  18. #if UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
  19. return 3;
  20. #elif UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
  21. return 4;
  22. #else
  23. return 5;
  24. #endif
  25. */
  26. string v = Application.unityVersion;
  27. String[] vs = v.Split(new char[] { '.' });
  28. return Int32.Parse(vs[0]);
  29. }
  30. public int GetMinorVersion()
  31. {
  32. /*
  33. #if UNITY_3_0 || UNITY_3_0_0
  34. return 0;
  35. #elif UNITY_3_1
  36. return 1;
  37. #elif UNITY_3_2
  38. return 2;
  39. #elif UNITY_3_3
  40. return 3;
  41. #elif UNITY_3_4
  42. return 4;
  43. #elif UNITY_3_5
  44. return 5;
  45. #elif UNITY_4_0 || UNITY_4_0_1
  46. return 0;
  47. #elif UNITY_4_1
  48. return 1;
  49. #elif UNITY_4_2
  50. return 2;
  51. #elif UNITY_4_3
  52. return 3;
  53. #elif UNITY_4_4
  54. return 4;
  55. #elif UNITY_4_5
  56. return 5;
  57. #else
  58. return 0;
  59. #endif
  60. */
  61. string v = Application.unityVersion;
  62. String[] vs = v.Split(new char[] { '.' });
  63. return Int32.Parse(vs[1]);
  64. }
  65. public bool GetActive(GameObject go)
  66. {
  67. #if UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
  68. return go.active;
  69. #else
  70. return go.activeInHierarchy;
  71. #endif
  72. }
  73. public void SetActive(GameObject go, bool isActive)
  74. {
  75. #if UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
  76. go.active = isActive;
  77. #else
  78. go.SetActive(isActive);
  79. #endif
  80. }
  81. public void SetActiveRecursively(GameObject go, bool isActive)
  82. {
  83. #if UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
  84. go.SetActiveRecursively(isActive);
  85. #else
  86. go.SetActive(isActive);
  87. #endif
  88. }
  89. public UnityEngine.Object[] FindSceneObjectsOfType(Type t)
  90. {
  91. #if UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
  92. return GameObject.FindSceneObjectsOfType(t);
  93. #else
  94. return GameObject.FindObjectsOfType(t);
  95. #endif
  96. }
  97. public void OptimizeMesh(Mesh m)
  98. {
  99. #if UNITY_EDITOR
  100. #if UNITY_5_5_OR_NEWER
  101. UnityEditor.MeshUtility.Optimize(m);
  102. #else
  103. m.Optimize();
  104. #endif
  105. #endif
  106. }
  107. public bool IsRunningAndMeshNotReadWriteable(Mesh m)
  108. {
  109. if (Application.isPlaying)
  110. {
  111. #if UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
  112. return false;
  113. #else
  114. return !m.isReadable;
  115. #endif
  116. }
  117. else
  118. {
  119. return false;
  120. }
  121. }
  122. Vector2 _HALF_UV = new Vector2(.5f, .5f);
  123. public Vector2[] GetMeshUV1s(Mesh m, MB2_LogLevel LOG_LEVEL)
  124. {
  125. Vector2[] uv;
  126. #if (UNITY_4_6 || UNITY_4_7 || UNITY_4_5 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5)
  127. uv = m.uv1;
  128. #else
  129. if (LOG_LEVEL >= MB2_LogLevel.warn) MB2_Log.LogDebug("UV1 does not exist in Unity 5+");
  130. uv = m.uv;
  131. #endif
  132. if (uv.Length == 0)
  133. {
  134. if (LOG_LEVEL >= MB2_LogLevel.debug) MB2_Log.LogDebug("Mesh " + m + " has no uv1s. Generating");
  135. if (LOG_LEVEL >= MB2_LogLevel.warn) Debug.LogWarning("Mesh " + m + " didn't have uv1s. Generating uv1s.");
  136. uv = new Vector2[m.vertexCount];
  137. for (int i = 0; i < uv.Length; i++) { uv[i] = _HALF_UV; }
  138. }
  139. return uv;
  140. }
  141. public Vector2[] GetMeshUV3orUV4(Mesh m, bool get3, MB2_LogLevel LOG_LEVEL)
  142. {
  143. Vector2[] uvs;
  144. #if (UNITY_4_6 || UNITY_4_7 || UNITY_4_5 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5)
  145. if (LOG_LEVEL >= MB2_LogLevel.warn) MB2_Log.LogDebug("UV3 and UV4 do not exist in Unity 4");
  146. uvs = m.uv;
  147. #else
  148. if (get3) uvs = m.uv3;
  149. else uvs = m.uv4;
  150. #endif
  151. if (uvs.Length == 0)
  152. {
  153. if (LOG_LEVEL >= MB2_LogLevel.debug) MB2_Log.LogDebug("Mesh " + m + " has no uv" + (get3 ? "3" : "4") + ". Generating");
  154. uvs = new Vector2[m.vertexCount];
  155. for (int i = 0; i < uvs.Length; i++) { uvs[i] = _HALF_UV; }
  156. }
  157. return uvs;
  158. }
  159. public void MeshClear(Mesh m, bool t)
  160. {
  161. #if UNITY_3_5
  162. m.Clear();
  163. #else
  164. m.Clear(t);
  165. #endif
  166. }
  167. public void MeshAssignUV3(Mesh m, Vector2[] uv3s)
  168. {
  169. #if (UNITY_4_6 || UNITY_4_7 || UNITY_4_5 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5)
  170. Debug.LogWarning("UV3 was checked but UV3 does not exist in Unity 4");
  171. #else
  172. m.uv3 = uv3s;
  173. #endif
  174. }
  175. public void MeshAssignUV4(Mesh m, Vector2[] uv4s)
  176. {
  177. #if (UNITY_4_6 || UNITY_4_7 || UNITY_4_5 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5)
  178. Debug.LogWarning("UV4 was checked but UV4 does not exist in Unity 4");
  179. #else
  180. m.uv4 = uv4s;
  181. #endif
  182. }
  183. public Vector4 GetLightmapTilingOffset(Renderer r)
  184. {
  185. #if (UNITY_4_6 || UNITY_4_7 || UNITY_4_5 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5)
  186. return r.lightmapTilingOffset ;
  187. #else
  188. return r.lightmapScaleOffset; //r.lightmapScaleOffset ;
  189. #endif
  190. }
  191. #if UNITY_5_OR_NEWER
  192. public Transform[] GetBones(Renderer r)
  193. {
  194. if (r is SkinnedMeshRenderer)
  195. {
  196. Transform[] bone;
  197. //check if I need to deoptimize
  198. Animator anim = r.GetComponentInParent<Animator>();
  199. if (anim != null)
  200. {
  201. if (anim.hasTransformHierarchy)
  202. {
  203. //nothing to do
  204. } else if (anim.isOptimizable)
  205. {
  206. //Deoptimize
  207. AnimatorUtility.DeoptimizeTransformHierarchy(anim.gameObject);
  208. }
  209. else
  210. {
  211. Debug.LogError("Could not getBones. Bones optimized but could not create TransformHierarchy.");
  212. return null;
  213. }
  214. bone = ((SkinnedMeshRenderer)r).bones;
  215. //can't deoptimize here because the transforms need to exist for the combined mesh
  216. } else
  217. {
  218. //no Animator component but check to see if bones were optimized on import
  219. bone = ((SkinnedMeshRenderer)r).bones;
  220. #if UNITY_EDITOR
  221. if (bone.Length == 0)
  222. {
  223. Mesh m = ((SkinnedMeshRenderer)r).sharedMesh;
  224. if (m.bindposes.Length != bone.Length) Debug.LogError("SkinnedMesh (" + r.gameObject + ") in the list of objects to combine has no bones. Check that 'optimize game object' is not checked in the 'Rig' tab of the asset importer. Mesh Baker cannot combine optimized skinned meshes because the bones are not available.");
  225. }
  226. #endif
  227. }
  228. return bone;
  229. }
  230. else if (r is MeshRenderer)
  231. {
  232. Transform[] bone = new Transform[1];
  233. bone[0] = r.transform;
  234. return bone;
  235. }
  236. else {
  237. Debug.LogError("Could not getBones. Object is not a Renderer.");
  238. return null;
  239. }
  240. }
  241. #else
  242. public Transform[] GetBones(Renderer r)
  243. {
  244. if (r is SkinnedMeshRenderer)
  245. {
  246. Transform[] bone = ((SkinnedMeshRenderer)r).bones;
  247. #if UNITY_EDITOR
  248. if (bone.Length == 0)
  249. {
  250. Mesh m = ((SkinnedMeshRenderer)r).sharedMesh;
  251. if (m.bindposes.Length != bone.Length) Debug.LogError("SkinnedMesh (" + r.gameObject + ") in the list of objects to combine has no bones. Check that 'optimize game object' is not checked in the 'Rig' tab of the asset importer. Mesh Baker cannot combine optimized skinned meshes because the bones are not available.");
  252. }
  253. #endif
  254. return bone;
  255. }
  256. else if (r is MeshRenderer)
  257. {
  258. Transform[] bone = new Transform[1];
  259. bone[0] = r.transform;
  260. return bone;
  261. }
  262. else
  263. {
  264. Debug.LogError("Could not getBones. Object does not have a renderer");
  265. return null;
  266. }
  267. }
  268. #endif
  269. public int GetBlendShapeFrameCount(Mesh m, int shapeIndex)
  270. {
  271. #if UNITY_5_3_OR_NEWER
  272. return m.GetBlendShapeFrameCount(shapeIndex);
  273. #else
  274. return 0;
  275. #endif
  276. }
  277. public float GetBlendShapeFrameWeight(Mesh m, int shapeIndex, int frameIndex)
  278. {
  279. #if UNITY_5_3_OR_NEWER
  280. return m.GetBlendShapeFrameWeight(shapeIndex, frameIndex);
  281. #else
  282. return 0;
  283. #endif
  284. }
  285. public void GetBlendShapeFrameVertices(Mesh m, int shapeIndex, int frameIndex, Vector3[] vs, Vector3[] ns, Vector3[] ts)
  286. {
  287. #if UNITY_5_3_OR_NEWER
  288. m.GetBlendShapeFrameVertices(shapeIndex, frameIndex, vs, ns, ts);
  289. #endif
  290. }
  291. public void ClearBlendShapes(Mesh m)
  292. {
  293. #if UNITY_5_3_OR_NEWER
  294. m.ClearBlendShapes();
  295. #endif
  296. }
  297. public void AddBlendShapeFrame(Mesh m, string nm, float wt, Vector3[] vs, Vector3[] ns, Vector3[] ts)
  298. {
  299. #if UNITY_5_3_OR_NEWER
  300. m.AddBlendShapeFrame(nm, wt, vs, ns, ts);
  301. #endif
  302. }
  303. public int MaxMeshVertexCount()
  304. {
  305. #if UNITY_2017_3_OR_NEWER
  306. return 2147483646;
  307. #else
  308. return 65535;
  309. #endif
  310. }
  311. public void SetMeshIndexFormatAndClearMesh(Mesh m, int numVerts, bool vertices, bool justClearTriangles)
  312. {
  313. #if UNITY_2017_3_OR_NEWER
  314. if (vertices && numVerts > 65534 && m.indexFormat == UnityEngine.Rendering.IndexFormat.UInt16)
  315. {
  316. MBVersion.MeshClear(m, false);
  317. m.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
  318. return;
  319. }
  320. else if (vertices && numVerts <= 65534 && m.indexFormat == UnityEngine.Rendering.IndexFormat.UInt32)
  321. {
  322. MBVersion.MeshClear(m, false);
  323. m.indexFormat = UnityEngine.Rendering.IndexFormat.UInt16;
  324. return;
  325. }
  326. #endif
  327. if (justClearTriangles)
  328. {
  329. MBVersion.MeshClear(m, true); //clear just triangles
  330. }
  331. else
  332. {//clear all the data and start with a blank mesh
  333. MBVersion.MeshClear(m, false);
  334. }
  335. }
  336. public bool GraphicsUVStartsAtTop()
  337. {
  338. #if UNITY_2017_1_OR_NEWER
  339. return SystemInfo.graphicsUVStartsAtTop;
  340. #else
  341. if (SystemInfo.graphicsDeviceVersion.Contains("metal"))
  342. {
  343. return false;
  344. }
  345. else
  346. {
  347. // "opengl es, direct3d"
  348. return true;
  349. }
  350. #endif
  351. }
  352. }
  353. }