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. Dismiss Notice

Creating new Functions for the UI Eventsystem

Discussion in 'UGUI & TextMesh Pro' started by TheFacelessOne, Sep 9, 2014.

  1. TheFacelessOne

    TheFacelessOne

    Joined:
    Aug 19, 2014
    Posts:
    5
    I don't know the syntax and was wondering how I could create a function within a script that is able to be manipulated through the Eventsystem, such as changing a bool in that function to true.
     
  2. Caio_Lib

    Caio_Lib

    Joined:
    Mar 4, 2014
    Posts:
    83
    Hi TheFacelessOne,
    I don't know if I understand right, it would be something like this?

    Code (CSharp):
    1. using System;
    2. using System.Reflection;
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEngine.EventSystems;
    6. using UnityEngine.Events;
    7.  
    8. public class MyEventObject : MonoBehaviour, IEventSystemHandler
    9. {
    10.     [RenamedSerializedData("onPress")]
    11.     [SerializeField]
    12.     private ButtonClickedEvent m_OnClick = new ButtonClickedEvent();
    13.  
    14.     public void Start()
    15.     {
    16.     ButtonPressed();
    17.     }
    18.  
    19.     private void ButtonPressed()
    20.     {
    21.         m_OnClick.Invoke(true);
    22.     }
    23.  
    24.     [Serializable]
    25.     public class ButtonClickedEvent : UnityEvent<bool>
    26.     {}
    27. }
    Source: http://pastebin.com/TJ3EJJkU
    I don't know who wrote the code but thank you!
     
  3. TheFacelessOne

    TheFacelessOne

    Joined:
    Aug 19, 2014
    Posts:
    5
    I'm not sure if this works. Does it allow you to run the class/function/everything/whatever via the built-in UI Eventsystem? Instead of adding the script as a component.