- Home /
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.
Any error output? If the script causes an exception it will stop execution. $$anonymous$$aybe the DrawPolyLine or nearby code is throwing an exception...
Nope. And as I said, it works perfectly fine if I call it from OnPreviewGUI but only draws one line if I call it from OnGUI and ingnores/discards all the others Handle methods o_0
You have to be more specific how you actually use it. The "answer" below doesn't help at all. It's just a collection of helper methods...
We need to know:
What OnGUI are you talking about? An EditorWindow? A CustomInspector? Or even OnSceneGUI?
What initialization do you actually do before you use DrawPolyLine?
That should include which camera you use / setup for rendering.
The Handles class is for 3d drawing operations. "OnGUI" is usually only used for 2d drawing stuff. It's of course possible to use it even in OnGUI but you have to setup a 3d environment. So did you use Handles.SetCamera before you use the other Handles methods?
You might want to remove your "answer" completely and edit your question to include only relevant details, especially the setup of the Handle context.
Ok Bunny, I will rethink the question in a better way. Any way, I only posted the code because $$anonymous$$GB requested it.
It works fine in
[CustomEditor(typeof($$anonymous$$onoClass))]
$$anonymous$$onoClassEditor:Editor
{
...
OnPreviewGUI(Rect r,GuiStyle style)
{
float x = ($$anonymous$$onoClass)(target).GetX();
EditorUtils.Curves.DrawCurveWithXValue(r, x, $$anonymous$$X, maxX, funcThatTakesFloatAndReturnFloat);
}
...
}
It does not work properly (because draws the first handle method)
$$anonymous$$onoClass:$$anonymous$$onobehaviour
{
...
OnGUI()
{
Rect r = GetScreenRect();
EditorUtils.Curves.DrawCurveWithXValue(r, GetX(), $$anonymous$$X, maxX, funcThatTakesFloatAndReturnFloat);
}
...
}
Answer by Maeslezo · Jul 10, 2015 at 08:34 AM
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[i];
p.x = translateX(p.x);
p.y = translateY(p.y);
p.z = 0.0f;
pointList[i] = p;
}
}
private static List<Vector3> GetVector3List(List<Vector2> vector2List)
{
List<Vector3> vector3List = new List<Vector3>();
foreach (Vector2 vector2 in vector2List)
{
vector3List.Add(vector2);
}
return vector3List;
}
private static AnimationCurve GetAnimationCurveByVector2List(List<Vector2> points2D)
{
List<Keyframe> ks = new List<Keyframe>();
foreach (Vector2 point2D in points2D)
{
ks.Add(new Keyframe(point2D.x, point2D.y));
}
return new AnimationCurve(ks.ToArray());
}
}
}