Hyluss icon

[Unity] ChoiceCharactersMenu

Hyluss | PRO | 04/30/18 03:38:08 PM UTC | 0 ⭐ | 439 👁️ | Never ⏰ | []
C# |

4.56 KB

|

None

|

0 👍

/

0 👎

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
public class CharactersChoiceMenu : MonoBehaviour {
 
    [SerializeField] Scrollbar scrollBar;
    [SerializeField] GameObject gridWithCharacters;
    [SerializeField] Button startButton;
 
    [SerializeField] ChosenCharacterColors chosenCharacterColors;
    [SerializeField] int numberOfPlayers;
 
    List<Button> selectedButtons = new List<Button>();
 
    private void Awake()
    {
        Button[] buttons = gridWithCharacters.GetComponentsInChildren<Button>();
        scrollBar.numberOfSteps = buttons.Length;
 
        foreach (var button in buttons)
        {
            button.onClick.AddListener(() => onCharactersClick(button));
        }
    }
 
    private void Start()
    {
        chosenCharacterColors.NextColor();
    }
 
    void UncheckButton(Button button)
    {
        Color color = button.GetComponent<Image>().color;
        chosenCharacterColors.UnlockColor(color);
        chosenCharacterColors.NextColor();
 
        button.GetComponent<Image>().color = Color.white;
        selectedButtons.Remove(button);
    }
 
    void onCharactersClick(Button button)
    {
        if (selectedButtons.Count == numberOfPlayers)
        {
            if (!selectedButtons.Contains(button))
            {
                return;
            }
        }
        
        if (selectedButtons.Contains(button))
        {
            UncheckButton(button);
            OnValidateStartButton();
            return;
        }
 
        OnCheckedButton(button);
        OnValidateStartButton();
    }
 
    void OnValidateStartButton()
    {
        if (selectedButtons.Count == numberOfPlayers)
        {
            startButton.interactable = true;
        }
        else
        {
            startButton.interactable = false;
        }
    }
 
    void OnCheckedButton(Button button)
    {
        selectedButtons.Add(button);
 
        button.GetComponent<Image>().color = chosenCharacterColors.CurrentColor;
        chosenCharacterColors.LockColor(chosenCharacterColors.CurrentColor);
        chosenCharacterColors.NextColor();
    }
 
    void ChangeButtonColor(Button button, Color color)
    {
        button.GetComponent<Image>().color = color;
    }
 
    /*
    static Color SumUpColors (Color firstColor, Color secondColor)
    {
        float r = (firstColor.r + secondColor.r) / 2;
        float g = (firstColor.g + secondColor.g) / 2;
        float b = (firstColor.b + secondColor.b) / 2;
        float a = 255f;
 
        Color expectedColor = new Color(r, g, b, a);
        return expectedColor;
    }
    */
 
    public void StartButtonClick()
    {
        SaveChosenCharacters();
        SceneManager.LoadScene("ChooseCharacter");
    }
 
    public void Return()
    {
        SceneManager.LoadScene("MapChoiceMenu");
    }
 
    void SaveChosenCharacters()
    {
        CharacterType.TYPE[] characterType = new CharacterType.TYPE[numberOfPlayers];
        for (int i = 0; i < selectedButtons.Count; i++)
        {
            characterType[i] = selectedButtons[i].GetComponent<CharacterType>().Type;
            PlayerPrefs.SetInt("Player" + i, (int)characterType[i]);
            Debug.Log( ((CharacterType.TYPE)PlayerPrefs.GetInt("Player" + i)) + " has been chosen ");
        } 
    }
 
#if !UNITY_IOS
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SceneManager.LoadScene("MapChoiceMenu");
        }
    }
#endif
 
 
}
 
 
[System.Serializable]
public class ChosenCharacterColors
{
    [SerializeField] List<ColorChoice> colorChoiceList;
    Color currentColor;
 
    public Color CurrentColor { get { return currentColor; } }
 
    /*
    public ChosenCharacterColors() 
    {
        NextColor();
    }
    */    
 
    public void NextColor()
    {
        for (int i = 0; i < colorChoiceList.Count; i++)
        {
            if (colorChoiceList[i].Used == false)
            {
                currentColor = colorChoiceList[i].Color;
            }
        }
    }
 
    public void UnlockColor(Color color)
    {
        colorChoiceList.Find(x => x.Color == color).Used = false;
    }    
 
    public void LockColor(Color color)
    {
        colorChoiceList.Find(x => x.Color == color).Used = true;
    }
 
}
 
[System.Serializable]
public class ColorChoice
{
    public Color Color;
    bool used = false;
    public bool Used { get { return used; } set { used = value; } }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎