数据存储(PlayerPrefs)JSon数据生成和解析,CSV文件

1.PlayerPrefs存储数据

                                                                                                        PlayerPrefs常用方法

数据存储(PlayerPrefs)JSon数据生成和解析,CSV文件_第1张图片


数据存储(PlayerPrefs)JSon数据生成和解析,CSV文件_第2张图片

(1)搭建如图1-1所示UI界面(最终实现效果运行后点击白色Button会出现整个蓝色UI可以进行更改里面各控件的参数然后点击红色按钮保存设置,再次打开后就是上次改变后的值)


数据存储(PlayerPrefs)JSon数据生成和解析,CSV文件_第3张图片
图1-1

(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)数据存储脚本如下

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour{   

 //需要保存数据的关键词 键值对应 的建   

 const string loginName = "loginName";

//登录名 字符串   

 const string soundVolum = "soundVolum";//音量 float 0-1之间的一个浮点值   

 const string gamePoint = "gamePoint";//游戏关卡 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);//游戏关卡

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数据生成和解析

(1)先下载需要的JSon类库链接: http://pan.baidu.com/s/1gfDTqaZ 密码: caqn

添加在项目中如图2-1


数据存储(PlayerPrefs)JSon数据生成和解析,CSV文件_第4张图片
图2-1

(2)在Resources下添加一个txt格式的文本文本内容如下

[

{"mosterID":121,"MosterName":"frog"},

{"mosterID":122,"MosterName":"sheep"},

{"mosterID":123,"MosterName":"wolf"}

]

(3)创建脚本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

        {

            Debug.Log("mosterID=" + jsd[i]["mosterID"]);

            Debug.Log("Name=" + jsd[i]["MosterName"]);

   }

}}

(4)运行后就可将txt文本中的内容解析出来如图2-2


数据存储(PlayerPrefs)JSon数据生成和解析,CSV文件_第5张图片
图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文档中的内容

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 "";

}

}

你可能感兴趣的:(数据存储(PlayerPrefs)JSon数据生成和解析,CSV文件)