MegaControl.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. // Two kinds, a mono versions and one for loading to objects
  3. [System.Serializable]
  4. public class MegaControl
  5. {
  6. public float[] Times;
  7. [HideInInspector]
  8. public int lastkey = 0;
  9. [HideInInspector]
  10. public float lasttime = 0.0f;
  11. public virtual float GetFloat(float time) { return 0.0f; }
  12. public virtual Vector3 GetVector3(float time) { return Vector3.zero; }
  13. int BinSearch(float t, int low, int high)
  14. {
  15. int probe = 0;
  16. while ( high - low > 1 )
  17. {
  18. probe = (high + low) / 2;
  19. if ( t < Times[probe] )
  20. high = probe;
  21. else
  22. {
  23. if ( t > Times[probe + 1] )
  24. low = probe;
  25. else
  26. break; // found
  27. }
  28. }
  29. return probe;
  30. }
  31. // get index
  32. // do a range check, anim code should keep the t in range
  33. public int GetKey(float t)
  34. {
  35. if ( t <= Times[1] )
  36. return 0;
  37. if ( t >= Times[Times.Length - 1] )
  38. return Times.Length - 2;
  39. // Cache result and then do a bin search
  40. int key = lastkey;
  41. if ( t >= Times[key] && t < Times[key + 1] )
  42. return key; // we get past this if out of time range of whole anim
  43. return BinSearch(t, -1, Times.Length - 1);
  44. }
  45. }