数据持久化-PlayerPrefs

一、基础方法

1、介绍持久化

将内存中的数据存储到硬盘中。

2、PalyerPrefs是一个存储玩家数据的公共类。

3、存储相关

键是唯一的,即使是存储不同类型的。

4、读取相关

取值和其参数介绍:

第二个参数是用来赋予默认值的。并不会存储到硬盘中。

判断是否存在该键:

删除数据

练习题:

第一题 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player1 : MonoBehaviour
{
    public string name;
    public int age;
    public int atk;
    public int def;
    void SaveData()
    {
        PlayerPrefs.SetString("name", this.name);
        PlayerPrefs.SetInt("age", this.age);
        PlayerPrefs.SetInt("atk", this.atk);
        PlayerPrefs.SetInt("def", this.def);
    }

    void ReadData()
    {
        name = PlayerPrefs.GetString("name", "未知名称");
        age = PlayerPrefs.GetInt("age", 20);
        atk = PlayerPrefs.GetInt("atk", 1);
        def = PlayerPrefs.GetInt("def", 1);
        print($"玩家{name}({age}岁)目前攻击力:{age},防御力:{def}");
    }
    private void Start()
    {
        SaveData();
        ReadData();
    }
}

二、存储位置

1、不同平台位置:

windows:

Android:

IOS:

2、保证数据不重复

练习题

PlayerPrefs主要作用

相关推荐
泉城老铁10 天前
VUE2实现加载Unity3d
前端·vue.js·unity3d
evamango12 天前
《Unity Shader入门精要》十一、让画面动起来
unity3d
Thomas游戏开发13 天前
Unity Android性能优化设置指南
前端框架·unity3d·游戏开发
Thomas游戏开发16 天前
Unity3D C#监听Button点击事件
前端·unity3d·游戏开发
谷宇.18 天前
【Unity3D实例-功能-拔枪】角色拔枪(三)IK的使用-紧握武器
游戏·unity·c#·unity3d·游戏开发·游戏编程·steam
evamango18 天前
《Unity Shader入门精要》十、高级纹理
unity3d
白色牙膏18 天前
从零到搞定:URP Lit彩色箱子材质的开发碎碎念
unity3d
Thomas游戏开发21 天前
博毅创为 Unity_0基础就业班
前端框架·unity3d·游戏开发
谷宇.22 天前
【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
游戏·unity·c#·游戏程序·unity3d·游戏开发·游戏编程