LoginSignup
2
0

More than 1 year has passed since last update.

【Unity】ボタンをスクリプトから生成する

Last updated at Posted at 2021-07-16

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class ItemPanel : MonoBehaviour
{
    public GameObject btn;
    public GameObject parent;

    // ボタン生成
    public void InstantiateUIBtn(GameObject parent, string name, float pos_x, float pos_y)
    {
        GameObject ui_btn = Instantiate(this.btn, new Vector3(pos_x, pos_y, 0), Quaternion.identity);

        // パネルを親に指定
        ui_btn.transform.SetParent(this.parent.transform,false);
        ui_btn.name = name;

        // クリックイベントを付与
        ui_btn.GetComponent<Button>().onClick.AddListener(() => BtnOnClick(ui_btn));
    }

    // クリックイベント内容
    void BtnOnClick(GameObject btn) {
        Debug.Log("ボタンをクリックしました" + btn.name);
    }
}

解説動画
https://youtu.be/Y7Ot3OJWjs4

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0