• 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 kinatun · Feb 04 at 04:33 PM · guieditornode

drag line in node editor

recently I'm follow a tutorial to create a node editor,the thing is the tutorial is using clicking to connect input/output of the nodes, so I try to make it using dragging to create node connection.

You can see the below code in DrawConnecting() is trying to draw line when the node input is getting dragged. But unfortunately it didn't work out. I put the Debug.Log() in DrawConnecting() it show the function is updating everytime. But it seems the DrawBezier() is not get called at all.

So I make it more aggressive by testing DrawBezier() in the OnGUI() function,now I can see the DrawBezier() is very laggy,it feels like updated every 2s or so.

Why and what can I do to achieve the effect I want?

    private void OnGUI()
     {
       //other thing

         DrawConnecting(Event.current);
         
         Handles.DrawBezier(Vector2.zero,
             Event.current.mousePosition,
             Vector2.right*50f,
             Event.current.mousePosition-Vector2.right*50f,
             Color.white,
         null,
         2f
         );

    
 }

   
 private void DrawConnecting(Event e)
 {
     bool ining = selectedInPoint != null;
     bool outing = selectedOutPoint != null;
     ConnectPoint p = ining ? selectedInPoint : selectedOutPoint;
     Vector2 p_dir = ining ? Vector2.left : Vector2.right;
     Vector2 t = e.mousePosition;
  //   if(ining^outing)
  if(e.type==EventType.MouseDrag && (ining||outing))
     {
         Debug.Log("Dragging at " +p.rect.position);
         Handles.DrawBezier(p.rect.center,
             t,
             p.rect.center + p_dir * 50f,
             t - p_dir * 50f,
             Color.white,
             null,
             2f
             );

     }

 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Feb 04 at 05:07 PM

EditorWindows are not auto-redrawn when a drag event is issued. When you want your window to be redrawn you have to call the Repaint method of your window.


Also note that you should not draw the bezier line when the mouse drag event happens. All drawing is done when the repaint event is handled. So you probably want something like this:

 if( ining || outing )
 {
     if ( e.type==EventType.MouseDrag )
         Repaint();
     else if( e.type==EventType.Repaint )
     {
         Handles.DrawBezier(
             p.rect.center,
             t,
             p.rect.center + p_dir * 50f,
             t - p_dir * 50f,
             Color.white, null, 2f
         );
     }
 }

The "Handles.DrawBezier" method does the event check already inside, that's why it hasn't any effect in your original case since you only call DrawBezier when the event is DragMouse. However the DrawBezier method only draws anything when called during the repaint event. So it will never do anything since the event can't be both at the same time.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Customize arrays and classes in inspector window 2 Answers

How to disable and enable UnityEditor Handles from receiving mouse events? 1 Answer

Can you display a model in a custom window without adding it to a scene? 0 Answers

UI Button and Color Tint Transition issue 0 Answers

Unity Editor script arrows to display a variable 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges