Advertisement
polyfied

Playground Synced Simulate

May 18th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. Synced Simulate will let you time-step a Particle Playground system in predetermined intervals. This is a rough workaround and will alter the global time of Particle Playground within the scene due to access level of members which otherwise are controlled internally within a Playground system.
  2.  
  3. Find the package here: http://polyfied.com/development/unity/playground/Playground_SyncedSimulate.unitypackage
  4.  
  5.  
  6. /////////////////////////////////////////////////////////////////////////////////////////////////
  7. // Playground Synced Simulate/SyncedSimulate.cs
  8. /////////////////////////////////////////////////////////////////////////////////////////////////
  9.  
  10. using UnityEngine;
  11. using System.Collections;
  12. using ParticlePlayground;
  13.  
  14. /// <summary>
  15. /// Synced Simulate is a rough hack to make a Particle Playground system be able to step forward in time intervals. It uses PlaygroundC.globalTime to reconfigure the current time, so it will affect other particle systems in your scene.
  16. /// </summary>
  17. [ExecuteInEditMode()]
  18. public class SyncedSimulate : MonoBehaviour
  19. {
  20.     public bool pauseOnStart = false;           // Should the simulation be paused in the beginning?
  21.     public bool pauseOnSimulation = false;      // Should the simulation pause upon calling Simulate()?
  22.     public float stepTime = 1f;                 // The time to step forward each Simulate() call
  23.     public int stepResolution = 100;            // The resolution to calculate forces, the more the heavier on CPU
  24.     PlaygroundParticlesC _particles;            // Cached particle system reference
  25.  
  26.     void OnEnable ()
  27.     {
  28.         _particles = GetComponent<PlaygroundParticlesC>();
  29.         _particles.applyDeltaOnRebirth = true;
  30.  
  31.         if (pauseOnStart)
  32.             _particles.particleTimescale = 0;
  33.     }
  34.  
  35.     /// <summary>
  36.     /// Simulate on main-thread to the specified time. Resolution determines how exact the final result should be in terms of forces.
  37.     /// </summary>
  38.     /// <param name="time">Time of simulation.</param>
  39.     /// <param name="resolution">The amount of calculated steps.</param>
  40.     public void Simulate (float time, int resolution)
  41.     {
  42.         _particles.particleTimescale = 1f;
  43.  
  44.         if (resolution < 1)
  45.             resolution = 1;
  46.  
  47.         float currentTime = PlaygroundC.globalTime;
  48.  
  49.         for (int i = 0; i<resolution; i++)
  50.         {
  51.             PlaygroundC.globalTime = currentTime+(time*((i*1f)/(resolution*1f)));
  52.             PlaygroundParticlesC.ThreadedCalculations(_particles);
  53.         }
  54.  
  55.         if (pauseOnSimulation)
  56.             _particles.particleTimescale = 0;
  57.     }
  58. }
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////////////////////////
  62. // Playground Synced Simulate/Editor/SyncedSimulateInspector.cs
  63. /////////////////////////////////////////////////////////////////////////////////////////////////
  64.  
  65. using UnityEngine;
  66. using UnityEditor;
  67. using System.Collections;
  68.  
  69. /// <summary>
  70. /// This is the inspector of SyncedSimulate.cs, make sure this is in a folder called 'Editor'.
  71. /// </summary>
  72. [CustomEditor(typeof(SyncedSimulate))]
  73. class SyncedSimulateInspector : Editor
  74. {
  75.     SyncedSimulate _sSimulate;
  76.  
  77.     public void OnEnable ()
  78.     {
  79.         _sSimulate = (SyncedSimulate)target;
  80.     }
  81.  
  82.     public override void OnInspectorGUI ()
  83.     {
  84.         _sSimulate.pauseOnStart = EditorGUILayout.Toggle ("Pause On Start", _sSimulate.pauseOnStart);
  85.         _sSimulate.pauseOnSimulation = EditorGUILayout.Toggle ("Pause On Simulation", _sSimulate.pauseOnSimulation);
  86.         _sSimulate.stepTime = EditorGUILayout.FloatField("Step Time", _sSimulate.stepTime);
  87.         _sSimulate.stepResolution = EditorGUILayout.IntField("Step Resolution", _sSimulate.stepResolution);
  88.         if (GUILayout.Button ("Step"))
  89.             _sSimulate.Simulate(_sSimulate.stepTime, _sSimulate.stepResolution);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement