Skip to content

Instantly share code, notes, and snippets.

@prodigga
Last active May 5, 2018 10:22
Show Gist options
  • Select an option

  • Save prodigga/b1cfbaadfa6723784b7b to your computer and use it in GitHub Desktop.

Select an option

Save prodigga/b1cfbaadfa6723784b7b to your computer and use it in GitHub Desktop.

Revisions

  1. prodigga revised this gist Aug 5, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ThreadsafeCurve.cs
    Original file line number Diff line number Diff line change
    @@ -6,18 +6,21 @@ public class ThreadsafeCurve : ISerializationCallbackReceiver
    private AnimationCurve _curve;
    private Dictionary<int, float> _precalculatedValues;

    //Returns the Y value of the curve at X = time. Time must be between 0 - 1.
    public float Evaluate(float time)
    {
    time = Mathf.Clamp01(time);
    return _precalculatedValues[Mathf.RoundToInt(time*100)];
    }

    //Assign new animation curve
    public void SetCurve(AnimationCurve curve)
    {
    _curve = curve;
    RefreshValues();
    }

    //Refresh internal cache
    public void RefreshValues()
    {
    _precalculatedValues = new Dictionary<int, float>();
  2. prodigga created this gist Aug 4, 2015.
    40 changes: 40 additions & 0 deletions ThreadsafeCurve.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@

    [System.Serializable]
    public class ThreadsafeCurve : ISerializationCallbackReceiver
    {
    [SerializeField]
    private AnimationCurve _curve;
    private Dictionary<int, float> _precalculatedValues;

    public float Evaluate(float time)
    {
    time = Mathf.Clamp01(time);
    return _precalculatedValues[Mathf.RoundToInt(time*100)];
    }

    public void SetCurve(AnimationCurve curve)
    {
    _curve = curve;
    RefreshValues();
    }

    public void RefreshValues()
    {
    _precalculatedValues = new Dictionary<int, float>();

    if (_curve == null)
    return;

    for (int i = 0; i <= 100; i++)
    _precalculatedValues.Add(i, _curve.Evaluate(i / 100f));
    }

    public void OnBeforeSerialize()
    {
    }

    public void OnAfterDeserialize()
    {
    RefreshValues();
    }
    }