Unity中保存和读取数据的类---PlayerPrefs

 一、认识 PlayerPrefs 类

  1.PlayerPrefs 类的方法:

    SetInt();保存整型数据;

    SetString();保存字符串型数据;

    SetFloat();保存浮点型数据;

    GetInt();读取整形数据;         

    GetFlost();读取浮点型数据;

    GetString();读取字符串型数据;

  2.使用PlayerPrefs 中的方法

using Framework.Common.Singleton;
using System;
using UnityEngine;

public class MyPlayerPrefs :Singleton
{
    public override void Init()
    {
        base.Init();
    }

    /// 
    /// 读取
    /// 
    /// 
    /// 
    public T Read(string key)
    {
        T value = default(T);

        if (typeof(T) == typeof(int))
        {
            int _value = PlayerPrefs.GetInt(key);
            value = (T)Convert.ChangeType(_value, typeof(int));
        }
        else if (typeof(T) == typeof(string))
        {
            string _value = PlayerPrefs.GetString(key);
            value = (T)Convert.ChangeType(_value, typeof(string));
        }
        else if (typeof(T) == typeof(float))
        {
            float _value = PlayerPrefs.GetFloat(key);
            value = (T)Convert.ChangeType(_value, typeof(float));
        }

        return value;
    }

    public void Save(string key, T value)
    {
        if (typeof(T) == typeof(int))
        {
            int _value = (int)Convert.ChangeType(value, typeof(int));
            PlayerPrefs.SetInt(key, _value);
        }
        else if (typeof(T) == typeof(string))
        {
            string _value = (string)Convert.ChangeType(value, typeof(string));
            PlayerPrefs.SetString(key, _value);
        }
        else if (typeof(T) == typeof(float))
        {
            float _value = (float)Convert.ChangeType(value, typeof(float));
            PlayerPrefs.SetFloat(key, _value);
        }

        
    }

    public void Save()
    {
        PlayerPrefs.Save();
    }

}
using UnityEngine;
using Dean27_V4;
using UnityEngine.UI;

public class UIMyPlayerRresf : BaseUI
{
    private InputField inputFieldName;
    private InputField inputFieldExp;
    private InputField inputFieldLv;
    private Dropdown dropDownSex;

    private Text textName;
    private Text textLv;
    private Text textExp;
    private Text textSex;

    private void Awake()
    {
        EventTriggerListener _saveTrigger = EventTriggerListener.Get(transform.Find("SaveButton").gameObject);
        _saveTrigger.SetEventTriggerHandle(EnumTouchEventType.OnClick, Save);

        EventTriggerListener _readTrigger = EventTriggerListener.Get(transform.Find("ReadButton").gameObject);
        _readTrigger.SetEventTriggerHandle(EnumTouchEventType.OnClick, Read);

        inputFieldName = transform.Find("InputField_Name").GetComponent();
        inputFieldExp = transform.Find("InputField_Exp").GetComponent();
        inputFieldLv = transform.Find("InputField_Lv").GetComponent();
        dropDownSex = transform.Find("Dropdown_Sex").GetComponent();

        textName = transform.Find("Info/Name").GetComponent();
        textLv = transform.Find("Info/Lv").GetComponent();
        textExp = transform.Find("Info/Exp").GetComponent();
        textSex = transform.Find("Info/Sex").GetComponent();
    }

    private void Read(GameObject _listener, object _args, object[] _params)
    {
        textName.text = MyPlayerPrefs.Instance.Read<string>("姓名");
        textLv.text = MyPlayerPrefs.Instance.Read<int>("等级").ToString();
        textExp.text = MyPlayerPrefs.Instance.Read<float>("经验").ToString();
        textSex.text = MyPlayerPrefs.Instance.Read<string>("性别");
    }

    private void Save(GameObject _listener, object _args, object[] _params)
    {
        string name = inputFieldName.text;
        float exp = float.Parse(inputFieldExp.text);
        int lv = int.Parse(inputFieldLv.text);
        string sex = "男性";
        if (dropDownSex.value == 1)
        {
            sex = "女性";
        }

        MyPlayerPrefs.Instance.Save<string>("姓名", name);
        MyPlayerPrefs.Instance.Save<float>("经验", exp);
        MyPlayerPrefs.Instance.Save<int>("等级", lv);
        MyPlayerPrefs.Instance.Save<string>("性别", sex);

        MyPlayerPrefs.Instance.Save();

    }
}

  3.实际使用效果

    1.输入存储的信息,并且读取数据之后得到结果为:

      Unity中保存和读取数据的类---PlayerPrefs_第1张图片

    

    2.运行程序,直接读取信息:

Unity中保存和读取数据的类---PlayerPrefs_第2张图片

转载于:https://www.cnblogs.com/Dean27/p/7879254.html

你可能感兴趣的:(Unity中保存和读取数据的类---PlayerPrefs)