Using Handles at OnGUI

Hello Guys!

I am using Handles.DrawPolyLine and Handles.Label for drawing some curves and values of those curves.

At OnPreviewGUI everything work just fine.

However, when I call the same method at OnGUI, only the first Handle method is drawn, although the others are called (I have debugged that).

If I comment the first DrawPolyLine, the second DrawPolyLine is drawn.

I do not know what could be happening.

Any idea?

Using Unity 4.6.5

Thank you very much.

Sure, but I am not sure if it will help.

The main method is: DrawCurveWithXValue

namespace EditorUtils
{
    public static class Curves
    {

        public static void DrawCurveField(Rect r, float minX, float maxX, float step, Color color, Func<float, float> func)
        {
            AnimationCurve curve = GetAnimationCurveByVector2List(GetCurve(minX, maxX, step, func));

            float minY = curve.keys.Min(key => key.value);
            float maxY = curve.keys.Max(key => key.value);

            Rect ranges = new Rect(minX, minY, maxX - minX, (maxY - minY));

            EditorGUI.CurveField(r, curve, color, ranges);
        }


        public static void DrawCurveFieldLayout(float minX, float maxX, float step, string title, Color color, Func<float, float> func)
        {
            AnimationCurve curve = GetAnimationCurveByVector2List(GetCurve(minX, maxX, step, func));

            float minY = curve.keys.Min(key => key.value);
            float maxY = curve.keys.Max(key => key.value);

            Rect ranges = new Rect(minX, minY, maxX - minX, (maxY - minY));

            EditorGUILayout.CurveField(title, curve, color, ranges);
        }

        public static void DrawCurveWithXValue(Rect r, float x, float minX, float maxX, Func<float, float> func)
        {
            List<Vector3> points = GetVector3List(GetCurve(minX, maxX, 1, func));

            float maxY = points.Max(p => p.y);
            float minY = points.Min(p => p.y);
            
            Func<float, float> translateX = oldX => ((oldX - minX) * (r.xMax - r.xMin) / (maxX - minX)) + r.xMin;
            Func<float, float> translateY = oldY => (-(oldY - minY) * (r.yMax - r.yMin) / (maxY - minY)) + r.yMin + r.height;

            TranslatePointList(points, translateX, translateY);
            
            DrawCurve(points);
            
            DrawCurveValueByX(x, func, translateX, translateY);
            
            DrawVerticalLineInXValue(r, x, translateX);

        }

        private static List<Vector2> GetCurve(float minX, float maxX, float step, Func<float, float> func)
        {
            List<Vector2> ks = new List<Vector2>();

            for (float x = minX; x < maxX; x += step)
            {
                ks.Add(new Vector2(x, func(x)));
            }


            return ks;
        }

        private static void DrawCurve(List<Vector3> points)
        {
            Handles.color = Color.blue;
            Handles.DrawPolyLine(points.ToArray());
        }

        private static void DrawVerticalLineInXValue(Rect r, float value, Func<float, float> translateX)
        {
            Vector3[] constantPoints = new Vector3[2];
            float valueNewAxes = translateX(value);
            constantPoints[0].x = valueNewAxes;
            constantPoints[1].x = valueNewAxes;

            constantPoints[0].y = r.yMin;
            constantPoints[1].y = r.yMax;

            Handles.color = Color.red;
            
            Handles.DrawPolyLine(constantPoints);

            GUIStyle guiStyle = new GUIStyle();
            guiStyle.normal.textColor = Color.red;
            
            Vector3 labelPos = (constantPoints[0] + constantPoints[1]) * (1 / 2.0f);
            Handles.Label(labelPos, value.ToString("0.0000"),guiStyle);
        }

        private static void DrawCurveValueByX(float x, Func<float, float> func, Func<float, float> translateX, Func<float, float> translateY)
        {
            GUIStyle guiStyle = new GUIStyle();
            guiStyle.normal.textColor = Color.blue;
            guiStyle.fontStyle = FontStyle.Bold;
            
            float constantNewXAxe = translateX(x);
            float yConstantValue = func(x);
            float constantNewYAxe = translateY(yConstantValue);
            Handles.Label(new Vector3(constantNewXAxe, constantNewYAxe, 0f), yConstantValue.ToString("0.000"), guiStyle);
        }

        private static void TranslatePointList(List<Vector3> pointList, Func<float, float> translateX,
            Func<float, float> translateY)
        {
            for (int i = 0; i < pointList.Count; i++)
            {
                Vector3 p = pointList*;*

p.x = translateX(p.x);

p.y = translateY(p.y);
p.z = 0.0f;

pointList = p;
}
}

private static List GetVector3List(List vector2List)
{
List vector3List = new List();

foreach (Vector2 vector2 in vector2List)
{
vector3List.Add(vector2);
}

return vector3List;
}

private static AnimationCurve GetAnimationCurveByVector2List(List points2D)
{
List ks = new List();
foreach (Vector2 point2D in points2D)
{
ks.Add(new Keyframe(point2D.x, point2D.y));
}

return new AnimationCurve(ks.ToArray());
}

}
}