Time.timeScale = 0 pauses all animation in my game, which appears to be the quick an easy way to stop everything while, for instance, the player examines his inventory window.
However, this doesn’t allow me to use animations in this UI menu, is there a way to keep some animations working, but not others?
You can wrap the built-in time class in your own class, of which you create multiple instances. The instances are used for their very own contexts.
You can pass those instances around, and the various contexts in your game can use a different, or a temporary, time service.
That is, you no longer use Time.deltaTime directly, but an instance of your time service class which is controlled individually and provides Time.deltaTime * it’s current factor.
This allows to speed up / slow down / pause individual objects, groups of objects or whatever you like. Note that some systems of the engine use the built-in time internally. You’d then need to stop animations on your own or add additional timing variables to achieve the effect of modifying the global time scale.
There’re also assets on the store that you can use if you don’t want to re-invent the wheel. Not sure if the engine’s newer versions have support built-in for that. It’s definitely something that is required quite often.
4 Likes
Suddoha:
You can wrap the built-in time class in your own class, of which you create multiple instances. The instances are used for their very own contexts.
You can pass those instances around, and the various contexts in your game can use a different, or a temporary, time service.
That is, you no longer use Time.deltaTime directly, but an instance of your time service class which is controlled individually and provides Time.deltaTime * it’s current factor.
This allows to speed up / slow down / pause individual objects, groups of objects or whatever you like. Note that some systems of the engine use the built-in time internally. You’d then need to stop animations on your own or add additional timing variables to achieve the effect of modifying the global time scale.
There’re also assets on the store that you can use if you don’t want to re-invent the wheel. Not sure if the engine’s newer versions have support built-in for that. It’s definitely something that is required quite often.
Oh wow i didnt know you can do that, i gotta experiment with this.
I hope the docs are actually written for that functionallity, heh
You have Time.deltaTime and Time.unscaledDeltaTime. One is effected by timeScale, the other not:
If you just have 2 modes… “effected by pause” and “unaffected by pause”, this is all you’ll need.
If you need more than just these 2, as @Suddoha mentioned, you can create your own time wrapper. Here is mine:
First I have an ‘ITimeSupplier’ interface that defines the functionality (and allows for adhoc implementations of very custom time suppliers):
Then I have SPTime, which is where I house an access point to all the general time suppliers (normal, unscaled, fixed, etc):
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using com.spacepuppy.Utils;
namespace com.spacepuppy
{
/// <summary>
/// A static entry point to the various ITimeSuppliers in existance. An ITimeSupplier gives object identity
/// to the varous kinds of time out there: normal (UnityEngine.Time.time), real (UnityEngine.Time.unscaledTime),
/// smooth (Time.smoothDeltaTime), custom (no unity parrallel).
///
/// With the objects in hand you can than swap out what time is used when updating objects.
///
/// For example the com.spacepuppy.Tween namespace containers tweeners that can update on any of the existing
/// times in unity. These objects can be passed in to allow updating at any one of those times. Lets say for
/// instance you've set the time scale of Normal time to 0 (to pause the game), but you still need the menu to
/// tween and update appropriately. Well, you'd use the 'RealTime' or a 'Custom' time object to update the
This file has been truncated. show original
And CustomTimeSupplier which is a general purpose time supplier which allows creating tagged time suppliers of various types:
using UnityEngine;
using System;
using System.Collections.Generic;
namespace com.spacepuppy
{
/// <summary>
/// A TimeSupplier that allows for individual scaling. You may have to scale the world time to 0 for pause
/// or other various effects, but you still want time to tick normally for other aspects like the menu or
/// something. BUT, lets say you want to be able to scale that time as well for various effects, independent
/// of the world time. By using a custom TimeSupplier you can scale that time independent of the world time.
///
/// Furthermore you can stack time scales, just like described in com.spacepuppy.SPTime.
///
/// Allows for approximately 29,247 years of simulation, twice that if you include negative time, while
/// maintaining at minimum 3 digits of fractional precision for seconds (millisecond precision) when at the
/// extents of its range.
/// </summary>
public class CustomTimeSupplier : ICustomTimeSupplier, IScalableTimeSupplier, System.IDisposable
This file has been truncated. show original
Furthermore, the ‘SPTime’ acts as a token for time suppliers. Using a PropertyDrawer for SPTime like this one:
using UnityEngine;
using UnityEditor;
using System.Linq;
using com.spacepuppy;
using com.spacepuppy.Project;
using com.spacepuppy.Collections;
namespace com.spacepuppyeditor.Core
{
[CustomPropertyDrawer(typeof(SPTime))]
public class SPTimePropertyDrawer : PropertyDrawer
{
public const string PROP_TIMESUPPLIERTYPE = "_timeSupplierType";
public const string PROP_TIMESUPPLIERNAME = "_timeSupplierName";
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
This file has been truncated. show original
I get the ability to do this:
With a bit of work, there’s a lot you can do with giving Time object identity like this.
4 Likes
If you are using an Animator to animate your UI there is a setting that uses unscaled time.
4 Likes
Thanks for the very interesting replies.