Advertisement
leomovskii

ButtonController

Feb 8th, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. public class ButtonController : MonoBehaviour {
  6.  
  7.     public enum State {
  8.         Idle, Win, Lose
  9.     }
  10.  
  11.     public State state;
  12.     public bool startButton;
  13.     public GameObject Win, Lose;
  14.     public List<ButtonController> list;
  15.  
  16.     private Button button;
  17.  
  18.     public Button GetButton() {
  19.         return button;
  20.     }
  21.  
  22.     void Start() {
  23.         button = GetComponent<Button>();
  24.         if (!startButton) {
  25.             gameObject.SetActive(false);
  26.         }
  27.  
  28.         button.onClick.AddListener(OnButtonClick);
  29.     }
  30.  
  31.     void OnButtonClick() {
  32.         button.interactable = false;
  33.  
  34.         for (int i = 0; i < list.Count; i++) {
  35.             list[i].gameObject.SetActive(true);
  36.             list[i].GetButton().interactable = true;
  37.         }
  38.  
  39.         if (state == State.Win) {
  40.             Debug.Log("Победа!");
  41.             if (Win != null) {
  42.                 Win.SetActive(true);
  43.             }
  44.  
  45.         } else if (state == State.Lose) {
  46.             Debug.Log("Поражение!");
  47.             if (Lose != null) {
  48.                 Lose.SetActive(true);
  49.             }
  50.         }
  51.     }
  52.  
  53.     private void OnDrawGizmos() {
  54.         Gizmos.color = Color.magenta;
  55.         var pointA = transform.position;
  56.         for (int i = 0; i < list.Count; i++) {
  57.             if (list[i] != null) {
  58.                 var pointB = list[i].transform.position;
  59.                 Gizmos.DrawLine(pointA, pointB);
  60.             }
  61.         }
  62.         if (startButton) {
  63.             Gizmos.DrawWireSphere(transform.position, .5f);
  64.  
  65.         } else if (state == State.Win) {
  66.             Gizmos.color = new Color(0f, 1f, 0f, 0.3f);
  67.             Gizmos.DrawSphere(transform.position, .5f);
  68.  
  69.         } else if (state == State.Lose) {
  70.             Gizmos.color = new Color(1f, 0f, 0f, 0.3f);
  71.             Gizmos.DrawSphere(transform.position, .5f);
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement