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.

94 lines
2.4 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 float weightedTimeScale = 0;
  10. [MenuItem("AddOn/TimeScale")]
  11. static void Init()
  12. {
  13. // Get existing open window or if none, make a new one:
  14. TimeScaleWindow window = (TimeScaleWindow)EditorWindow.GetWindow(typeof(TimeScaleWindow));
  15. window.Show();
  16. }
  17. void OnGUI()
  18. {
  19. GUILayout.Label("Base Settings", EditorStyles.boldLabel);
  20. GUILayout.BeginHorizontal();
  21. if (GUILayout.Button("<<")) NextPower(-1);
  22. if (GUILayout.Button("<")) weightedTimeScale--;
  23. if (GUILayout.Button(">")) weightedTimeScale++;
  24. if (GUILayout.Button(">>")) NextPower(1);
  25. GUILayout.EndHorizontal();
  26. weightedTimeScale = GUILayout.HorizontalSlider(weightedTimeScale, -100f, 100f);
  27. Time.timeScale = ConvertWeightedToRaw(weightedTimeScale);
  28. GUILayout.BeginHorizontal();
  29. if (GUILayout.Button("Pause")) weightedTimeScale = -100;
  30. var centeredStyle = GUI.skin.GetStyle("Label");
  31. centeredStyle.alignment = TextAnchor.UpperCenter;
  32. GUILayout.Label(DisplayString(Time.timeScale), centeredStyle);
  33. if (GUILayout.Button("Play")) Time.timeScale = 0;
  34. GUILayout.EndHorizontal();
  35. }
  36. private float ConvertWeightedToRaw(float input)
  37. {
  38. if (input > 0)
  39. return Mathf.Lerp(1, 100, input / 100);
  40. else
  41. return Mathf.Lerp(1, 0, -input / 100);
  42. }
  43. private void NextPower(int dir)
  44. {
  45. float sign = Mathf.Sign(weightedTimeScale);
  46. Debug.Log("Sign: " + sign);
  47. float value = Mathf.Abs(weightedTimeScale + dir);
  48. Debug.Log("Value: " + value);
  49. value = Mathf.NextPowerOfTwo((int)(value));
  50. if (Mathf.Sign(dir) != Mathf.Sign(weightedTimeScale))
  51. value /= 2;
  52. Debug.Log("retVal: " + value + " * " + sign);
  53. weightedTimeScale = value * sign;
  54. }
  55. private void SetSpeed(float newSpeed)
  56. {
  57. Time.timeScale = newSpeed;
  58. }
  59. private string DisplayString(float input)
  60. {
  61. return input.ToString() + " (" + weightedTimeScale + ")";
  62. if (input > 1)
  63. return Mathf.Floor(input).ToString();
  64. else
  65. return ("1/" + Mathf.Floor(-weightedTimeScale));
  66. }
  67. }