Advertisement
Hyluss

[Unity] ChoiceCharactersMenu

Apr 30th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6.  
  7. public class CharactersChoiceMenu : MonoBehaviour {
  8.  
  9.     [SerializeField] Scrollbar scrollBar;
  10.     [SerializeField] GameObject gridWithCharacters;
  11.     [SerializeField] Button startButton;
  12.  
  13.     [SerializeField] ChosenCharacterColors chosenCharacterColors;
  14.     [SerializeField] int numberOfPlayers;
  15.  
  16.     List<Button> selectedButtons = new List<Button>();
  17.  
  18.     private void Awake()
  19.     {
  20.         Button[] buttons = gridWithCharacters.GetComponentsInChildren<Button>();
  21.         scrollBar.numberOfSteps = buttons.Length;
  22.  
  23.         foreach (var button in buttons)
  24.         {
  25.             button.onClick.AddListener(() => onCharactersClick(button));
  26.         }
  27.     }
  28.  
  29.     private void Start()
  30.     {
  31.         chosenCharacterColors.NextColor();
  32.     }
  33.  
  34.     void UncheckButton(Button button)
  35.     {
  36.         Color color = button.GetComponent<Image>().color;
  37.         chosenCharacterColors.UnlockColor(color);
  38.         chosenCharacterColors.NextColor();
  39.  
  40.         button.GetComponent<Image>().color = Color.white;
  41.         selectedButtons.Remove(button);
  42.     }
  43.  
  44.     void onCharactersClick(Button button)
  45.     {
  46.         if (selectedButtons.Count == numberOfPlayers)
  47.         {
  48.             if (!selectedButtons.Contains(button))
  49.             {
  50.                 return;
  51.             }
  52.         }
  53.        
  54.         if (selectedButtons.Contains(button))
  55.         {
  56.             UncheckButton(button);
  57.             OnValidateStartButton();
  58.             return;
  59.         }
  60.  
  61.         OnCheckedButton(button);
  62.         OnValidateStartButton();
  63.     }
  64.  
  65.     void OnValidateStartButton()
  66.     {
  67.         if (selectedButtons.Count == numberOfPlayers)
  68.         {
  69.             startButton.interactable = true;
  70.         }
  71.         else
  72.         {
  73.             startButton.interactable = false;
  74.         }
  75.     }
  76.  
  77.     void OnCheckedButton(Button button)
  78.     {
  79.         selectedButtons.Add(button);
  80.  
  81.         button.GetComponent<Image>().color = chosenCharacterColors.CurrentColor;
  82.         chosenCharacterColors.LockColor(chosenCharacterColors.CurrentColor);
  83.         chosenCharacterColors.NextColor();
  84.     }
  85.  
  86.     void ChangeButtonColor(Button button, Color color)
  87.     {
  88.         button.GetComponent<Image>().color = color;
  89.     }
  90.  
  91.     /*
  92.     static Color SumUpColors (Color firstColor, Color secondColor)
  93.     {
  94.         float r = (firstColor.r + secondColor.r) / 2;
  95.         float g = (firstColor.g + secondColor.g) / 2;
  96.         float b = (firstColor.b + secondColor.b) / 2;
  97.         float a = 255f;
  98.  
  99.         Color expectedColor = new Color(r, g, b, a);
  100.         return expectedColor;
  101.     }
  102.     */
  103.  
  104.     public void StartButtonClick()
  105.     {
  106.         SaveChosenCharacters();
  107.         SceneManager.LoadScene("ChooseCharacter");
  108.     }
  109.  
  110.     public void Return()
  111.     {
  112.         SceneManager.LoadScene("MapChoiceMenu");
  113.     }
  114.  
  115.     void SaveChosenCharacters()
  116.     {
  117.         CharacterType.TYPE[] characterType = new CharacterType.TYPE[numberOfPlayers];
  118.         for (int i = 0; i < selectedButtons.Count; i++)
  119.         {
  120.             characterType[i] = selectedButtons[i].GetComponent<CharacterType>().Type;
  121.             PlayerPrefs.SetInt("Player" + i, (int)characterType[i]);
  122.             Debug.Log( ((CharacterType.TYPE)PlayerPrefs.GetInt("Player" + i)) + " has been chosen ");
  123.         }
  124.     }
  125.  
  126. #if !UNITY_IOS
  127.     private void Update()
  128.     {
  129.         if (Input.GetKeyDown(KeyCode.Escape))
  130.         {
  131.             SceneManager.LoadScene("MapChoiceMenu");
  132.         }
  133.     }
  134. #endif
  135.  
  136.  
  137. }
  138.  
  139.  
  140. [System.Serializable]
  141. public class ChosenCharacterColors
  142. {
  143.     [SerializeField] List<ColorChoice> colorChoiceList;
  144.     Color currentColor;
  145.  
  146.     public Color CurrentColor { get { return currentColor; } }
  147.  
  148.     /*
  149.     public ChosenCharacterColors()
  150.     {
  151.         NextColor();
  152.     }
  153.     */    
  154.  
  155.     public void NextColor()
  156.     {
  157.         for (int i = 0; i < colorChoiceList.Count; i++)
  158.         {
  159.             if (colorChoiceList[i].Used == false)
  160.             {
  161.                 currentColor = colorChoiceList[i].Color;
  162.             }
  163.         }
  164.     }
  165.  
  166.     public void UnlockColor(Color color)
  167.     {
  168.         colorChoiceList.Find(x => x.Color == color).Used = false;
  169.     }    
  170.  
  171.     public void LockColor(Color color)
  172.     {
  173.         colorChoiceList.Find(x => x.Color == color).Used = true;
  174.     }
  175.  
  176. }
  177.  
  178. [System.Serializable]
  179. public class ColorChoice
  180. {
  181.     public Color Color;
  182.     bool used = false;
  183.     public bool Used { get { return used; } set { used = value; } }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement