You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.2 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.UI;
  6. public class TimeScaleWindow : EditorWindow
  7. {
  8. // Add menu named "My Window" to the Window menu
  9. private int weightedTimeScale = 1;
  10. private bool isPaused = false;
  11. private Color defaultColor;
  12. [MenuItem("Tools/TimeScale")]
  13. static void Init()
  14. {
  15. // Get existing open window or if none, make a new one:
  16. TimeScaleWindow window = (TimeScaleWindow)EditorWindow.GetWindow(typeof(TimeScaleWindow),false,"Time Scale");
  17. Rect rect = window.position;
  18. rect.width = 300;
  19. rect.height = EditorGUIUtility.singleLineHeight * 4;
  20. window.minSize = new Vector2(rect.width, rect.height);
  21. window.maxSize = new Vector2(rect.width, rect.height);
  22. window.position = rect;
  23. window.Show();
  24. }
  25. void OnGUI()
  26. {
  27. GUILayout.BeginHorizontal();
  28. if (GUILayout.Button("<<")) NextPower(-1);
  29. if (GUILayout.Button("<")) weightedTimeScale--;
  30. if (GUILayout.Button(">")) weightedTimeScale++;
  31. if (GUILayout.Button(">>")) NextPower(1);
  32. GUILayout.EndHorizontal();
  33. weightedTimeScale = (int)GUILayout.HorizontalSlider(weightedTimeScale, -98f, 100f);
  34. weightedTimeScale = Mathf.Clamp(weightedTimeScale,-98,100);
  35. GUILayout.Space(EditorGUIUtility.singleLineHeight);
  36. GUILayout.BeginHorizontal();
  37. if (GUILayout.Button("Pause")) isPaused = true;
  38. var centeredStyle = new GUIStyle(EditorStyles.label);
  39. centeredStyle.alignment = TextAnchor.UpperCenter;
  40. centeredStyle.normal.textColor = EditorStyles.label.normal.textColor;
  41. if (isPaused)
  42. centeredStyle.normal.textColor = Color.red;
  43. GUILayout.Label(DisplayString(weightedTimeScale), centeredStyle);
  44. if (GUILayout.Button("Play")) isPaused = false;
  45. GUILayout.EndHorizontal();
  46. Time.timeScale = ConvertWeightedToRaw(weightedTimeScale);
  47. }
  48. private float ConvertWeightedToRaw(float input)
  49. {
  50. if (isPaused)
  51. return 0;
  52. if (input >= 1)
  53. return Mathf.Lerp(0, 100, input / 100);
  54. else
  55. return 1 / (-input + 2);
  56. }
  57. private void NextPower(int dir)
  58. {
  59. int sign = (int)Mathf.Sign(weightedTimeScale - 1);
  60. int prevValue = (sign >= 0) ? Mathf.Abs(weightedTimeScale) : Mathf.Abs(weightedTimeScale) + 2;
  61. int nextValue = Mathf.NextPowerOfTwo(prevValue);
  62. if (weightedTimeScale == 0)
  63. {
  64. weightedTimeScale = 1;
  65. if (dir < 0)
  66. weightedTimeScale = -2;
  67. return;
  68. }
  69. if (Mathf.Sign(dir) != Mathf.Sign(weightedTimeScale))
  70. nextValue /= 2;
  71. else if (prevValue == nextValue)
  72. nextValue = Mathf.NextPowerOfTwo(prevValue + 1);
  73. if (sign <= 0)
  74. nextValue -= 2;
  75. weightedTimeScale = nextValue * sign;
  76. }
  77. private string DisplayString(float input)
  78. {
  79. string retVal = "";
  80. if (isPaused)
  81. retVal += "(Paused) ";
  82. if (input > 0)
  83. retVal += input.ToString();
  84. else
  85. retVal += "1/" + (-input + 2);
  86. return retVal;
  87. }
  88. }