MegaControl.cs 1.2 KB

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