对于简单的存档,可以利用Playerpref永久保存,通过保存游戏角色的位置来实现,然后当点击继续游戏时通过start初始化得到保存的数据,重新开始游戏则不读取数据。大体思路如下:界面上有退出游戏按钮,点击时弹出窗口,有保存和不保存两个按钮,如下所示
function windowsave()
{
if(GUILayout.Button("保存"))
{ cls=false;
//由于只能保存int,string,float型,所以将坐标分开保存
PlayerPrefs.SetFloat("str1",transform.position.x);
PlayerPrefs.SetFloat("str2",transform.position.y);
PlayerPrefs.SetFloat("str3",transform.position.z);
PlayerPrefs.SetInt("str5",PlayerPrefs.GetInt("str4"));
}
if(GUILayout.Button("不保存"))
{ cls=false;
}
}
同时在start函数写下如下:
function Start ()
{
if(PlayerPrefs.GetInt("state"))
{
transform.position = Vector3(PlayerPrefs.GetFloat("str1"),PlayerPrefs.GetFloat("str2"),PlayerPrefs.GetFloat("str3"));
}
}
unity圣典上面介绍如下:
在游戏会话中储存和访问游戏存档。
可以理解为持久化储存,还可以理解为游戏存档, 玩RPG游戏的时候肯定会有游戏存档 存档中就会记录玩家以前游戏的过程,这些都是以数据的形式存在PlayerPrefs中的。
在Web模式,PlayerPrefs存储在Mac OS X的二进制文件 ~/Library/Preferences/Unity/WebPlayerPrefs中和Windows的 %APPDATA%\Unity\WebPlayerPrefs中,一个游戏存档文件对应一个web播放器URL并且文件大小被限制为1MB。如果超出这个限制,SetInt、SetFloat和SetString将不会存储值并抛出一个PlayerPrefsException。
Class Functions类函数
-
Sets the value of the preference identified by key.
设置由key确定的参数值。 -
Returns the value corresponding to key in the preference file if it exists.
如果存在,返回偏好文件中key对应的值。 -
Sets the value of the preference identified by key.
设置由key确定的参数值。 -
Returns the value corresponding to key in the preference file if it exists.
如果存在,返回游戏存档文件中key对应的值。 -
Sets the value of the preference identified by key.
设置由key确定的参数值。 -
Returns the value corresponding to key in the preference file if it exists.
如果存在,返回游戏存档文件中key对应的值。 -
Returns true if key exists in the preferences.
如果key在游戏存档中存在,返回true。 -
Removes key and its corresponding value from the preferences.
从游戏存档中删除key和它对应的值。 -
Removes all keys and values from the preferences. Use with caution.
从偏好中删除所有key。请谨慎使用。 -
Writes all modified preferences to disk.
写入所有修改参数到硬盘。