Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.33 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4.  
  5. namespace Cinemachine.Editor
  6. {
  7. [InitializeOnLoad]
  8. internal sealed class CinemachineSettings
  9. {
  10. public static class CinemachineCoreSettings
  11. {
  12. private static readonly string hShowInGameGuidesKey = "CNMCN_Core_ShowInGameGuides";
  13. public static bool ShowInGameGuides
  14. {
  15. get { return EditorPrefs.GetBool(hShowInGameGuidesKey, true); }
  16. set
  17. {
  18. if (ShowInGameGuides != value)
  19. {
  20. EditorPrefs.SetBool(hShowInGameGuidesKey, value);
  21. UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
  22. }
  23. }
  24. }
  25.  
  26. private static readonly string kCoreActiveGizmoColourKey = "CNMCN_Core_Active_Gizmo_Colour";
  27. public static readonly Color kDefaultActiveColour = new Color32(255, 0, 0, 100);
  28. public static Color ActiveGizmoColour
  29. {
  30. get
  31. {
  32. string packedColour = EditorPrefs.GetString(kCoreActiveGizmoColourKey, PackColor(kDefaultActiveColour));
  33. return UnpackColour(packedColour);
  34. }
  35.  
  36. set
  37. {
  38. if (ActiveGizmoColour != value)
  39. {
  40. string packedColour = PackColor(value);
  41. EditorPrefs.SetString(kCoreActiveGizmoColourKey, packedColour);
  42. }
  43. }
  44. }
  45.  
  46. private static readonly string kCoreInactiveGizmoColourKey = "CNMCN_Core_Inactive_Gizmo_Colour";
  47. public static readonly Color kDefaultInactiveColour = new Color32(9, 54, 87, 100);
  48. public static Color InactiveGizmoColour
  49. {
  50. get
  51. {
  52. string packedColour = EditorPrefs.GetString(kCoreInactiveGizmoColourKey, PackColor(kDefaultInactiveColour));
  53. return UnpackColour(packedColour);
  54. }
  55.  
  56. set
  57. {
  58. if (InactiveGizmoColour != value)
  59. {
  60. string packedColour = PackColor(value);
  61. EditorPrefs.SetString(kCoreInactiveGizmoColourKey, packedColour);
  62. }
  63. }
  64. }
  65. }
  66.  
  67. public static class ComposerSettings
  68. {
  69. private static readonly string kOverlayOpacityKey = "CNMCN_Overlay_Opacity";
  70. private static readonly string kComposerHardBoundsColourKey = "CNMCN_Composer_HardBounds_Colour";
  71. private static readonly string kComposerSoftBoundsColourKey = "CNMCN_Composer_SoftBounds_Colour";
  72. private static readonly string kComposerTargetColourKey = "CNMCN_Composer_Target_Colour";
  73. private static readonly string kComposerTargetSizeKey = "CNMCN_Composer_Target_Size";
  74.  
  75. public const float kDefaultOverlayOpacity = 0.15f;
  76. public static readonly Color kDefaultHardBoundsColour = new Color32(255, 0, 72, 255);
  77. public static readonly Color kDefaultSoftBoundsColour = new Color32(0, 194, 255, 255);
  78. public static readonly Color kDefaultTargetColour = new Color32(255, 254, 25, 255);
  79.  
  80. public static float OverlayOpacity
  81. {
  82. get { return EditorPrefs.GetFloat(kOverlayOpacityKey, kDefaultOverlayOpacity); }
  83. set
  84. {
  85. if (value != OverlayOpacity)
  86. {
  87. EditorPrefs.SetFloat(kOverlayOpacityKey, value);
  88. }
  89. }
  90. }
  91.  
  92. public static Color HardBoundsOverlayColour
  93. {
  94. get
  95. {
  96. string packedColour = EditorPrefs.GetString(kComposerHardBoundsColourKey, PackColor(kDefaultHardBoundsColour));
  97. return UnpackColour(packedColour);
  98. }
  99.  
  100. set
  101. {
  102. if (HardBoundsOverlayColour != value)
  103. {
  104. string packedColour = PackColor(value);
  105. EditorPrefs.SetString(kComposerHardBoundsColourKey, packedColour);
  106. }
  107. }
  108. }
  109.  
  110. public static Color SoftBoundsOverlayColour
  111. {
  112. get
  113. {
  114. string packedColour = EditorPrefs.GetString(kComposerSoftBoundsColourKey, PackColor(kDefaultSoftBoundsColour));
  115. return UnpackColour(packedColour);
  116. }
  117.  
  118. set
  119. {
  120. if (SoftBoundsOverlayColour != value)
  121. {
  122. string packedColour = PackColor(value);
  123. EditorPrefs.SetString(kComposerSoftBoundsColourKey, packedColour);
  124. }
  125. }
  126. }
  127.  
  128. public static Color TargetColour
  129. {
  130. get
  131. {
  132. string packedColour = EditorPrefs.GetString(kComposerTargetColourKey, PackColor(kDefaultTargetColour));
  133. return UnpackColour(packedColour);
  134. }
  135.  
  136. set
  137. {
  138. if (TargetColour != value)
  139. {
  140. string packedColour = PackColor(value);
  141. EditorPrefs.SetString(kComposerTargetColourKey, packedColour);
  142. }
  143. }
  144. }
  145.  
  146. public static float TargetSize
  147. {
  148. get
  149. {
  150. return EditorPrefs.GetFloat(kComposerTargetSizeKey, 5f);
  151. }
  152.  
  153. set
  154. {
  155. if (TargetSize != value)
  156. {
  157. EditorPrefs.SetFloat(kComposerTargetSizeKey, value);
  158. }
  159. }
  160. }
  161. }
  162.  
  163. private static bool ShowCoreSettings
  164. {
  165. get { return EditorPrefs.GetBool(kCoreSettingsFoldKey, false); }
  166. set
  167. {
  168. if (value != ShowCoreSettings)
  169. {
  170. EditorPrefs.SetBool(kCoreSettingsFoldKey, value);
  171. }
  172. }
  173. }
  174.  
  175. private static bool ShowComposerSettings
  176. {
  177. get { return EditorPrefs.GetBool(kComposerSettingsFoldKey, false); }
  178. set
  179. {
  180. if (value != ShowComposerSettings)
  181. {
  182. EditorPrefs.SetBool(kComposerSettingsFoldKey, value);
  183. }
  184. }
  185. }
  186.  
  187. private static Texture2D sCinemachineLogoTexture = null;
  188. internal static Texture2D CinemachineLogoTexture
  189. {
  190. get
  191. {
  192. if (sCinemachineLogoTexture == null)
  193. sCinemachineLogoTexture = Resources.Load<Texture2D>("cm_logo_sm");
  194. if (sCinemachineLogoTexture != null)
  195. sCinemachineLogoTexture.hideFlags = HideFlags.DontSaveInEditor;
  196. return sCinemachineLogoTexture;
  197. }
  198. }
  199.  
  200. private static Texture2D sCinemachineHeader = null;
  201. internal static Texture2D CinemachineHeader
  202. {
  203. get
  204. {
  205. if (sCinemachineHeader == null)
  206. sCinemachineHeader = Resources.Load<Texture2D>("cinemachine_header");
  207. ;
  208. if (sCinemachineHeader != null)
  209. sCinemachineHeader.hideFlags = HideFlags.DontSaveInEditor;
  210. return sCinemachineHeader;
  211. }
  212. }
  213.  
  214. private static readonly string kCoreSettingsFoldKey = "CNMCN_Core_Folded";
  215. private static readonly string kComposerSettingsFoldKey = "CNMCN_Composer_Folded";
  216.  
  217. internal static event Action AdditionalCategories = null;
  218.  
  219. static CinemachineSettings()
  220. {
  221. if (CinemachineLogoTexture != null)
  222. {
  223. EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
  224. }
  225. }
  226.  
  227. //private static readonly GUIContent sCoreShowHiddenObjectsToggle = new GUIContent("Show Hidden Objects", "If checked, Cinemachine hidden objects will be shown in the inspector. This might be necessary to repair broken script mappings when upgrading from a pre-release version");
  228. private static readonly GUIContent sCoreActiveGizmosColour = new GUIContent("Active Virtual Camera", "The colour for the active virtual camera's gizmos");
  229. private static readonly GUIContent sCoreInactiveGizmosColour = new GUIContent("Inactive Virtual Camera", "The colour for all inactive virtual camera gizmos");
  230.  
  231. private static readonly GUIContent sComposerOverlayOpacity = new GUIContent("Overlay Opacity", "The alpha of the composer's overlay when a virtual camera is selected with composer module enabled");
  232. private static readonly GUIContent sComposerHardBoundsOverlay = new GUIContent("Hard Bounds Overlay", "The colour of the composer overlay's hard bounds region");
  233. private static readonly GUIContent sComposerSoftBoundsOverlay = new GUIContent("Soft Bounds Overlay", "The colour of the composer overlay's soft bounds region");
  234. private static readonly GUIContent sComposerTargetOverlay = new GUIContent("Composer Target", "The colour of the composer overlay's target");
  235. private static readonly GUIContent sComposerTargetOverlayPixels = new GUIContent("Composer Target Size(px)", "The size of the composer overlay's target box in pixels");
  236.  
  237. private const string kCinemachineHeaderPath = "cinemachine_header.tif";
  238. private const string kCinemachineDocURL = @"http://www.cinemachineimagery.com/documentation/";
  239.  
  240. private static Vector2 sScrollPosition = Vector2.zero;
  241.  
  242. [PreferenceItem("Cinemachine")]
  243. private static void OnGUI()
  244. {
  245. if (CinemachineHeader != null)
  246. {
  247. const float kWidth = 350f;
  248. float aspectRatio = (float)CinemachineHeader.height / (float)CinemachineHeader.width;
  249. GUILayout.BeginScrollView(Vector2.zero, false, false, GUILayout.Width(kWidth), GUILayout.Height(kWidth * aspectRatio));
  250. Rect texRect = new Rect(0f, 0f, kWidth, kWidth * aspectRatio);
  251.  
  252. GUILayout.BeginArea(texRect);
  253. GUI.DrawTexture(texRect, CinemachineHeader, ScaleMode.ScaleToFit);
  254. GUILayout.EndArea();
  255.  
  256. GUILayout.EndScrollView();
  257. }
  258.  
  259. sScrollPosition = GUILayout.BeginScrollView(sScrollPosition);
  260.  
  261. //CinemachineCore.sShowHiddenObjects
  262. // = EditorGUILayout.Toggle("Show Hidden Objects", CinemachineCore.sShowHiddenObjects);
  263.  
  264. ShowCoreSettings = EditorGUILayout.Foldout(ShowCoreSettings, "Runtime Settings");
  265. if (ShowCoreSettings)
  266. {
  267. EditorGUI.indentLevel++;
  268. EditorGUI.BeginChangeCheck();
  269. EditorGUILayout.BeginHorizontal();
  270. EditorGUI.BeginChangeCheck();
  271. Color newActiveGizmoColour = EditorGUILayout.ColorField(sCoreActiveGizmosColour, CinemachineCoreSettings.ActiveGizmoColour);
  272.  
  273. if (EditorGUI.EndChangeCheck())
  274. {
  275. CinemachineCoreSettings.ActiveGizmoColour = newActiveGizmoColour;
  276. UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
  277. }
  278.  
  279. if (GUILayout.Button("Reset"))
  280. {
  281. CinemachineCoreSettings.ActiveGizmoColour = CinemachineCoreSettings.kDefaultActiveColour;
  282. }
  283. EditorGUILayout.EndHorizontal();
  284.  
  285. EditorGUILayout.BeginHorizontal();
  286. EditorGUI.BeginChangeCheck();
  287. Color newInactiveGizmoColour = EditorGUILayout.ColorField(sCoreInactiveGizmosColour, CinemachineCoreSettings.InactiveGizmoColour);
  288.  
  289. if (EditorGUI.EndChangeCheck())
  290. {
  291. CinemachineCoreSettings.InactiveGizmoColour = newInactiveGizmoColour;
  292. UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
  293. }
  294.  
  295. if (GUILayout.Button("Reset"))
  296. {
  297. CinemachineCoreSettings.InactiveGizmoColour = CinemachineCoreSettings.kDefaultInactiveColour;
  298. }
  299. EditorGUILayout.EndHorizontal();
  300. EditorGUI.indentLevel--;
  301. }
  302.  
  303. ShowComposerSettings = EditorGUILayout.Foldout(ShowComposerSettings, "Composer Settings");
  304. if (ShowComposerSettings)
  305. {
  306. EditorGUI.indentLevel++;
  307. EditorGUILayout.BeginHorizontal();
  308. EditorGUI.BeginChangeCheck();
  309.  
  310. float overlayOpacity = EditorGUILayout.Slider(sComposerOverlayOpacity, ComposerSettings.OverlayOpacity, 0f, 1f);
  311.  
  312. if (EditorGUI.EndChangeCheck())
  313. {
  314. ComposerSettings.OverlayOpacity = overlayOpacity;
  315. }
  316.  
  317. if (GUILayout.Button("Reset"))
  318. {
  319. ComposerSettings.OverlayOpacity = ComposerSettings.kDefaultOverlayOpacity;
  320. }
  321. EditorGUILayout.EndHorizontal();
  322.  
  323. EditorGUILayout.BeginHorizontal();
  324. EditorGUI.BeginChangeCheck();
  325. Color newHardEdgeColor = EditorGUILayout.ColorField(sComposerHardBoundsOverlay, ComposerSettings.HardBoundsOverlayColour);
  326.  
  327. if (EditorGUI.EndChangeCheck())
  328. {
  329. ComposerSettings.HardBoundsOverlayColour = newHardEdgeColor;
  330. }
  331.  
  332. if (GUILayout.Button("Reset"))
  333. {
  334. ComposerSettings.HardBoundsOverlayColour = ComposerSettings.kDefaultHardBoundsColour;
  335. }
  336. EditorGUILayout.EndHorizontal();
  337.  
  338. EditorGUILayout.BeginHorizontal();
  339. EditorGUI.BeginChangeCheck();
  340. Color newSoftEdgeColor = EditorGUILayout.ColorField(sComposerSoftBoundsOverlay, ComposerSettings.SoftBoundsOverlayColour);
  341.  
  342. if (EditorGUI.EndChangeCheck())
  343. {
  344. ComposerSettings.SoftBoundsOverlayColour = newSoftEdgeColor;
  345. }
  346.  
  347. if (GUILayout.Button("Reset"))
  348. {
  349. ComposerSettings.SoftBoundsOverlayColour = ComposerSettings.kDefaultSoftBoundsColour;
  350. }
  351. EditorGUILayout.EndHorizontal();
  352.  
  353. EditorGUILayout.BeginHorizontal();
  354. EditorGUI.BeginChangeCheck();
  355. Color newTargetColour = EditorGUILayout.ColorField(sComposerTargetOverlay, ComposerSettings.TargetColour);
  356.  
  357. if (EditorGUI.EndChangeCheck())
  358. {
  359. ComposerSettings.TargetColour = newTargetColour;
  360. }
  361.  
  362. if (GUILayout.Button("Reset"))
  363. {
  364. ComposerSettings.TargetColour = ComposerSettings.kDefaultTargetColour;
  365. }
  366. EditorGUILayout.EndHorizontal();
  367.  
  368. EditorGUI.BeginChangeCheck();
  369. float targetSide = EditorGUILayout.FloatField(sComposerTargetOverlayPixels, ComposerSettings.TargetSize);
  370.  
  371. if (EditorGUI.EndChangeCheck())
  372. {
  373. ComposerSettings.TargetSize = targetSide;
  374. }
  375. EditorGUI.indentLevel--;
  376. }
  377.  
  378. if (AdditionalCategories != null)
  379. {
  380. AdditionalCategories();
  381. }
  382.  
  383. GUILayout.EndScrollView();
  384.  
  385. //if (GUILayout.Button("Open Documentation"))
  386. //{
  387. // Application.OpenURL(kCinemachineDocURL);
  388. //}
  389. }
  390.  
  391. private static void OnHierarchyGUI(int instanceID, Rect selectionRect)
  392. {
  393. GameObject instance = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
  394. if (instance == null)
  395. {
  396. // Object in process of being deleted?
  397. return;
  398. }
  399.  
  400. if (instance.GetComponent<CinemachineBrain>() != null)
  401. {
  402. Rect texRect = new Rect(selectionRect.xMax - selectionRect.height, selectionRect.yMin, selectionRect.height, selectionRect.height);
  403. GUI.DrawTexture(texRect, CinemachineLogoTexture, ScaleMode.ScaleAndCrop);
  404. }
  405. }
  406.  
  407. internal static Color UnpackColour(string str)
  408. {
  409. if (!string.IsNullOrEmpty(str))
  410. {
  411. byte[] bytes = Base64Decode(str);
  412.  
  413. if ((bytes != null) && bytes.Length == 16)
  414. {
  415. float r = BitConverter.ToSingle(bytes, 0);
  416. float g = BitConverter.ToSingle(bytes, 4);
  417. float b = BitConverter.ToSingle(bytes, 8);
  418. float a = BitConverter.ToSingle(bytes, 12);
  419.  
  420. return new Color(r, g, b, a);
  421. }
  422. }
  423.  
  424. return Color.white;
  425. }
  426.  
  427. internal static string PackColor(Color col)
  428. {
  429. byte[] bytes = new byte[16];
  430.  
  431. byte[] rBytes = BitConverter.GetBytes(col.r);
  432. byte[] gBytes = BitConverter.GetBytes(col.g);
  433. byte[] bBytes = BitConverter.GetBytes(col.b);
  434. byte[] aBytes = BitConverter.GetBytes(col.a);
  435.  
  436. Buffer.BlockCopy(rBytes, 0, bytes, 0, 4);
  437. Buffer.BlockCopy(gBytes, 0, bytes, 4, 4);
  438. Buffer.BlockCopy(bBytes, 0, bytes, 8, 4);
  439. Buffer.BlockCopy(aBytes, 0, bytes, 12, 4);
  440.  
  441. return Base64Encode(bytes);
  442. }
  443.  
  444. private static string Base64Encode(byte[] data)
  445. {
  446. return Convert.ToBase64String(data);
  447. }
  448.  
  449. private static byte[] Base64Decode(string base64EncodedData)
  450. {
  451. return Convert.FromBase64String(base64EncodedData);
  452. }
  453. }
  454. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement