unity程序转成微信小游戏以后File.WriteAllText和File.ReadAllText由于路径问题不生效。
这个时候需要采用微信自己的存储路径来把用户信息保存下来。
private void LoadPlayerData()
{
#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
if (File.Exists(filePath))
{
playerDataString = File.ReadAllText(filePath);
m_PlayerData = CMSimpleJson.CMSimpleJson.DeserializeObject<PlayerData>(playerDataString);
}文章来源地址https://www.toymoban.com/news/detail-695475.html
else
{
m_PlayerData = new PlayerData();
WritePlayerData();
Analysis.Instance.TrackRegist("regist");
}
#elif UNITY_WEBGL
playerDataString = fs.ReadFileSync(WX.env.USER_DATA_PATH +"/*.txt", "utf-8");//*文件名字
if (playerDataString != "")
{
m_PlayerData = CMSimpleJson.CMSimpleJson.DeserializeObject<PlayerData>(playerDataString);
}
else
{
m_PlayerData = new PlayerData();
WritePlayerData();
}
#endif文章来源:https://www.toymoban.com/news/detail-695475.html
}
private void WritePlayerData()
{
string playerData = CMSimpleJson.CMSimpleJson.SerializeObject(m_PlayerData);
#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
string filePath = Path.Combine(Constants.DATA_PATH, Constants.GAMESAVE_PATH);
File.WriteAllText(filePath, playerData);
#elif UNITY_WEBGL
WXFileSystemManager fs = WX.GetFileSystemManager();
fs.WriteFileSync(WX.env.USER_DATA_PATH +"/*.txt", playerData, "utf-8");
#endif
}
到了这里,关于unity转微信小游戏数据存储的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!