3
\$\begingroup\$

I need to add a simple slider like scale slider in Game-view to change the timescale because I usually test my games by changing timescale.

H

\$\endgroup\$
3
\$\begingroup\$

Simple answer would be no, you can't. This is done in this internal class (In the 'DoToolbarGUI' private method to be precise), meaning you can't just overwrite it easily. See also this question if you still want to try to do that.

The best alternative that I can come up with, is a custom editor window that you can dock somewhere on a fixed place below the Game window or hierarchy window, so you can alter this setting (And perhaps other settings) from there like so:

public class ExampleWindow : EditorWindow
{
    public ExampleWindow()
    {
        minSize = new Vector2(100, 16);
    }

    [MenuItem("Window/ExampleWindow")]
    public static void ShowWindow()
    {
        GetWindow(typeof(ExampleWindow));
    }

    void OnGUI()
    {
        Time.timeScale = EditorGUILayout.Slider("Speed", Time.timeScale, 1, 10, GUILayout.ExpandWidth(false));
    }
}

If the window above is docked below the Game window, it would look like this: Example window


EDIT

By looking a bit more at my custom window, I found a way to get it to look exactly like you want it to, it is however a bit cheating, as I'm just setting the position of my custom window on top of that bar basically...

public class ExampleWindow : EditorWindow
{
    private static ExampleWindow _instance;

    public ExampleWindow()
    {
        _instance = this;
        minSize = new Vector2(100, 16);
    }

    [MenuItem("Window/SettingsWindow")]
    public static void ShowWindow()
    {
        if (_instance == null)
        {
            ExampleWindow window = ScriptableObject.CreateInstance<ExampleWindow>();
            window.ShowPopup();
        }
        else
        {
            _instance.Close(); // Close existing window instead of opening it
        }
    }

    private void Update()
    {
        System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");

        var window = EditorWindow.focusedWindow;
        if (window?.GetType() == T)
        {
            Rect gameRect = window.position;

            // Set our position to the free space of the toolbar, you might need to alter this to be more exact as I just made some quick hardcoded guess here.
            float leftOffset = 500;
            float rightOffset = 315;
            float width = gameRect.width - leftOffset - rightOffset;

            position = new Rect(gameRect.x + leftOffset, gameRect.y + 19, width, 16);
        }

        //Close(); // Uncomment to force close all open windows (Don't forget to re-comment after they are closed)
    }

    void OnGUI()
    {
        GUILayout.BeginHorizontal(EditorStyles.toolbar);
        Time.timeScale = EditorGUILayout.Slider("Speed", Time.timeScale, 1, 10);
        GUILayout.EndHorizontal();
    }
}

After you initially open this window, click the game view to automatically move it into the correct place. Same applies when you resize the game view. The reason you need to do this is because I can't find a way to get a reference to the GameView without opening it. Meaning I can only get it by checking if the focussed window is the gameview.

Result looks like this for me: enter image description here

\$\endgroup\$
  • \$\begingroup\$ I know how to make custom windows, I like to add this slider to unity's game view.is there a way to inherit from that class as a new window and add this slider? \$\endgroup\$ – Seyed Morteza Kamali Oct 17 at 7:56
  • \$\begingroup\$ No, as mentioned, this class is internal, the method that you would want to overwrite is not virtual and private aswell. Follow the link to that other question if you still wan't to try and overwrite it. I haven't tried to do that, as it is complicated and a lot of work for something that is not worth the time investement in my opinion. Especially as it could break in a new version of Unity. I did however try something else by exploiting my custom window, I'll update my original answer. \$\endgroup\$ – troien Oct 17 at 8:19
  • \$\begingroup\$ ok, no problem I hope unity will be open source. I used window here thanks for spending time to answer me \$\endgroup\$ – Seyed Morteza Kamali Oct 17 at 8:40
  • \$\begingroup\$ Added the other way to get it to look like you want to, there is probably still some work to do to get it to work exactly like how you want it to, as I haven't tested it that much. But this should give you an idea on how you can get it to work. Note however that to get it to look like this, I use the popup display, which doesn't have a way to close the window, so you'd have to do that yourself via code :D \$\endgroup\$ – troien Oct 17 at 9:04
  • \$\begingroup\$ thanks troien, this is really cool! but have many problems :) image \$\endgroup\$ – Seyed Morteza Kamali Oct 17 at 12:27
1
\$\begingroup\$

I made a custom toolbar. I uploaded it in my github

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ @troien I make this possible :D \$\endgroup\$ – Seyed Morteza Kamali yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.