Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting the point mouse intersects Canvas UI objects in Scene View

Discussion in 'Immediate Mode GUI (IMGUI)' started by pixxelbob, Oct 19, 2016.

  1. pixxelbob

    pixxelbob

    Joined:
    Aug 26, 2014
    Posts:
    111
    Hi
    I'm trying to make an editor extension to manipulate canvas UI objects.
    I need to be able to Raycast or find the point in world space in the scene view where the mouse position intersects with canvas objects in the scene view.

    Finding the object is easy using:
    Code (CSharp):
    1. HandleUtility.PickGameObject(mousePosition, false);
    but I need the actual intersection point.

    It's fairly easy with 3D meshes or object with Colliders on. Using
    Code (CSharp):
    1. HandleUtility.GUIPointToWorldRay (mousePosition);
    2. //and
    3. collider.Raycast
    4.  
    However I want to find the point the where the mousePosition intersects with Canvas UI objects in the scene.

    I cannot raycast with physics obviously because there's no colliders on canvas ui objects and EventSystem.current is null so I cannot Raycast with a EventSystem.current.RaycastAll() so I'm a bit stuck.

    Does anyone know a way this could be achieved?
     
    Last edited: Oct 19, 2016
    Deleted User likes this.
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Do you tried to GetComponent<GraphicRaycaster> on your root canvas and call Raycast() ?

    PS : I haven't test it, but it should work.
     
  3. pixxelbob

    pixxelbob

    Joined:
    Aug 26, 2014
    Posts:
    111
    I will try but I didn't think those instances would be available during OnSceneGUI()
    (Not during Play mode)