L
L
Layen2017-09-19 20:54:17
Unity
Layen, 2017-09-19 20:54:17

How to assign a specific button to an image from an array in Unity?

The topic is this: there is an array of 33 pictures, you need to assign each picture to a specific button, that is, if, say, the 3rd picture is the "E" button, and if the button and the picture match, some action occurs, yes, I know, the wording of the question weird, there is. Need help) I write in Unity in C #

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2017-09-20
@Layen

A dictionary is good for your case, but the unit does not know how to serialize them, so it's better to use a list of container classes:

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

public class Example : MonoBehaviour
{
    public List<KeyboardKey> keyboardKeys = new List<KeyboardKey>();

    private void Update()
    {
        for (int i = 0; i < keyboardKeys.Count; i++)
        {
            var key = keyboardKeys[i];
            if (Input.GetKeyDown(key.code))
            {
                // Делаем что-то с key.image
            }
        }
    }
}

[Serializable]
public class KeyboardKey
{
    public Image image;
    public KeyCode code;
}

If you wish, KeyboardKey can be made MonoBehaviour, here it’s more convenient for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question