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.

113 lines
2.9 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. [MenuItem("AddOn/TimeScale")]
  12. static void Init()
  13. {
  14. // Get existing open window or if none, make a new one:
  15. TimeScaleWindow window = (TimeScaleWindow)EditorWindow.GetWindow(typeof(TimeScaleWindow));
  16. window.Show();
  17. }
  18. void OnGUI()
  19. {
  20. GUILayout.Label("Base Settings", EditorStyles.boldLabel);
  21. GUILayout.BeginHorizontal();
  22. if (GUILayout.Button("<<")) NextPower(-1);
  23. if (GUILayout.Button("<")) weightedTimeScale--;
  24. if (GUILayout.Button(">")) weightedTimeScale++;
  25. if (GUILayout.Button(">>")) NextPower(1);
  26. GUILayout.EndHorizontal();
  27. weightedTimeScale = (int)GUILayout.HorizontalSlider(weightedTimeScale, -98f, 100f);
  28. weightedTimeScale = Mathf.Clamp(weightedTimeScale,-98,100);
  29. GUILayout.BeginHorizontal();
  30. if (GUILayout.Button("Pause")) isPaused = true;
  31. var centeredStyle = GUI.skin.GetStyle("Label");
  32. centeredStyle.alignment = TextAnchor.UpperCenter;
  33. centeredStyle.normal.textColor = Color.black;
  34. if (isPaused)
  35. centeredStyle.normal.textColor = Color.red;
  36. GUILayout.Label(DisplayString(weightedTimeScale), centeredStyle);
  37. if (GUILayout.Button("Play")) isPaused = false;
  38. GUILayout.EndHorizontal();
  39. Time.timeScale = ConvertWeightedToRaw(weightedTimeScale);
  40. }
  41. private float ConvertWeightedToRaw(float input)
  42. {
  43. if (isPaused)
  44. return 0;
  45. if (input >= 1)
  46. return Mathf.Lerp(0, 100, input / 100);
  47. else
  48. return 1 / (-input + 2);
  49. }
  50. private void NextPower(int dir)
  51. {
  52. int sign = (int)Mathf.Sign(weightedTimeScale - 1);
  53. int prevValue = (sign >= 0) ? Mathf.Abs(weightedTimeScale) : Mathf.Abs(weightedTimeScale) + 2;
  54. int nextValue = Mathf.NextPowerOfTwo(prevValue);
  55. if (weightedTimeScale == 0)
  56. {
  57. weightedTimeScale = 1;
  58. if (dir < 0)
  59. weightedTimeScale = -2;
  60. return;
  61. }
  62. if (Mathf.Sign(dir) != Mathf.Sign(weightedTimeScale))
  63. nextValue /= 2;
  64. else if (prevValue == nextValue)
  65. nextValue = Mathf.NextPowerOfTwo(prevValue + 1);
  66. if (sign <= 0)
  67. nextValue -= 2;
  68. weightedTimeScale = nextValue * sign;
  69. }
  70. private string DisplayString(float input)
  71. {
  72. string retVal = "";
  73. if (isPaused)
  74. retVal += "(Paused) ";
  75. if (input > 0)
  76. retVal += input.ToString();
  77. else
  78. retVal += "1/" + (-input + 2);
  79. return retVal;
  80. }
  81. }