Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using UnityEngine.UI;
  7.  
  8. public class SettingsController : MonoBehaviour {
  9.  
  10.     public const string MUTE_INT = "mute";
  11.     public const string TEXT_F = "text";
  12.     public GameObject sound;
  13.     public Text text;
  14.     public Image image;
  15.  
  16.     public void RestorePreferences()
  17.     {
  18.         SetMuted(GetIsMuted());
  19.         SetText(GetIsText());
  20.     }
  21.  
  22.     public void SetText(bool texted)
  23.     {
  24.  
  25.         PlayerPrefs.SetString(TEXT_F, texted ?  : 0);
  26.  
  27.     }
  28.  
  29.     public void SetMuted(bool muted)
  30.     {
  31.         PlayerPrefs.SetInt(MUTE_INT, muted ? 1 : 0);
  32.        
  33.         AudioListener.pause = muted;
  34.         if (DEBUG_ON)
  35.         Debug.LogFormat("SetMuted({0})", muted);
  36.     }
  37.  
  38.    
  39.     public bool GetIsMuted()
  40.     {
  41.         return PlayerPrefs.GetInt(MUTE_INT, 0) == 1;
  42.     }
  43.     public bool GetIsText()
  44.     {
  45.         return PlayerPrefs.GetInt(MUTE_INT, 0) == 1;
  46.     }
  47.  
  48.     public void Toggle_Changed(bool newValue)
  49.     {
  50.         sound.SetActive(newValue);
  51.     }
  52.    
  53.     public void DoSoundToggle()
  54.     {
  55.         if (AudioListener.volume > 0)
  56.         {
  57.            SoundOn();
  58.         }
  59.         else
  60.         {
  61.            
  62.            SoundOff();
  63.         }
  64.     }
  65.  
  66.     private void SoundOff()
  67.     {
  68.        
  69.         AudioListener.volume = 1;
  70.        
  71.     }
  72.  
  73.     private void SoundOn()
  74.     {
  75.         AudioListener.volume = 0;
  76.        
  77.     }
  78.  
  79.     /*    public void SetVolume(float volume)
  80.         {
  81.  
  82.             volume = Mathf.Clamp(volume, 0, 1);
  83.  
  84.             PlayerPrefs.SetFloat(VOLUME_F, volume);
  85.             AudioListener.volume = volume;
  86.         }
  87.  
  88.         public float GetVolume()
  89.         {
  90.             return Mathf.Clamp(PlayerPrefs.GetFloat(VOLUME_F, 1), 0, 1);
  91.         }*/
  92.  
  93.  
  94.     [System.Serializable]
  95.     public class Position
  96.     {
  97.         private bool DEBUG_ON = true;
  98.  
  99.     }
  100.     public void Save()
  101.     {
  102.         Position position = new Position();
  103.        
  104.        
  105.  
  106.         if (Directory.Exists(Application.dataPath + "/saves"))
  107.         {
  108.             Directory.CreateDirectory(Application.dataPath + "/saves");
  109.             FileStream fs = new FileStream(Application.dataPath + "/saves/save.sv", FileMode.Create);
  110.             BinaryFormatter formatter = new BinaryFormatter();
  111.             formatter.Serialize(fs, formatter);
  112.             fs.Close();
  113.         }
  114.  
  115.     }
  116.  
  117.     public void Load()
  118.     {
  119.         if (Application.isPlaying)
  120.         {
  121.             if (File.Exists(Application.dataPath + "/saves/save.sv"))
  122.             {
  123.                 FileStream fs = new FileStream(Application.dataPath + "/saves/save.sv", FileMode.Open);
  124.                 BinaryFormatter formatter = new BinaryFormatter();
  125.  
  126.                 try
  127.                 {
  128.                     Position position = (Position)formatter.Deserialize(fs);
  129.  
  130.                 }
  131.                 catch (System.Exception e)
  132.                 {
  133.                     Debug.Log(e.Message);
  134.                 }
  135.                 finally
  136.                 {
  137.                     fs.Close();
  138.                 }
  139.             }
  140.             else
  141.             {
  142.                 Application.LoadLevel(0);
  143.             }
  144.         }
  145.  
  146.     }
  147.  
  148.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement