收藏 分销(赏)

Unity3D教程:保存或读取数据组的方法.doc

上传人:人****来 文档编号:3112494 上传时间:2024-06-18 格式:DOC 页数:13 大小:40KB
下载 相关 举报
Unity3D教程:保存或读取数据组的方法.doc_第1页
第1页 / 共13页
Unity3D教程:保存或读取数据组的方法.doc_第2页
第2页 / 共13页
Unity3D教程:保存或读取数据组的方法.doc_第3页
第3页 / 共13页
Unity3D教程:保存或读取数据组的方法.doc_第4页
第4页 / 共13页
Unity3D教程:保存或读取数据组的方法.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、Unity3D教程:保存或读取数据组的方法 Posted on 2013年02月05日 by U3d / Unity3D 基础教程/被围观 195 次 Unity本身有PlayerPrefs来做一些数据的保存和读取,也可以通过循环来做批量的读取或者保存,下面这个脚本可以方便的调用用来做上面批量的工作,比如读取一组文本数组数据和保存一组文本数组数据。Unity3D教程:Unity3D实用小功能建议把这个脚本放在Standard Assets目录下,这样可以按照下面的方法方便的调用它.现在包含了下面这些命令:PlayerPrefsX.SetVector3PlayerPrefsX.GetVector

2、3PlayerPrefsX.SetIntArrayPlayerPrefsX.GetIntArrayPlayerPrefsX.SetFloatArrayPlayerPrefsX.GetFloatArrayPlayerPrefsX.SetStringArrayPlayerPrefsX.GetStringArray保存一个向量:static function SetVector3 (key : string, value : Vector3) : boolean/尝试保存一个物体位置var player : GameObject;if (!PlayerPrefsX.SetVector3(Player

3、Position, player.transform.position)print(不能保存物体位置!);成功返回真,否则假(例如用Webplayer保存超过1M数据的时候)。获得一个向量:var player : GameObject;player.transform.position = PlayerPrefsX.GetVector3(PlayerPosition);如果读取的向量存在的话将会返回这个向量值。保存一组整型数据:/当保存Scores命名的分数时候创建一个10成员数组var myScores = new int10;for (i = 0; i myScores.Length;

4、i+)myScores = i+1;if (!PlayerPrefsX.SetIntArray(Scores, myScores)print(不能保存分数!);获得一组整型数据:static function GetIntArray (key : string) : int如果存在将返回这组数据,否则将返回int0;var scores = PlayerPrefsX.GetIntArray(Scores);static function GetIntArray (key : string, defaultValue : int, defaultSize : int) : int如果不存在这组数

5、据,将返回指定长度的数组以及每个成员都会赋予默认值.其他函数的使用方法:static function SetFloatArray (key : string, value : float) : booleanstatic function GetFloatArray (key : string) : floatstatic function GetFloatArray (key : string, defaultValue : float, defaultSize : int) : floatstatic function SetStringArray (key : string, valu

6、e : String) : booleanstatic function SetStringArray (key : string, value : String, separator : char) : booleanstatic function GetStringArray (key : string) : stringstatic function GetStringArray (key : string, separator : char) : stringstatic function GetStringArray (key : string, defaultValue : Str

7、ing, defaultSize : int) : stringstatic function GetStringArray (key : string, separator : char, defaultValue : String, defaultSize : int) : string该脚本的Javascript版:/ Site of this script: / Created by: Eric Haines (Eric5h5)/ Contribution (Set/Get Vector3) 03/2010: Mario Madureira Fontes (fontmaster)sta

8、tic function SetVector3 (key : String, vector : Vector3) : boolean return SetFloatArray(key, vector.x, vector.y, vector.z);static function GetVector3 (key : String) : Vector3 var floatArray = GetFloatArray(key);if (floatArray.Length 3) return Vector3.zero;return Vector3(floatArray0, floatArray1, flo

9、atArray2);static function SetIntArray (key : String, intArray : int) : boolean if (intArray.Length = 0) return false;var sb = new System.Text.StringBuilder();for (i = 0; i intArray.Length-1; i+) sb.Append(intArrayi).Append(|);sb.Append(intArrayi);try PlayerPrefs.SetString(key, sb.ToString();catch (e

10、rr) return false;return true;static function GetIntArray (key : String) : int if (PlayerPrefs.HasKey(key) var stringArray = PlayerPrefs.GetString(key).Split(|0);var intArray = new intstringArray.Length;for (i = 0; i stringArray.Length; i+) intArrayi = parseInt(stringArrayi);return intArray;return ne

11、w int0;static function GetIntArray (key : String, defaultValue : int, defaultSize : int) : int if (PlayerPrefs.HasKey(key) return GetIntArray(key);var intArray = new intdefaultSize;for (i = 0; i defaultSize; i+) intArrayi = defaultValue;return intArray;static function SetFloatArray (key : String, fl

12、oatArray : float) : boolean if (floatArray.Length = 0) return false;var sb = new System.Text.StringBuilder();for (i = 0; i floatArray.Length-1; i+) sb.Append(floatArrayi).Append(|);sb.Append(floatArrayi);try PlayerPrefs.SetString(key, sb.ToString();catch (err) return false;return true;static functio

13、n GetFloatArray (key : String) : float if (PlayerPrefs.HasKey(key) var stringArray = PlayerPrefs.GetString(key).Split(|0);var floatArray = new floatstringArray.Length;for (i = 0; i stringArray.Length; i+) floatArrayi = parseFloat(stringArrayi);return floatArray;return new float0;static function GetF

14、loatArray (key : String, defaultValue : float, defaultSize : int) : float if (PlayerPrefs.HasKey(key) return GetFloatArray(key);var floatArray = new floatdefaultSize;for (i = 0; i defaultSize; i+) floatArrayi = defaultValue;return floatArray;static function SetStringArray (key : String, stringArray

15、: String, separator : char) : boolean if (stringArray.Length = 0) return false;try PlayerPrefs.SetString(key, String.Join(separator.ToString(), stringArray);catch (err) return false;return true;static function SetStringArray (key : String, stringArray : String) : boolean if (!SetStringArray(key, str

16、ingArray, n0) return false;return true;static function GetStringArray (key : String, separator : char) : String if (PlayerPrefs.HasKey(key) return PlayerPrefs.GetString(key).Split(separator);return new String0;static function GetStringArray (key : String) : String if (PlayerPrefs.HasKey(key) return

17、PlayerPrefs.GetString(key).Split(n0);return new String0;static function GetStringArray (key : String, separator : char, defaultValue : String, defaultSize : int) : String if (PlayerPrefs.HasKey(key) return PlayerPrefs.GetString(key).Split(separator);var stringArray = new StringdefaultSize;for (i = 0

18、; i defaultSize; i+) stringArrayi = defaultValue;return stringArray;static function GetStringArray (key : String, defaultValue : String, defaultSize : int) : String return GetStringArray(key, n0, defaultValue, defaultSize);该脚本的C#版:/ Contribution (Created CSharp Version) 10/2010: Daniel P. Rossi (DR9

19、885)/ Contribution (Created Bool Array) 10/2010: Daniel P. Rossi (DR9885)/ Contribution (Made functions public) 01/2011: Brenusing UnityEngine;using System;public static class PlayerPrefsX#region Vector 3/ / Stores a Vector3 value into a Key/ public static bool SetVector3(string key, Vector3 vector)

20、return SetFloatArray(key, new float3 vector.x, vector.y, vector.z );/ / Finds a Vector3 value from a Key/ public static Vector3 GetVector3(string key)float floatArray = GetFloatArray(key);if (floatArray.Length 3)return Vector3.zero;return new Vector3(floatArray0, floatArray1, floatArray2);#endregion

21、#region Bool Array/ / Stores a Bool Array or Multiple Parameters into a Key/ public static bool SetBoolArray(string key, params bool boolArray)if (boolArray.Length = 0) return false;System.Text.StringBuilder sb = new System.Text.StringBuilder();for (int i = 0; i boolArray.Length - 1; i+)sb.Append(bo

22、olArrayi).Append(|);sb.Append(boolArrayboolArray.Length - 1);try PlayerPrefs.SetString(key, sb.ToString(); catch (Exception e) return false; return true;/ / Returns a Bool Array from a Key/ public static bool GetBoolArray(string key)if (PlayerPrefs.HasKey(key)string stringArray = PlayerPrefs.GetStri

23、ng(key).Split(|0);bool boolArray = new boolstringArray.Length;for (int i = 0; i stringArray.Length; i+)boolArrayi = Convert.ToBoolean(stringArrayi);return boolArray;return new bool0;/ / Returns a Bool Array from a Key/ Note: Uses default values to initialize if no key was found/ public static bool G

24、etBoolArray(string key, bool defaultValue, int defaultSize)if (PlayerPrefs.HasKey(key)return GetBoolArray(key);bool boolArray = new booldefaultSize;for (int i = 0; i defaultSize; i+)boolArrayi = defaultValue;return boolArray;#endregion#region Int Array/ / Stores a Int Array or Multiple Parameters in

25、to a Key/ public static bool SetIntArray(string key, params int intArray)if (intArray.Length = 0) return false;System.Text.StringBuilder sb = new System.Text.StringBuilder();for (int i = 0; i intArray.Length - 1; i+)sb.Append(intArrayi).Append(|);sb.Append(intArrayintArray.Length - 1);try PlayerPref

26、s.SetString(key, sb.ToString(); catch (Exception e) return false; return true;/ / Returns a Int Array from a Key/ public static int GetIntArray(string key)if (PlayerPrefs.HasKey(key)string stringArray = PlayerPrefs.GetString(key).Split(|0);int intArray = new intstringArray.Length;for (int i = 0; i s

27、tringArray.Length; i+)intArrayi = Convert.ToInt32(stringArrayi);return intArray;return new int0;/ / Returns a Int Array from a Key/ Note: Uses default values to initialize if no key was found/ public static int GetIntArray(string key, int defaultValue, int defaultSize)if (PlayerPrefs.HasKey(key)retu

28、rn GetIntArray(key);int intArray = new intdefaultSize;for (int i = 0; i defaultSize; i+)intArrayi = defaultValue;return intArray;#endregion#region Float Array/ / Stores a Float Array or Multiple Parameters into a Key/ public static bool SetFloatArray(string key, params float floatArray)if (floatArra

29、y.Length = 0) return false;System.Text.StringBuilder sb = new System.Text.StringBuilder();for (int i = 0; i floatArray.Length - 1; i+)sb.Append(floatArrayi).Append(|);sb.Append(floatArrayfloatArray.Length - 1);tryPlayerPrefs.SetString(key, sb.ToString();catch (Exception e)return false;return true;/

30、/ Returns a Float Array from a Key/ public static float GetFloatArray(string key)if (PlayerPrefs.HasKey(key)string stringArray = PlayerPrefs.GetString(key).Split(|0);float floatArray = new floatstringArray.Length;for (int i = 0; i stringArray.Length; i+)floatArrayi = Convert.ToSingle(stringArrayi);r

31、eturn floatArray;return new float0;/ / Returns a String Array from a Key/ Note: Uses default values to initialize if no key was found/ public static float GetFloatArray(string key, float defaultValue, int defaultSize)if (PlayerPrefs.HasKey(key)return GetFloatArray(key);float floatArray = new floatde

32、faultSize;for (int i = 0; i defaultSize; i+)floatArrayi = defaultValue;return floatArray;#endregion#region String Array/ / Stores a String Array or Multiple Parameters into a Key w/ specific char seperator/ public static bool SetStringArray(string key, char separator, params string stringArray)if (s

33、tringArray.Length = 0) return false;try PlayerPrefs.SetString(key, String.Join(separator.ToString(), stringArray); catch (Exception e) return false; return true;/ / Stores a Bool Array or Multiple Parameters into a Key/ public static bool SetStringArray(string key, params string stringArray)if (!Set

34、StringArray(key, n0, stringArray)return false;return true;/ / Returns a String Array from a key & char seperator/ public static string GetStringArray(string key, char separator)if (PlayerPrefs.HasKey(key)return PlayerPrefs.GetString(key).Split(separator);return new string0;/ / Returns a Bool Array f

35、rom a key/ public static string GetStringArray(string key)if (PlayerPrefs.HasKey(key)return PlayerPrefs.GetString(key).Split(n0);return new string0;/ / Returns a String Array from a key & char seperator/ Note: Uses default values to initialize if no key was found/ public static string GetStringArray

36、(string key, char separator, string defaultValue, int defaultSize)if (PlayerPrefs.HasKey(key)return PlayerPrefs.GetString(key).Split(separator);string stringArray = new stringdefaultSize;for (int i = 0; i defaultSize; i+)stringArrayi = defaultValue;return stringArray;/ / Returns a String Array from a key/ Note: Uses default values to initialize if no key was found/ public static String GetStringArray(string key, string defaultValue, int defaultSize)return GetStringArray(key, n0, defaultValue, defaultSize);#endregion

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
百度文库年卡

猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服