Answer the question
In order to leave comments, you need to log in
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
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question