Skip to content

Instantly share code, notes, and snippets.

@prodigga
Last active May 5, 2018 10:22
Show Gist options
  • Save prodigga/b1cfbaadfa6723784b7b to your computer and use it in GitHub Desktop.
Save prodigga/b1cfbaadfa6723784b7b to your computer and use it in GitHub Desktop.
Thread Safe Curve, Unity
[System.Serializable]
public class ThreadsafeCurve : ISerializationCallbackReceiver
{
[SerializeField]
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>();
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();
}
}
@JGuthrie-Bluebeck
Copy link

This wont work for Unity 2018.2 (One it releases) since it flags the animation curves evaluate as not thread safe, and now throws and error if you try and call it during serialisation as you are here.

This should fix and micro optimise it slightly (ignore my namespace)

`using UnityEngine;

namespace Laircraft
{
[System.Serializable]
public class CustomCurve
{
#if(UNITY_EDITOR)
[SerializeField]
AnimationCurve curve;
#endif

    public int cachedValues = 100;

    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 * cachedValues)];
    }

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

    public void RefreshValues()
    {
        precalculatedValues = new float[cachedValues + 1];

        if(curve == null)
            return;

        for(int i = 0; i <= cachedValues; i++)
            precalculatedValues[i] = curve.Evaluate(i / (float)cachedValues);
    }
}

}`

@JGuthrie-Bluebeck
Copy link

Also needs a property drawer update to work this way:

` [CustomPropertyDrawer(typeof(CustomCurve))]
public class CustomCurvePropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
EditorGUI.PropertyField(position, property.FindPropertyRelative("curve"), GUIContent.none);

        CustomCurve curve = (CustomCurve)fieldInfo.GetValue(property.serializedObject.targetObject);
        curve.RefreshValues();

        EditorGUI.EndProperty();
    }
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment