MB3_MBVersionConcrete.cs 10 KB

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