Search Unity

S*** Code, Need Comment !

Discussion in 'Timeline' started by wanzhang, Mar 31, 2020.

  1. wanzhang

    wanzhang

    Joined:
    May 19, 2015
    Posts:
    8
    Code From ParticleControlPlayable.cs

    Code (CSharp):
    1.         public override void PrepareFrame(Playable playable, FrameData data)
    2.         {
    3.             if (particleSystem == null || !particleSystem.gameObject.activeInHierarchy)
    4.                 return;
    5.  
    6.             float localTime = (float)playable.GetTime();
    7.             bool shouldUpdate = Mathf.Approximately(m_LastTime, kUnsetTime) ||
    8.                 !Mathf.Approximately(m_LastTime, localTime);
    9.             if (shouldUpdate)
    10.             {
    11.                 float epsilon = Time.fixedDeltaTime * 0.5f;
    12.                 float simTime = localTime;
    13.                 float expectedDelta = simTime - m_LastTime;
    14.  
    15.                 //  The first iteration includes the start delay. Evaluate(particleSystem.randomSeed) is how the particle system generates the random value internally.
    16.                 float startDelay = particleSystem.main.startDelay.Evaluate(particleSystem.randomSeed);
    17.                 float particleSystemDurationLoop0 = particleSystem.main.duration + startDelay;
    18.  
    19.                 // The particle system time does not include the start delay so we need to remove this for our own system time.
    20.                 float expectedSystemTime = simTime > particleSystemDurationLoop0 ? m_SystemTime : m_SystemTime - startDelay;
    21.  
    22.                 // conditions for restart
    23.                 bool restart = (simTime < m_LastTime) || // time went backwards
    24.                     (simTime < epsilon) || // time is set to 0
    25.                     Mathf.Approximately(m_LastTime, kUnsetTime) || // object disabled
    26.                     (expectedDelta > particleSystem.main.duration) || // large jump (bug workaround)
    27.                     !(Mathf.Abs(expectedSystemTime - particleSystem.time) < Time.maximumParticleDeltaTime); // particle system isn't where we left it
    28.                 if (restart)
    29.                 {
    30.                     // work around for a bug where simulate(simTime, true, true) doesn't work on loops
    31.                     particleSystem.Simulate(0, true, true);
    32.                     particleSystem.Simulate(simTime, true, false);
    33.                     m_SystemTime = simTime;
    34.                 }
    35.                 else
    36.                 {
    37.                     // ps.time will wrap, so we need to account for that in computing delta time
    38.                     float particleSystemDuration = simTime > particleSystemDurationLoop0 ? particleSystem.main.duration : particleSystemDurationLoop0;
    39.                     float fracTime = simTime % particleSystemDuration;
    40.                     float deltaTime = fracTime - m_SystemTime;
    41.  
    42.                     if (deltaTime < -epsilon) // detect wrapping of ps.time
    43.                         deltaTime = fracTime + particleSystemDurationLoop0 - m_SystemTime;
    44.  
    45.                     particleSystem.Simulate(deltaTime, true, false);
    46.                     m_SystemTime += deltaTime;
    47.                 }
    48.  
    49.                 m_LastTime = localTime;
    50.             }
    51.         }
     
  2. wanzhang

    wanzhang

    Joined:
    May 19, 2015
    Posts:
    8
    the code really sucks
     
  3. wanzhang

    wanzhang

    Joined:
    May 19, 2015
    Posts:
    8
    i need some guy in unity, help me explain what's this line around mean
    Code (CSharp):
    1.                     // ps.time will wrap, so we need to account for that in computing delta time
    2.                     float particleSystemDuration = simTime > particleSystemDurationLoop0 ? particleSystem.main.duration : particleSystemDurationLoop0;
    3.                     float fracTime = simTime % particleSystemDuration;
     
  4. wanzhang

    wanzhang

    Joined:
    May 19, 2015
    Posts:
    8
    the fracTime.
     
  5. wanzhang

    wanzhang

    Joined:
    May 19, 2015
    Posts:
    8
    finally, i know what's going on. the code is wrong. does unity has code review ? i'm suppringly unity can write such bad code! and no one can figure it out ?