Author Topic: RealTime problem  (Read 2803 times)

Capyvara

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
RealTime problem
« on: June 28, 2014, 08:07:13 PM »
Hi,

Yesterday I faced one Unity bug while trying to play a UITweener on the OnEnable() of one of my scripts, for some reason it delayed a several seconds before starting, I found out it was related to this:

http://forum.unity3d.com/threads/realtimesincestartup-is-not-0-in-first-awake-call.205773/

Basically the Time.realtimeSinceStartup isn't updated yet in the Awake() / OnEnable() calls when the scene is entering play mode in editor.

In order to prevent this I've made some workarounds in RealTime class, not sure if it's the best option (also not well tested):

  1.         static void Spawn ()
  2.         {
  3.                 GameObject go = new GameObject("_RealTime");
  4.                 DontDestroyOnLoad(go);
  5.                 mInst = go.AddComponent<RealTime>();
  6.                 mInst.mRealTime = Time.realtimeSinceStartup;
  7.                 mInst.mInitialRealTime = mInst.mRealTime;
  8.         }
  9.  
  10.         void Update ()
  11.         {
  12.                 float rt = Time.realtimeSinceStartup;
  13.  
  14.                 if (rt < mRealTime) rt += mInitialRealTime;
  15.  
  16.                 mRealDelta = Mathf.Clamp01(rt - mRealTime);
  17.                 mRealTime = rt;
  18.         }
  19.  

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: RealTime problem
« Reply #1 on: June 29, 2014, 04:11:55 PM »

Capyvara

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: RealTime problem
« Reply #2 on: June 29, 2014, 06:58:44 PM »
Arguably it should just use http://docs.unity3d.com/ScriptReference/Time-unscaledTime.html now.

The problem is that it's Unity 4.5 only, and NGUI supports 4.3+

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: RealTime problem
« Reply #3 on: June 29, 2014, 10:45:45 PM »
Ah, I didn't know it was finally added. I remember mentioning that it was needed back in 2012 when I was in Unity. Good news. I've adjusted the RealTime class accordingly.