thomazrb icon

Controlador Scroll View

thomazrb | PRO | 03/26/21 07:07:52 PM UTC | 0 ⭐ | 1247 👁️ | Never ⏰ | []
C# |

1.73 KB

|

None

|

0 👍

/

0 👎

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class ControladorScrollView : MonoBehaviour
{
    [SerializeField]
    private GameObject entrada;
    [SerializeField]
    private GameObject saida;
    [SerializeField]
    private GameObject ModeloDoItem;
 
    private int numero = 1;
    private List<string> nomes = new List<string>();
    private int idAtual = 0;
    public void Adiciona()
    {
        string texto = entrada.transform.Find("Text").gameObject.GetComponent<Text>().text;
 
        if (texto != "")
        {
            if (!nomes.Contains(texto))
            {
                GameObject item = Instantiate(ModeloDoItem) as GameObject;
                item.transform.SetParent(transform, false);
                item.GetComponentInChildren<Text>().text = texto;
                item.GetComponent<ControladorItem>().SetaId(numero);
                item.name = "item " + numero;
                numero++;
                nomes.Add(texto);
            }
            else
                Debug.Log("Já adicionado");
 
        }
        else
        {
            Debug.Log("Nome vazio");
        }
    }
 
    public void Remove()
    {
        string nomeDoItem = "item " + idAtual;
        GameObject item = GameObject.Find(nomeDoItem);
        if (item)
        {
            nomes.Remove(item.GetComponentInChildren<Text>().text);
            Destroy(item.gameObject);
            saida.GetComponent<Text>().text = "Nenhum";
            idAtual = 0;
        }
        else
        {
            Debug.Log("Item não encontrado");
        }
    }
 
    public void Seleciona(string textoDoItem, int id)
    {
        saida.GetComponent<Text>().text = textoDoItem;
        idAtual = id;
    }
 
}
 

Comments