Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Maeslezo · Jul 09, 2015 at 09:12 PM · guionguihandles

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _MGB_ · Jul 09, 2015 at 09:11 PM 0
Share

It will help if you post your code here.

avatar image _MGB_ · Jul 10, 2015 at 10:30 AM 0
Share

Any error output? If the script causes an exception it will stop execution. $$anonymous$$aybe the DrawPolyLine or nearby code is throwing an exception...

avatar image Maeslezo · Jul 10, 2015 at 10:37 AM 0
Share

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

avatar image Bunny83 · Jul 10, 2015 at 11:55 AM 0
Share

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.

avatar image Maeslezo · Jul 10, 2015 at 12:05 PM 0
Share

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);
         
         }
         ...
         }


Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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());
         }
 
     }
 }
 

 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to display a set of guiText OnGUI 0 Answers

OnGUI() Resources 1 Answer

Is there a way to set a GUI.matrix that will be used by all OnGui() functions? 1 Answer

Drawing GUI rectangle uses negative y coordinate 1 Answer

How to draw a triangle with OnGUI from its sides and angles? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges