Advertisement
emonegarand

OptionsController,cs

Apr 21st, 2018
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5.  
  6. public class OptionsController : MonoBehaviour {
  7.  
  8.     public Slider volumeSlider;
  9.     public Slider difficultySlider;
  10.     public Slider controlSlider;
  11.  
  12.     private float percentage;
  13.    
  14.     private int maxValue; //max types, if gamepad not detected max is 1 not 2
  15.  
  16.     public Text percentText;
  17.     public Text difficultyLevel;
  18.     public Text controlText;
  19.     public Text ControlLeft;
  20.     public Text ControlRight;
  21.  
  22.     private Color inactive = Color.gray;
  23.     private Color active = Color.white;
  24.  
  25.     public LevelManager levelManager;
  26.     private MusicPlayer musicManager;
  27.     private GamePadManager gamePad;
  28.  
  29.    
  30.  
  31.  
  32.     // Use this for initialization
  33.     void Start()
  34.     {
  35.         levelManager = GameObject.FindObjectOfType<LevelManager>();
  36.         musicManager = GameObject.FindObjectOfType<MusicPlayer>();
  37.         gamePad = FindObjectOfType<GamePadManager>();
  38.  
  39.         volumeSlider.value = PlayerPrefsManager.GetMasterVolume();
  40.         difficultySlider.value = PlayerPrefsManager.GetDifficulty();
  41.         controlSlider.value = PlayerPrefsManager.GetControlType();
  42.  
  43.  
  44.     }
  45.  
  46.     // Update is called once per frame
  47.     void Update()
  48.     {
  49.         //if(gamePad.noGamepad)
  50.         //{
  51.         //    controlSlider.maxValue = 1;
  52.         //}
  53.         //else
  54.         //{
  55.         //    controlSlider.maxValue = 2;
  56.         //}
  57.  
  58.         //Is a gamepad detected?
  59.         if(gamePad.noGamepad)
  60.         {
  61.             maxValue = 1;
  62.         }
  63.         else { maxValue = 2; }
  64.        
  65.         // Activate or Inactivate Arrow
  66.         if (gamePad.controlType == 0)
  67.         {
  68.             ControlLeft.color = inactive;
  69.         }
  70.         else if (gamePad.controlType > 0)
  71.         {
  72.             ControlLeft.color = active;
  73.         }
  74.  
  75.         if (gamePad.controlType == maxValue)
  76.         {
  77.             ControlRight.color = inactive;
  78.         }
  79.        
  80.         else if (gamePad.controlType < maxValue)
  81.         {
  82.             ControlRight.color = active;
  83.         }
  84.        
  85.  
  86.  
  87.         percentage = Mathf.RoundToInt((volumeSlider.value / 1f) * 100);
  88.         percentText.text = percentage + "%";
  89.  
  90.         CheckDifficulty();
  91.         CheckControlType();
  92.        
  93.  
  94.         //PlayerPrefsManager.SetMasterVolume (volumeSlider.value);
  95.  
  96.         musicManager.ChangeVolume(volumeSlider.value);
  97.         if (musicManager == null)
  98.         {
  99.             return;
  100.         }
  101.     }
  102.     void CheckDifficulty()
  103.     {
  104.         if (difficultySlider.value == 1)
  105.         {
  106.             difficultyLevel.color = Color.white;
  107.             difficultyLevel.text = "Easy";
  108.         }
  109.         else if (difficultySlider.value == 2)
  110.         {
  111.             difficultyLevel.color = Color.white;
  112.             difficultyLevel.text = "Normal";
  113.         }
  114.         else if (difficultySlider.value == 3)
  115.         {
  116.             difficultyLevel.color = Color.white;
  117.             difficultyLevel.text = "Hard";
  118.         }
  119.         else if (difficultySlider.value == 4)
  120.         {
  121.             difficultyLevel.color = Color.red;
  122.             difficultyLevel.text = "Masochist";
  123.         }
  124.     }
  125.     void CheckControlType()
  126.     {
  127.         if (gamePad.controlType == 0)
  128.         {
  129.             controlText.text = "Mouse";
  130.         }
  131.         else if (gamePad.controlType == 1)
  132.         {
  133.             controlText.text = "keyboard";
  134.         }
  135.         else if (gamePad.controlType == 2)
  136.         {
  137.             controlText.text = "GamePad";
  138.         }
  139.     }
  140.     public void ControlLeftArrow()
  141.     {
  142.         if (gamePad.controlType > 0)
  143.         {
  144.             gamePad.controlType--;
  145.         }
  146.     }
  147.     public void ControlRightArrow()
  148.     {
  149.         if (!gamePad.noGamepad && gamePad.controlType < 2)
  150.         {
  151.             gamePad.controlType++;
  152.         }
  153.         else if (gamePad.noGamepad && gamePad.controlType < 1)
  154.         {
  155.             gamePad.controlType++;
  156.         }
  157.     }
  158.  
  159.     public void SaveAndExit()
  160.     {
  161.         PlayerPrefsManager.SetMasterVolume(volumeSlider.value);
  162.         PlayerPrefsManager.SetDifficulty(difficultySlider.value);
  163.         PlayerPrefsManager.SetControlType(gamePad.controlType);
  164.         levelManager.Loadlevel("01a Start");
  165.     }
  166.  
  167.     public void SetDefaults()
  168.     {
  169.         volumeSlider.value = 0.8f;
  170.         difficultySlider.value = 2f;
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement