Search Unity

Adding custom variables like the built in shader variables.

Discussion in 'Shaders' started by thorham3011, May 24, 2019.

  1. thorham3011

    thorham3011

    Joined:
    Sep 7, 2017
    Posts:
    5
    Is there any way to add shader variables such as _Time to Unity without having to use monobehavior scripts? The problem with those is that they don't work very conveniently in the editor (Time.deltaTime being too fast for example).
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,744
    No.

    Well, Time.deltaTime is going to be especially wonky in the editor.

    If you have a script that uses ExecuteInEditMode, it'll call the Update function of your script, but it's not consistent. The editor isn't trying to run the game in real time, so updates will be sporadic based on when you're doing stuff in the inspector or otherwise manipulating objects ... or clicking in the game view and wiggling your mouse (no really). Time.deltaTime may or may not be getting updated at the same rate as your script's Update is getting called, so it may have huge spikes, or be much smaller than you expect.

    If you want something to update more consistently in the editor, you'd have to use the EditorApplication.update delegate. But that's not tied directly to any kind of game rendering rate, so may be running at a few hundred times per second. Plus there's no delta time associated with it, only a real time value, EditorApplication.timeSinceStartup. You could track the delta yourself, but when the editor is updating at a high rate, I believe that time is also incremented, so you'd potentially have really tiny deltas compared to what you're actually rendering at.

    Short version, when it comes to non-play mode _Time values, they're going to be a little screwy no matter what you do. The best option, if you want to use a custom time property, would be to use EditorApplication.update & timeSinceStartup, and a fixed delta time when outside of play mode. Otherwise updating using the default Update() and drive your custom time using the appropriate Time values.
     
  3. thorham3011

    thorham3011

    Joined:
    Sep 7, 2017
    Posts:
    5
    Nice explanation, thanks.

    I was thinking about using Stopwatch.GetTimestamp() in OnObjectRender to calculate all the required time values for editor mode.Seemed like the best way seeing how OnObjectRender is tied to rendering actual frames.