Created
July 7, 2014 08:04
-
-
Save haramakoto/b964c9cc45648b85a5a1 to your computer and use it in GitHub Desktop.
ローカルアプリ用の調整パラメータをPlayerPrefsに保存して取り出すやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ParamPrefsSetter : MonoBehaviour { | |
#region singleton | |
static ParamPrefsSetter instance; | |
public static ParamPrefsSetter Instance | |
{ | |
get { return instance; } | |
} | |
void Awake () { | |
if(instance) { | |
return; | |
} | |
instance = this; | |
} | |
#endregion | |
#region publicparams | |
public bool isEnabale=false; | |
#endregion | |
#region paramaters | |
//params | |
public float param1; | |
//Default Params | |
float param1Def = 0.0f; | |
//String Params | |
string param1St; | |
//keys | |
string key_param1="key_param1"; | |
#endregion | |
#region privateparams | |
GUIStyle style; | |
GUIStyleState _styleState; | |
bool isInputInterval=false; | |
#endregion | |
// Use this for initialization | |
void Start () { | |
loadFromPrefs(); | |
//style | |
style=new GUIStyle(); | |
_styleState=new GUIStyleState(); | |
style.fontSize=12; | |
_styleState.textColor=Color.white; | |
style.normal=_styleState; | |
} | |
float somefloat; | |
void OnGUI() { | |
if(isEnabale){ | |
GUI.Label(new Rect(10,10,30,10),"param1",style); | |
//input to string parameters | |
param1St = GUI.TextField(new Rect(50, 10, 50, 20), param1St, 25); | |
if(GUI.Button(new Rect(120,10,120,20),"Load Defaults"))setDefaults(); | |
if(GUI.Button(new Rect(120,40,120,20),"Save Params"))saveParameters(); | |
//input to origin parameters | |
param1=float.Parse(param1St); | |
} | |
} | |
public void saveParameters(){ | |
PlayerPrefs.SetFloat(key_param1,param1); | |
} | |
public void loadFromPrefs(){ | |
param1 = PlayerPrefs.GetFloat(key_param1); | |
setToString(); | |
} | |
void setToString(){ | |
param1St = param1.ToString(); | |
} | |
public void setDefaults(){ | |
param1 = param1Def; | |
setToString(); | |
} | |
// Update is called once per frame | |
void Update () { | |
if(Input.GetKey(KeyCode.G)){ | |
if(!isInputInterval){ | |
isEnabale=!isEnabale; | |
Screen.showCursor=isEnabale; | |
StartCoroutine(setInputInterval()); | |
} | |
} | |
} | |
IEnumerator setInputInterval(){ | |
isInputInterval=true; | |
yield return new WaitForSeconds(0.5f); | |
isInputInterval=false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment