1.PlayerPrefs存儲數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? PlayerPrefs常用方法
(1)搭建如圖1-1所示UI界面(最終實現(xiàn)效果運(yùn)行后點擊白色Button會出現(xiàn)整個藍(lán)色UI可以進(jìn)行更改里面各控件的參數(shù)然后點擊紅色按鈕保存設(shè)置匙瘪,再次打開后就是上次改變后的值)
(2)打開Ui的腳本OpenUI如下
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class openUI : MonoBehaviour{? ?
?GameObject _playprefsUI;? ? Button _openButton;??
? public void Awake()? ? {? ? ??
? _playprefsUI = transform.Find("PlayerPrefs1").gameObject;? ??
? ? _openButton = transform.Find("Button").GetComponent();
_openButton.onClick.AddListener(OpenUIFunc);
}
GameObject tempUI;
void OpenUIFunc()
{
tempUI = Instantiate(_playprefsUI) as GameObject;
tempUI.transform.parent = transform;
tempUI.transform.localPosition = Vector3.zero;
tempUI.transform.localScale = Vector3.one;
}}
(3)數(shù)據(jù)存儲腳本如下
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour{? ?
?//需要保存數(shù)據(jù)的關(guān)鍵詞 鍵值對應(yīng) 的建? ?
?const string loginName = "loginName";
//登錄名 字符串? ?
?const string soundVolum = "soundVolum";//音量 float 0-1之間的一個浮點值? ?
?const string gamePoint = "gamePoint";//游戲關(guān)卡 int? ??
Button _clossButton;? ??
Slider _soundVolum; ??
InputField _input;? ??
Text _gamepointshow;? ?
?public void Awake()? ??
{? ? ? ?
?_clossButton = transform.Find("Button").GetComponent();? ? ??
? _soundVolum = transform.Find("Slider").GetComponent();? ? ??
? _input = transform.Find("InputField").GetComponent();? ? ??
? _gamepointshow = transform.Find("Text").GetComponent();
_clossButton.onClick.AddListener(closeFunc);
}
void closeFunc()
{
PlayerPrefs.SetString(loginName,_input.text);//保存字符串
PlayerPrefs.SetFloat(soundVolum, _soundVolum.value);//保存浮點值
PlayerPrefs.SetInt(gamePoint, 5);//游戲關(guān)卡
Destroy(gameObject);
}
void Start()
{
//Get獲取失敗會返回空字符串
_input.text = PlayerPrefs.GetString(loginName);
_soundVolum.value = PlayerPrefs.GetFloat(soundVolum);
_gamepointshow.text = PlayerPrefs.GetInt(gamePoint).ToString();
//? string name = PlayerPrefs.GetString(loginName);
//? Debug.Log("name=" + name);
}
}
2.JSon數(shù)據(jù)生成和解析
(1)先下載需要的JSon類庫鏈接: http://pan.baidu.com/s/1gfDTqaZ 密碼: caqn
添加在項目中如圖2-1
(2)在Resources下添加一個txt格式的文本文本內(nèi)容如下
[
{"mosterID":121,"MosterName":"frog"},
{"mosterID":122,"MosterName":"sheep"},
{"mosterID":123,"MosterName":"wolf"}
]
(3)創(chuàng)建腳本LessonJsonparse
using UnityEngine;
using System.Collections;
using LitJson;
public class LessonJsonparse : MonoBehaviour {
// Use this for initialization
void Start () {
TextAsset m_text = Resources.Load("JSONText") as TextAsset;
string str = m_text.text;
// Debug.LogError("str="+str);
JsonData jsd = JsonMapper.ToObject(str);
// Debug.Log(jsd.Count);
for (int i = 0; i<jsd.Count; i++)
? ? ? ? {
? ? ? ? ? ? Debug.Log("mosterID=" + jsd[i]["mosterID"]);
? ? ? ? ? ? Debug.Log("Name=" + jsd[i]["MosterName"])靶壮;
? ?}
}}
(4)運(yùn)行后就可將txt文本中的內(nèi)容解析出來如圖2-2
3.CSV文件
(1)將以下文本放在excel表格中另存為csv文檔
id,name,pricegold
10001,30分鐘VIP激活,500
10002,糧食10000,1000
10003,木材10000,2000
10004,石材10000,3000
10005,鐵礦10000,4000
10006,銀幣10000,5000
10007,1小時加速,1000
10008,12小時攻擊加成,1000
10009,新手寶箱,1000
(2)將csv文檔放在Resources下
(3)添加腳本就能獲取csv文檔中的內(nèi)容
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class LessonGetCsv : MonoBehaviour {
string[][] Array;
void Start () {
TextAsset binAssent = Resources.Load("CSV1") as TextAsset;
string [] Temp_lineArray=binAssent.text.Split("\r"[0]);
Debug.Log(Temp_lineArray.Length);
Array = new string[Temp_lineArray.Length][];
for (int i = 0; i < Temp_lineArray.Length; ++i)
{
Array[i] = Temp_lineArray[i].Split(',');
}
string getname = GetDataByIdAndName(10001, "name");
Debug.Log(getname);
}
string GetDataByIdAndName(int id,string strTitle)
{
if (Array.Length<=0)
{
return "";}
int nRow = Array.Length;
int nCol = Array[0].Length;
for (int i = 1; i < nRow; i++)
{
string strID = string.Format("\n{0}",id);
if (Array[i][0]==strID)
{
for (int j = 0; j < nCol; j++)
{
if (Array[0][j]==strTitle)
{
return Array[i][j];? ? ? ? ? ? ? ? ? ? }
}
}
}
return "";
}
}