Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question how to hover element from script?

Discussion in 'UI Toolkit' started by jeffkenz, Dec 16, 2022.

  1. jeffkenz

    jeffkenz

    Joined:
    Apr 8, 2013
    Posts:
    22
    is there a way to hover cursor to element to trigger event from script (mouse not click only move cursor) ?
     
  2. C-UITools

    C-UITools

    Unity Technologies

    Joined:
    Jun 23, 2021
    Posts:
    22
    Hello!

    If I understand correctly, you want to something to happen in your script when the mouse hovers over a VisualElement, but not when you click on it?

    You could register a callback on
    MouseEnterEvent
    or
    MouseOverEvent
    , depending on your desired behaviour. For more events, check out this link: Unity - Manual: Mouse events (unity3d.com)

    Hope this helps!
     
    kruskal21 likes this.
  3. jeffkenz

    jeffkenz

    Joined:
    Apr 8, 2013
    Posts:
    22
    hi thanks for the reply,but on unity toolkit event like
    MouseEnterEvent
    or
    MouseOverEvent only invoke when there is interaction/input from user(mouse click) if mouse cursor touch or overlaps no event invoke,it is bug?
     
  4. C-UITools

    C-UITools

    Unity Technologies

    Joined:
    Jun 23, 2021
    Posts:
    22
    Which version are you on?
    MouseEnterEvent
    works fine on 2022.2.1 . If you want to verify quickly that an event works, you can do this in your code (provided you have a reference to the Label/VisualElement you are testing):

    Code (CSharp):
    1. label.RegisterCallback<MouseEnterEvent>(evt =>
    2. {
    3.     Debug.Log("MouseEnterEvent registered");
    4. });
    and look at the Unity console.
     
    Last edited: Dec 20, 2022
  5. jeffkenz

    jeffkenz

    Joined:
    Apr 8, 2013
    Posts:
    22
    2022.20f1 ,i know how to debug,but the behaviour of the mouseEnterevent like i said it must be click 1st to invoke the event,u cannot hover the cursor on the element to make it invoke,the question is?it's bug or event on ui toolkit like that?in the end right now i tried to make my own hover logic with mouse and worldBound (using rect to compare with mouse position)