在应用安装 Unity 后清除本地存储



我是 Unity 的新手。我想清除所有localstorage并在安装或更新应用程序时缓存。那么我该如何检查应用程序是否已安装或正在更新。以前的数据会导致应用崩溃。

我知道如何手动清除数据,但我想通过应用程序进行清除。

我知道如何清除本地存储。PlayerPrefs.DeleteAll(),如何检查应用程序是否正在更新或重新安装。

正在更新

好吧,至少检查您是否更新了应用程序,您可以存储和比较Application.version

此函数返回应用程序的当前版本。这是只读的。要在 Unity 中设置版本号,请转到→播放器→编辑项目设置,然后打开其他设置选项卡。

public class VersionCheck : MonoBehaviour
{
private void Awake()
{
var version = PlayerPrefs.GetString("Version", string.Empty);
if (string.IsNullOrWhiteSpace(version))
{
// Probably not more to do since there is no stored data apparently
// Just to be sure you could still do
PlayerPrefs.DeleteAll();
// => THIS IS THE FIRST RUN
PlayerPrefs.SetString("Version", Application.version);
}
else 
{
if(version != Application.version)
{
// => THIS IS A VERSION MISMATCH -> UPDATED
PlayerPrefs.DeleteAll();
PlayerPrefs.SetString("Version", Application.version);
}
// else
//{
//     // Otherwise it could either mean you re-installed the same version 
//     // or just re-started the app -> There should be no difference between these two in behavior of your app
//}
}
}
}

重新安装

重新安装:有关系吗?实际上,如果您安装相同的版本,那么运行再次安装的代码或再次运行以前存在的代码之间应该没有区别。

因此,如果需要,我建议为用户添加一个选项来清除数据。

最新更新