以下の内容はhttps://baba-s.hatenablog.com/entry/2018/01/03/110100より取得しました。


【Unity】uGUI でスクリプトからボタンを押す方法

概要

ExecuteEvents.Execute 関数を使用することで
スクリプトから uGUI のボタンを押すことができます

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Example : MonoBehaviour
{
    public Button target;

    private void Start()
    {
        target.onClick.AddListener( () => Debug.Log( "ピカチュウ" ) );
    }

    private void Update()
    {
        if ( Input.GetKeyDown( KeyCode.Z ) )
        {
            ExecuteEvents.Execute
            (
                target      : target.gameObject,
                eventData   : new PointerEventData( EventSystem.current ),
                functor     : ExecuteEvents.pointerClickHandler
            );
        }
    }
}

関連記事




以上の内容はhttps://baba-s.hatenablog.com/entry/2018/01/03/110100より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14