Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Find the package here: http://polyfied.com/development/unity/playground/Playground_SyncedSimulate.unitypackage
- /////////////////////////////////////////////////////////////////////////////////////////////////
- // Playground Synced Simulate/SyncedSimulate.cs
- /////////////////////////////////////////////////////////////////////////////////////////////////
- using UnityEngine;
- using System.Collections;
- using ParticlePlayground;
- /// <summary>
- /// 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.
- /// </summary>
- [ExecuteInEditMode()]
- public class SyncedSimulate : MonoBehaviour
- {
- public bool pauseOnStart = false; // Should the simulation be paused in the beginning?
- public bool pauseOnSimulation = false; // Should the simulation pause upon calling Simulate()?
- public float stepTime = 1f; // The time to step forward each Simulate() call
- public int stepResolution = 100; // The resolution to calculate forces, the more the heavier on CPU
- PlaygroundParticlesC _particles; // Cached particle system reference
- void OnEnable ()
- {
- _particles = GetComponent<PlaygroundParticlesC>();
- _particles.applyDeltaOnRebirth = true;
- if (pauseOnStart)
- _particles.particleTimescale = 0;
- }
- /// <summary>
- /// Simulate on main-thread to the specified time. Resolution determines how exact the final result should be in terms of forces.
- /// </summary>
- /// <param name="time">Time of simulation.</param>
- /// <param name="resolution">The amount of calculated steps.</param>
- public void Simulate (float time, int resolution)
- {
- _particles.particleTimescale = 1f;
- if (resolution < 1)
- resolution = 1;
- float currentTime = PlaygroundC.globalTime;
- for (int i = 0; i<resolution; i++)
- {
- PlaygroundC.globalTime = currentTime+(time*((i*1f)/(resolution*1f)));
- PlaygroundParticlesC.ThreadedCalculations(_particles);
- }
- if (pauseOnSimulation)
- _particles.particleTimescale = 0;
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////
- // Playground Synced Simulate/Editor/SyncedSimulateInspector.cs
- /////////////////////////////////////////////////////////////////////////////////////////////////
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- /// <summary>
- /// This is the inspector of SyncedSimulate.cs, make sure this is in a folder called 'Editor'.
- /// </summary>
- [CustomEditor(typeof(SyncedSimulate))]
- class SyncedSimulateInspector : Editor
- {
- SyncedSimulate _sSimulate;
- public void OnEnable ()
- {
- _sSimulate = (SyncedSimulate)target;
- }
- public override void OnInspectorGUI ()
- {
- _sSimulate.pauseOnStart = EditorGUILayout.Toggle ("Pause On Start", _sSimulate.pauseOnStart);
- _sSimulate.pauseOnSimulation = EditorGUILayout.Toggle ("Pause On Simulation", _sSimulate.pauseOnSimulation);
- _sSimulate.stepTime = EditorGUILayout.FloatField("Step Time", _sSimulate.stepTime);
- _sSimulate.stepResolution = EditorGUILayout.IntField("Step Resolution", _sSimulate.stepResolution);
- if (GUILayout.Button ("Step"))
- _sSimulate.Simulate(_sSimulate.stepTime, _sSimulate.stepResolution);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement