Tengku Fathullah
Tengku Fathullah

Reputation: 1370

How to add Unity component functions in UnityEvent using script

I created a custom editor where the object and their components are automatically added.

But how do I put the Unity component built-in function in UnityEvent via script?

In this case, I want to put audio source Play() function in the UnityEvent via script.

before after

Upvotes: 1

Views: 5570

Answers (3)

ZayedUpal
ZayedUpal

Reputation: 1601

Hope this works:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;

[ExecuteInEditMode]
[RequireComponent (typeof (AudioSource))]
public class SceneAnimationEvent : MonoBehaviour {
    public AudioSource audioSrc;
    [SerializeField]
    public UnityEvent unityEvent;
    UnityAction methodDelegate;
    bool eventAdded = false;

    void OnEnable() {
        UpdateInspector ();
    }
    void UpdateInspector() {
        if (!eventAdded) {
            audioSrc = GetComponent<AudioSource> ();
            unityEvent = new UnityEvent ();
            methodDelegate = System.Delegate.CreateDelegate (typeof(UnityAction), audioSrc, "Play") as UnityAction;
            UnityEditor.Events.UnityEventTools.AddPersistentListener (unityEvent, methodDelegate);
            eventAdded = true;
        }
    }
}

Upvotes: 6

Tengku Fathullah
Tengku Fathullah

Reputation: 1370

Thanks to @zayedupal's answer, this is my final code to automatically create a GameObject, Components and automatically add a UnityEvent.

[MenuItem("GameObject/Create Animation/With SFX", false, -1)]
static void CreateAnimationWithSFX()
{
    GameObject go = new GameObject("animationWithSFX");
    go.transform.SetParent(Selection.activeTransform);

    AudioSource audioSource = go.AddComponent<AudioSource>();
    audioSource.playOnAwake = false;
    // Automate-add the right channel for audio source
    AudioMixer mixer = Resources.Load("Master") as AudioMixer;
    audioSource.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];

    SceneAnimationEvent script = go.AddComponent<SceneAnimationEvent>();
    // Automate-add the unity built-in function into UnityEvent
    script.Events = new UnityEvent ();
    UnityAction methodDelegate = System.Delegate.CreateDelegate (typeof(UnityAction), audioSource, "Play") as UnityAction;
    UnityEventTools.AddPersistentListener (script.Events, methodDelegate);

    Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
    Selection.activeObject = go;
}

Upvotes: 2

5argon
5argon

Reputation: 3863

You can use unityEvent.AddListener

public class EventRunner : MonoBehaviour {

    public AudioSource audioSource;
    public UnityEvent ev;

    IEnumerator Start () {

        //you won't see an entry comes up in the inspector
        ev.AddListener(audioSource.Play);

        while (true)
        {
            ev.Invoke(); //you will hear the sound
            yield return new WaitForSeconds(0.5f);
        }

    }
}

Upvotes: 1

Related Questions