O
O
opt1ons2020-05-23 17:59:02
Android
opt1ons, 2020-05-23 17:59:02

How to delete a button when clicked?

Android game. At first I made a game according to the example of 3-in-a-row, because it was the closest in meaning to my idea. But now the question arose how to remove buttons when they are clicked. Button values ​​(I have: ShowBox (...)), how to write it differently (?).

Code one:
using System;

public delegate void ShowBox(int x, int y, int cube);

public class GAME
{
    public const int SIZE = 7;
    public const int CUBE = 3;

    ShowBox showBox;
    int[,] map;

    public GAME(ShowBox showBox)
    {
        this.showBox = showBox;
        map = new int[SIZE, SIZE];
    }

    public void Start()
    {
        ClearMap();
    }

    public void Click(int x, int y)
    {

    }

    private void ClearMap()
    {

    }

    private void SetMap(int x, int y, int cube)
    {
        map[x, y] = cube;
        showBox(x, y, cube);
    }
}


Second code:
using System;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Controller : MonoBehaviour
{
    Button[,] buttons;
    Image[] images;

    GAME game;

    void Start()
    {
        game = new GAME(ShowBox);
        InitButtons();
        InitImage();
        ShowBox(1, 1, 1);
        ShowBox(1, 2, 1);
        ShowBox(1, 3, 1);
        ShowBox(1, 4, 1);
        ShowBox(2, 5, 1);
        ShowBox(3, 3, 1);
        ShowBox(3, 4, 1);
        ShowBox(4, 5, 1);
        ShowBox(5, 1, 1);
        ShowBox(5, 2, 1);
        ShowBox(5, 3, 1);
        ShowBox(5, 4, 1);
        game.Start();
    }

    public void ShowBox(int x, int y, int cube)
    {
        buttons[x, y].GetComponent<Image>().sprite = images[cube].sprite;
    }

    public void Click()
    {

        string name = EventSystem.current.currentSelectedGameObject.name;
        int nr = GetNumber(name);
        int x = nr % GAME.SIZE;
        int y = nr / GAME.SIZE;
        Debug.Log($"clicked {name} {x} {y}");
        game.Click(x, y);
    }

    private void InitButtons()
    {
        buttons = new Button[GAME.SIZE, GAME.SIZE];
        for (int nr = 0; nr < GAME.SIZE * GAME.SIZE; nr++)
            buttons[nr % GAME.SIZE, nr / GAME.SIZE] = GameObject.Find($"Button ({nr})").GetComponent<Button>();
    }

    private void InitImage()
    {
        images = new Image[GAME.CUBE];
        for (int j = 0; j < GAME.CUBE; j++)
            images[j] = GameObject.Find($"Image ({j})").GetComponent<Image>();
    }

    private int GetNumber(string name)
    {
        Regex regex = new Regex("\\((\\d+)\\)");
        Match match = regex.Match(name);
        if (!match.Success)
            throw new System.Exception("Unrecognized object name");
        Group group = match.Groups[1];
        string number = group.Value;
        return Convert.ToInt32(number);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanielMcRon, 2020-05-23
@DanielMcRon

Destroy(gameObject)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question