Advertisement
thomazrb

Controlador Scroll View

Mar 26th, 2021
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class ControladorScrollView : MonoBehaviour
  7. {
  8.     [SerializeField]
  9.     private GameObject entrada;
  10.     [SerializeField]
  11.     private GameObject saida;
  12.     [SerializeField]
  13.     private GameObject ModeloDoItem;
  14.  
  15.     private int numero = 1;
  16.     private List<string> nomes = new List<string>();
  17.     private int idAtual = 0;
  18.     public void Adiciona()
  19.     {
  20.         string texto = entrada.transform.Find("Text").gameObject.GetComponent<Text>().text;
  21.  
  22.         if (texto != "")
  23.         {
  24.             if (!nomes.Contains(texto))
  25.             {
  26.                 GameObject item = Instantiate(ModeloDoItem) as GameObject;
  27.                 item.transform.SetParent(transform, false);
  28.                 item.GetComponentInChildren<Text>().text = texto;
  29.                 item.GetComponent<ControladorItem>().SetaId(numero);
  30.                 item.name = "item " + numero;
  31.                 numero++;
  32.                 nomes.Add(texto);
  33.             }
  34.             else
  35.                 Debug.Log("Já adicionado");
  36.  
  37.         }
  38.         else
  39.         {
  40.             Debug.Log("Nome vazio");
  41.         }
  42.     }
  43.  
  44.     public void Remove()
  45.     {
  46.         string nomeDoItem = "item " + idAtual;
  47.         GameObject item = GameObject.Find(nomeDoItem);
  48.         if (item)
  49.         {
  50.             nomes.Remove(item.GetComponentInChildren<Text>().text);
  51.             Destroy(item.gameObject);
  52.             saida.GetComponent<Text>().text = "Nenhum";
  53.             idAtual = 0;
  54.         }
  55.         else
  56.         {
  57.             Debug.Log("Item não encontrado");
  58.         }
  59.     }
  60.  
  61.     public void Seleciona(string textoDoItem, int id)
  62.     {
  63.         saida.GetComponent<Text>().text = textoDoItem;
  64.         idAtual = id;
  65.     }
  66.  
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement