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

Adding mouse over event to toggle

Discussion in '2D' started by kabuto178, Mar 31, 2017.

  1. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    How can I add an event to watch for mouse hover over a toggle button in unity 5?
     
  2. Zonlib

    Zonlib

    Joined:
    Apr 15, 2014
    Posts:
    39
    You add an Event Triggers component then add PointerEnter event:

     
  3. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    My GUI items are being added through code though
     
  4. Zonlib

    Zonlib

    Joined:
    Apr 15, 2014
    Posts:
    39
    Try this

    Code (CSharp):
    1.    using UnityEngine.EventSystems;
    2.    ...
    3.     void AssignEvent(){
    4.         EventTrigger eventTrigger = _targetButton.gameObject.AddComponent<EventTrigger>();
    5.         EventTrigger.Entry eventEntry = new EventTrigger.Entry();
    6.         eventEntry.eventID = EventTriggerType.PointerEnter;
    7.         eventEntry.callback.AddListener((data) => {OnPointerEnterDelegate((PointerEventData)data); });
    8.         eventTrigger.triggers.Add(eventEntry);
    9.     }
    10.  
    11.     public void OnPointerEnterDelegate(PointerEventData data)
    12.     {
    13.         //Take some action here
    14.     }
     
  5. kabuto178

    kabuto178

    Joined:
    Mar 21, 2017
    Posts:
    59
    my target button is actually made from script, GUI.Toggle(new Rect(20,(70*i) + 70, 100,50), items, names);