Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TheRealRan · Mar 26, 2021 at 12:31 PM · editorwindowmousepositiongraphnodes

How to get the correct contextual menu mouse position on graph view when I have ContentDragger & ContentZoomer manipulators?

I've added the following manipulators to the GraphView:

 this.AddManipulator(new ContentZoomer());
 this.AddManipulator(new SelectionDragger());
 this.AddManipulator(new ContentDragger());
 this.AddManipulator(new RectangleSelector());

Then I've added a manipulator to add a Contextual Menu to add a node, like so:

 this.AddManipulator(CreateContextualMenu("Title"));
 
 private ContextualMenuManipulator CreateContextualMenu(string contextualMenuText)
 {
     return new ContextualMenuManipulator(menuEvent =>
         menuEvent.menu.AppendAction(contextualMenuText, actionEvent =>
             AddElement(CreateNode(actionEvent.eventInfo.mousePosition)), DropdownMenuAction.AlwaysEnabled));
 }

The CreateNode method receives the mouse position and inserts the node at the correct place, until I drag the graph view (due to the content dragger) and try to create a new node.

I've inserted a log to see the position and it seems that the actionEvent.eventInfo.mousePosition (and others) only goes up to the current window size, and therefore instead of adding on the current position it creates in the closest to the window size (which in terms of graph position, it might be in a position that isn't showing after I drag).

The contextual menu is where I've clicked to add a node, and the node on the left is where it was added.

Graph View

I've tried adding a MouseMove/Up/DownEvent to both the GraphView and GridBackground but it still does the same. I've also tried using Mouse.current.position.ReadValue() but the same happens.

Is there a way to fix this?

![alt text][2]

upload-2021-3-26-1-28-41.png (7.4 kB)
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

4 Replies

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

Answer by TheRealRan · Mar 26, 2021 at 02:02 PM

I was able to kind of solve it (isn't perfect but works better) by adding these lines:

 Vector2 worldMousePosition = editorWindow.rootVisualElement.ChangeCoordinatesTo(editorWindow.rootVisualElement.parent, position - editorWindow.position.position);
 Vector2 localMousePosition = contentViewContainer.WorldToLocal(worldMousePosition);

Taken from this tutorial: https://www.youtube.com/watch?v=F4cTWOxMjMY

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
avatar image
3

Answer by quabug · Sep 18, 2021 at 11:12 AM

 var position = viewTransform.matrix.inverse.MultiplyPoint(evt.localMousePosition)
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
avatar image
1

Answer by MichaelDusk · Aug 10, 2021 at 03:54 PM

I found a solution that works perfect in my case (I use it as the position for newly created Nodes).


public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
    VisualElement contentViewContainer = ElementAt(1);
    Vector3 screenMousePosition = evt.localMousePosition;
    Vector2 worldMousePosition = screenMousePosition - contentViewContainer.transform.position;
    worldMousePosition *= 1 / contentViewContainer.transform.scale.x;
    // add context menu entries here...
}
Basically, ElementAt(1) is the "contentViewContainer" element, which is transformed (positioned and scaled) when you zoom / drag. alt text The evt parameter contains a localMousePosition that is in screenspace, with which you can calculate the "world position" .


screenshot-2021-08-10-175218.png (34.4 kB)
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
avatar image
0

Answer by Ziplaw · Oct 03, 2021 at 11:11 PM

This works like a charm, but I'll add that you should cache the localMousePosition of the event before modifying the event, otherwise in some versions of graphview it goes back to (0,0):

 Vector2 localMousePos = evt.localMousePosition;
 
 evt.menu.AppendAction(...) //And other possible manipulations...
     
 Vector2 actualGraphPosition = viewTransform.matrix.inverse.MultiplyPoint(localMousePos );


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

118 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

Related Questions

Visual script graph causes NPC to 'glitch' (shudder) when facing Player 0 Answers

I get weird bug when i use scene depth node in shader graph (lwrp) in VR Oculus rift. 0 Answers

Rect.contains with mouse position in editorwindow 1 Answer

Are Shader graph nodes not un-docking/busted? 0 Answers

Custom Editor Window Nodes Unresponsive Button 1 Answer


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