Advertisement
Guest User

Deformable Manager Immediate Version

a guest
Nov 27th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Unity.Jobs;
  4.  
  5. namespace Deform
  6. {
  7.     /// <summary>
  8.     /// Manages scheduling deformables.
  9.     /// </summary>
  10.     [HelpURL ("https://github.com/keenanwoodall/Deform/wiki/DeformableManager")]
  11.     public class DeformableManager : MonoBehaviour
  12.     {
  13.         private static readonly string DEF_MANAGER_NAME = "DefaultDeformableManager";
  14.  
  15.         private static DeformableManager defaultInstance;
  16.         /// <summary>
  17.         /// Returns the default manager.
  18.         /// </summary>
  19.         /// <param name="createIfMissing">If true, a manager will be created if one doesn't exist.</param>
  20.         /// <returns></returns>
  21.         public static DeformableManager GetDefaultManager (bool createIfMissing)
  22.         {
  23.             if (defaultInstance == null)
  24.                 defaultInstance = new GameObject (DEF_MANAGER_NAME).AddComponent<DeformableManager> ();
  25.             return defaultInstance;
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Should the manager update?
  30.         /// </summary>
  31.         public bool update = true;
  32.  
  33.         private HashSet<IDeformable> deformables = new HashSet<IDeformable> ();
  34.  
  35.         /// <summary>
  36.         /// Temporary storage for added deformables to allow them to be updated immediately on the first frame they're added
  37.         /// </summary>
  38.         private HashSet<IDeformable> addedDeformables = new HashSet<IDeformable> ();
  39.  
  40.         private void Update ()
  41.         {
  42.             if (update)
  43.                 ScheduleDeformables ();
  44.         }
  45.  
  46.         private void LateUpdate()
  47.         {
  48.             // Move added deformables into the main deformables collection
  49.             foreach (var added in addedDeformables)
  50.                 if (added != null)
  51.                     deformables.Add(added);
  52.             addedDeformables.Clear();
  53.  
  54.             CompleteDeformables();
  55.         }
  56.  
  57.         private void OnDisable ()
  58.         {
  59.             CompleteDeformables ();
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Creates a chain of work from the deformables and schedules it.
  64.         /// </summary>
  65.         public void ScheduleDeformables ()
  66.         {
  67.             foreach (var deformable in deformables)
  68.                 deformable.PreSchedule ();
  69.             foreach (var deformable in deformables)
  70.                 deformable.Schedule ();
  71.  
  72.             // Schedule the chain.
  73.             JobHandle.ScheduleBatchedJobs ();
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Finishes the schedules work chain.
  78.         /// </summary>
  79.         public void CompleteDeformables ()
  80.         {
  81.             foreach (var deformable in deformables)
  82.             {
  83.                 deformable.Complete();
  84.                 if (update)
  85.                 {
  86.                     // Apply the finished work.
  87.                     deformable.ApplyData();
  88.                 }
  89.             }
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Registers a deformable to be updated by this manager.
  94.         /// </summary>
  95.         public void AddDeformable (IDeformable deformable)
  96.         {
  97.             addedDeformables.Add (deformable);
  98.             // Force an immediate update so the deformable isn't undeformed on the first frame.
  99.             deformable.ForceImmediateUpdate ();
  100.             // Since changes from the previous frame are applied on the next, schedule changes now so that
  101.             // when the next frame arrives the reset data from the immediate update isn't applied.
  102.             deformable.PreSchedule ();
  103.             deformable.Schedule ();
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Unregisters a deformable from this manager.
  108.         /// </summary>
  109.         public void RemoveDeformable (IDeformable deformable)
  110.         {
  111.             deformables.Remove (deformable);
  112.             addedDeformables.Remove (deformable);
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement