InspectorsBase.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace VertexAnimationTools_30 {
  6. public class InspectorsBase : Editor {
  7. internal GUIContent[] MeshCompressionNames = new GUIContent[4] { new GUIContent("Off"), new GUIContent("Low"), new GUIContent("Medium"), new GUIContent("High") };
  8. internal int[] MeshCompressionIndeces = new int[4] { 0, 1, 2, 3 };
  9. internal GUIContent s_meshCompression = new GUIContent("Mesh compression", "Mesh compression settings. Comperessing meshes saves space in the built game, but more compression introduces more artifacts in vertex data");
  10. internal SerializedProperty prp_meshCompression;
  11. internal GUIContent s_clipNormalizedTime = new GUIContent("Normalized Time", "The time percentage of overall clip length. The Auto Playback Mode set how this value will be changed on Play mode. This property is animable");
  12. internal GUIContent s_clipWeight = new GUIContent("Weight", "The weight of point cache clip. Used for clips blending and transitions. This property is animable");
  13. internal GUIContent s_clipPlaybackMode = new GUIContent("Auto Playback", "Simple animators for NormalizedTime value. Works only on Play mode. Select None when updated manually, or by script, or by custom Animation ");
  14. internal SerializedProperty prp_clipPlaybackMode;
  15. internal GUIContent s_playerUpdateMode = new GUIContent("Update Order", "Event Function used for execution. Select Manually in case player updated by by code by call UpdatePlayback() method");
  16. internal SerializedProperty prp_playerUpdateMode;
  17. internal GUIContent s_normalizedTime = new GUIContent("Normalized time", "Normalized time of the animation");
  18. internal SerializedProperty prp_normalizedTime;
  19. internal GUIContent s_useTimescale = new GUIContent("Use Time Scale", "When enabled, Time.timeScale affects the speed of Auto Playback, otherwise it does not.");
  20. internal SerializedProperty prp_useTimescale;
  21. internal GUIContent s_importButton = new GUIContent("Import", "Import");
  22. internal GUIContent s_importButtonRequireReimport = new GUIContent("Import", "Import (require re-import)");
  23. internal GUIContent s_importCustomRange = new GUIContent("Import custom range", "When off, all frames will be exported, otherwise selected range only");
  24. internal SerializedProperty prp_importCustomRange;
  25. internal SerializedProperty prp_importFrom;
  26. internal SerializedProperty prp_importTo;
  27. internal SerializedProperty prp_filesSortMode;
  28. internal GUIContent s_filesSortMode = new GUIContent("Sort mode", "Sorting mode for obj files sequence");
  29. internal GUIContent s_TransitionMode = new GUIContent("Transition", "Controls copying selected frames range outside Import Custom Range into it using dissolve. Suitable to making seamless loop transition. ");
  30. internal GUIContent s_TransitionLengthBegin = new GUIContent("Frames count", "Begin transition length. Cannot be longer than custom clip`s length");
  31. internal GUIContent s_TransitionLengthEnd = new GUIContent("Frames count", "End transition length. Cannot be longer than custom clip`s length");
  32. internal GUIContent s_DrawMotionPath = new GUIContent("Draw motion path", "Displays motion paths gizmos for vertices wich defined in Import/Generate Path/Index Step.");
  33. internal GUIContent s_MotionPathIconSize = new GUIContent("Icon size", "Size of motion path icons.");
  34. internal GUIContent s_swapYZAxis = new GUIContent("Swap YZ", "Swap Y and Z axis of coorinates. Suitable when model has Z axis aligned to up direction");
  35. internal SerializedProperty prp_swapYZAxis;
  36. internal GUIContent s_pivotOffset = new GUIContent("Pivot offset", "Offset model about its local coordinates");
  37. internal SerializedProperty prp_pivotOffset;
  38. internal GUIContent s_scaleFactor = new GUIContent("Scale", "Scale model about its local coordinates");
  39. internal SerializedProperty prp_scaleFactor;
  40. internal GUIContent s_flipNormals = new GUIContent("Flip Normals", "When On, reverse the direction of all surface normals of the mesh");
  41. internal SerializedProperty prp_flipNormals;
  42. internal SerializedProperty prp_generateMaterials;
  43. internal GUIContent s_generateMaterials = new GUIContent("Generate materials", "When On, materials will be created and applied. Set Off if you planned to set materials manually in Renderer component.");
  44. internal SerializedProperty prp_smoothingGroupImportMode;
  45. internal GUIContent s_smoothingGroupImportMode = new GUIContent("Smoothing groups", "Controls how smoothing groups is applied to a mesh");
  46. internal SerializedProperty prp_normalRecalculationMode;
  47. internal GUIContent s_normalRecalculationMode = new GUIContent("Normals", "Defines how the mesh normals are calculated.");
  48. internal SerializedProperty prp_savePortableData;
  49. internal GUIContent s_savePortableData = new GUIContent("Save Portable Data", "Saves Meshes as .mesh and Clips as .anim assets");
  50. internal GUIContent s_brightIcons = new GUIContent("Bright icons", "Enables bright inspector icons");
  51. #if UNITY_2017_3_OR_NEWER
  52. internal SerializedProperty prp_IndexFormat;
  53. internal GUIContent s_IndexFormat = new GUIContent("Index format", "Format of the mesh index buffer data. Index buffer can either be 16 bit (supports up to 65535 vertices in a mesh, takes less memory and bandwidth), or 32 bit (supports up to 4 billion vertices).");
  54. #endif
  55. internal SerializedProperty prp_OptimizeMesh;
  56. internal GUIContent s_OptimizeMesh = new GUIContent("Optimize mesh", "This option might take a while but will make the geometry displayed be faster.");
  57. static EditorResourcesHolderSO _resourceHolder;
  58. public static EditorResourcesHolderSO ResourceHolder {
  59. get {
  60. if (_resourceHolder == null) {
  61. string[] result = UnityEditor.AssetDatabase.FindAssets("pc2unityResourcesHolder");
  62. if (result != null && result.Length > 0) {
  63. string path = AssetDatabase.GUIDToAssetPath(result[0]);
  64. _resourceHolder = (EditorResourcesHolderSO)AssetDatabase.LoadAssetAtPath(path, typeof(EditorResourcesHolderSO));
  65. }
  66. }
  67. return _resourceHolder;
  68. }
  69. }
  70. internal List<Vector3[]> GizmoAxisPoints;
  71. bool _brightIcons;
  72. protected bool brightIcons {
  73. get {
  74. return _brightIcons;
  75. }
  76. set {
  77. if (value != _brightIcons) {
  78. _brightIcons = value;
  79. EditorPrefs.SetBool("pc2unityBrightIcons", _brightIcons);
  80. }
  81. }
  82. }
  83. Vector3 qv(float xy, float z, int idx) {
  84. int[] signMask = new int[8] { -1, -1, -1, 1, 1, 1, 1, -1 };
  85. return new Vector3(xy * signMask[idx * 2], xy * signMask[idx * 2 + 1], z);
  86. }
  87. internal void OnEnableBase() {
  88. _brightIcons = EditorPrefs.GetBool("pc2unityBrightIcons", false);
  89. s_importButtonRequireReimport.image = IsDarkTheme ? ResourceHolder.RequireReimportDark : ResourceHolder.RequireReimportLight;
  90. GizmoAxisPoints = new List<Vector3[]>();
  91. GizmoAxisPoints.Add(new Vector3[3] { new Vector3(0, 0, 0), qv(1, 2, 0), qv(1, 2, 1) });
  92. GizmoAxisPoints.Add(new Vector3[3] { new Vector3(0, 0, 0), qv(1, 2, 1), qv(1, 2, 2) });
  93. GizmoAxisPoints.Add(new Vector3[3] { new Vector3(0, 0, 0), qv(1, 2, 2), qv(1, 2, 3) });
  94. GizmoAxisPoints.Add(new Vector3[3] { new Vector3(0, 0, 0), qv(1, 2, 3), qv(1, 2, 0) });
  95. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 2, 0), qv(1, 2, 1), qv(1, 14, 1), qv(1, 14, 0) });
  96. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 2, 1), qv(1, 2, 2), qv(1, 14, 2), qv(1, 14, 1) });
  97. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 2, 2), qv(1, 2, 3), qv(1, 14, 3), qv(1, 14, 2) });
  98. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 2, 3), qv(1, 2, 0), qv(1, 14, 0), qv(1, 14, 3) });
  99. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 14, 0), qv(1, 14, 1), qv(2, 14, 1), qv(2, 14, 0) });
  100. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 14, 1), qv(1, 14, 2), qv(2, 14, 2), qv(2, 14, 1) });
  101. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 14, 2), qv(1, 14, 3), qv(2, 14, 3), qv(2, 14, 2) });
  102. GizmoAxisPoints.Add(new Vector3[4] { qv(1, 14, 3), qv(1, 14, 0), qv(2, 14, 0), qv(2, 14, 3) });
  103. GizmoAxisPoints.Add(new Vector3[3] { qv(0, 18, 0), qv(2, 14, 1), qv(2, 14, 0) });
  104. GizmoAxisPoints.Add(new Vector3[3] { qv(0, 18, 1), qv(2, 14, 2), qv(2, 14, 1) });
  105. GizmoAxisPoints.Add(new Vector3[3] { qv(0, 18, 2), qv(2, 14, 3), qv(2, 14, 2) });
  106. GizmoAxisPoints.Add(new Vector3[3] { qv(0, 18, 3), qv(2, 14, 0), qv(2, 14, 3) });
  107. }
  108. internal string GetResentDirectory() {
  109. string cachedDirectoryPath = EditorPrefs.GetString("pc2unityLastOpenedDirectory");
  110. if (!System.IO.Directory.Exists(cachedDirectoryPath)) {
  111. cachedDirectoryPath = EditorPrefs.GetString("pc2unityLastOpenedDirectoryParent");
  112. }
  113. return cachedDirectoryPath;
  114. }
  115. internal void SetResentDirectory(string filePath) {
  116. EditorPrefs.SetString("pc2unityLastOpenedDirectory", new System.IO.FileInfo(filePath).Directory.FullName);
  117. EditorPrefs.SetString("pc2unityLastOpenedDirectoryParent", new System.IO.FileInfo(filePath).Directory.Parent.FullName);
  118. }
  119. public void DrawHandleAxis(Color c) {
  120. Handles.color = new Color(c.r, c.g, c.b, 0.25f);
  121. Handles.DrawAAConvexPolygon(GizmoAxisPoints[0]);
  122. Handles.DrawAAConvexPolygon(GizmoAxisPoints[1]);
  123. Handles.DrawAAConvexPolygon(GizmoAxisPoints[2]);
  124. Handles.DrawAAConvexPolygon(GizmoAxisPoints[3]);
  125. Handles.DrawAAConvexPolygon(GizmoAxisPoints[4]);
  126. Handles.DrawAAConvexPolygon(GizmoAxisPoints[5]);
  127. Handles.DrawAAConvexPolygon(GizmoAxisPoints[6]);
  128. Handles.DrawAAConvexPolygon(GizmoAxisPoints[7]);
  129. Handles.DrawAAConvexPolygon(GizmoAxisPoints[8]);
  130. Handles.DrawAAConvexPolygon(GizmoAxisPoints[9]);
  131. Handles.DrawAAConvexPolygon(GizmoAxisPoints[10]);
  132. Handles.DrawAAConvexPolygon(GizmoAxisPoints[11]);
  133. Handles.DrawAAConvexPolygon(GizmoAxisPoints[12]);
  134. Handles.DrawAAConvexPolygon(GizmoAxisPoints[13]);
  135. Handles.DrawAAConvexPolygon(GizmoAxisPoints[14]);
  136. Handles.DrawAAConvexPolygon(GizmoAxisPoints[15]);
  137. Handles.color = new Color(c.r, c.g, c.b, 0.5f);
  138. Handles.DrawPolyLine(GizmoAxisPoints[0]);
  139. Handles.DrawPolyLine(GizmoAxisPoints[1]);
  140. Handles.DrawPolyLine(GizmoAxisPoints[2]);
  141. Handles.DrawPolyLine(GizmoAxisPoints[3]);
  142. Handles.DrawPolyLine(GizmoAxisPoints[4]);
  143. Handles.DrawPolyLine(GizmoAxisPoints[5]);
  144. Handles.DrawPolyLine(GizmoAxisPoints[6]);
  145. Handles.DrawPolyLine(GizmoAxisPoints[7]);
  146. Handles.DrawPolyLine(GizmoAxisPoints[12]);
  147. Handles.DrawPolyLine(GizmoAxisPoints[13]);
  148. Handles.DrawPolyLine(GizmoAxisPoints[14]);
  149. Handles.DrawPolyLine(GizmoAxisPoints[15]);
  150. }
  151. public void DrawTM(Matrix4x4 tm, float scale) {
  152. Matrix4x4 original = Handles.matrix;
  153. float size = HandleUtility.GetHandleSize(tm.GetColumn(3)) * scale * 0.01f;
  154. Vector4 x = tm.GetColumn(0) * size;
  155. Vector4 y = tm.GetColumn(1) * size;
  156. Vector4 z = tm.GetColumn(2) * size;
  157. tm.SetColumn(0, x);
  158. tm.SetColumn(1, y);
  159. tm.SetColumn(2, z);
  160. Handles.matrix = tm;
  161. DrawHandleAxis(Color.blue);
  162. tm.SetColumn(0, z);
  163. tm.SetColumn(1, y);
  164. tm.SetColumn(2, x);
  165. Handles.matrix = tm;
  166. DrawHandleAxis(Color.red);
  167. tm.SetColumn(0, x);
  168. tm.SetColumn(1, z);
  169. tm.SetColumn(2, y);
  170. Handles.matrix = tm;
  171. DrawHandleAxis(Color.green);
  172. Handles.matrix = original;
  173. }
  174. public static Object AddToAsset(Object asset, Object assetToAdd) {
  175. if (assetToAdd == null) {
  176. Debug.LogError("Trying to add null object");
  177. return null;
  178. }
  179. Object[] existing = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(asset));
  180. for (int i = 0; i < existing.Length; i++) {
  181. if (existing[i] == assetToAdd) {
  182. return existing[i];
  183. }
  184. }
  185. AssetDatabase.AddObjectToAsset(assetToAdd, asset);
  186. return AddToAsset(asset, assetToAdd);
  187. }
  188. internal bool IsDarkTheme {
  189. get {
  190. return brightIcons || PlayerSettings.advancedLicense;
  191. }
  192. }
  193. internal GUIStyle FoldoutToggleStyle {
  194. get {
  195. return IsDarkTheme ? ResourceHolder.FoldoutToggleDark : ResourceHolder.FoldoutToggle;
  196. }
  197. }
  198. GUIStyle quadButtonStyle;
  199. internal GUIStyle QuadButtonStyle{
  200. get {
  201. return ResourceHolder.QuadIconButtonStyle;
  202. }
  203. }
  204. GUIStyle boldLabelStyle;
  205. internal GUIStyle BoldLabelStyle {
  206. get {
  207. if (boldLabelStyle == null) {
  208. boldLabelStyle = new GUIStyle(EditorStyles.boldLabel);
  209. }
  210. return boldLabelStyle;
  211. }
  212. }
  213. GUIStyle labelStyle;
  214. internal GUIStyle LabelStyle {
  215. get {
  216. if (labelStyle == null) {
  217. labelStyle = new GUIStyle(EditorStyles.label);
  218. }
  219. return labelStyle;
  220. }
  221. }
  222. internal Texture2D AddButtonTexture {
  223. get {
  224. return IsDarkTheme? ResourceHolder.AddDark: ResourceHolder.AddLight;
  225. }
  226. }
  227. internal Texture2D RemoveButtonTexture {
  228. get {
  229. return IsDarkTheme ? ResourceHolder.RemoveDark : ResourceHolder.RemoveLight;
  230. }
  231. }
  232. internal Texture2D RefreshButtonTexture {
  233. get {
  234. return IsDarkTheme ? ResourceHolder.RefreshDark : ResourceHolder.RefreshLight;
  235. }
  236. }
  237. internal Texture2D GraphBackgroundTexture {
  238. get {
  239. return IsDarkTheme ? ResourceHolder.Graph0101BackgroundDark : ResourceHolder.Graph0101BackgroundLight;
  240. }
  241. }
  242. public static void CleanupAsset (Object asset, List<Object> usedObjects ) {
  243. string existingPath = AssetDatabase.GetAssetPath(asset);
  244. Object[] existing = AssetDatabase.LoadAllAssetsAtPath(existingPath);
  245. List<Object> toDelete = new List<Object>();
  246. for (int i = 0; i < existing.Length; i++) {
  247. Object obj = existing[i];
  248. if (obj == asset) {
  249. continue;
  250. }
  251. bool isUsed = false;
  252. for (int u = 0; u < usedObjects.Count; u++) {
  253. if (usedObjects[u] == obj) {
  254. isUsed = true;
  255. break;
  256. }
  257. }
  258. if (isUsed == false) {
  259. toDelete.Add(obj);
  260. }
  261. }
  262. for (int i = 0; i<toDelete.Count; i++) {
  263. DestroyImmediate(toDelete[i], true);
  264. }
  265. }
  266. }
  267. }