国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化之PlayerPrefs的使用

這篇具有很好參考價值的文章主要介紹了Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化之PlayerPrefs的使用。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

數(shù)據(jù)持久化

PlayerPrefs相關(guān)

PlayerPrefs是Unity游戲引擎中的一個類,用于在游戲中存儲和訪問玩家的偏好設(shè)置和數(shù)據(jù)。它可以用來保存玩家的游戲進(jìn)度、設(shè)置選項、最高分?jǐn)?shù)等信息。PlayerPrefs將數(shù)據(jù)存儲在本地文件中,因此可以在游戲重新啟動時保持?jǐn)?shù)據(jù)的持久性。

//PlayerPrefs的數(shù)據(jù)存儲 類似于鍵值對存儲 一個鍵對應(yīng)一個值
//提供了存儲3種數(shù)據(jù)的方法 int float string
//鍵: string類型 
//值:int float string 對應(yīng)3種API

PlayerPrefs.SetInt("myAge", 18);
PlayerPrefs.SetFloat("myHeight", 177.5f);
PlayerPrefs.SetString("myName", "TonyChang");

//直接調(diào)用Set相關(guān)方法 只會把數(shù)據(jù)存到內(nèi)存里
//當(dāng)游戲結(jié)束時 Unity會自動把數(shù)據(jù)存到硬盤中
//如果游戲不是正常結(jié)束的 而是崩潰 數(shù)據(jù)是不會存到硬盤中的
//只要調(diào)用該方法 就會馬上存儲到硬盤中
PlayerPrefs.Save();

//PlayerPrefs是有局限性的 它只能存3種類型的數(shù)據(jù)
//如果你想要存儲別的類型的數(shù)據(jù) 只能降低精度 或者上升精度來進(jìn)行存儲
bool sex = true;
PlayerPrefs.SetInt("sex", sex ? 1 : 0);

//如果不同類型用同一鍵名進(jìn)行存儲 會進(jìn)行覆蓋
PlayerPrefs.SetFloat("myAge", 20.2f);

//注意 運(yùn)行時 只要你Set了對應(yīng)鍵值對
//即使你沒有馬上存儲Save在本地
//也能夠讀取出信息

//int
int age = PlayerPrefs.GetInt("myAge");
print(age);
//前提是 如果找不到myAge對應(yīng)的值 就會返回函數(shù)的第二個參數(shù) 默認(rèn)值
age = PlayerPrefs.GetInt("myAge", 100);
print(age);

//float
float height = PlayerPrefs.GetFloat("myHeight", 1000f);
print(height);

//string
string name = PlayerPrefs.GetString("myName");
print(name);

//第二個參數(shù) 默認(rèn)值 對于我們的作用
//就是 在得到?jīng)]有的數(shù)據(jù)的時候 就可以用它來進(jìn)行基礎(chǔ)數(shù)據(jù)的初始化

//判斷數(shù)據(jù)是否存在
if( PlayerPrefs.HasKey("myName") )
{
    print("存在myName對應(yīng)的鍵值對數(shù)據(jù)");
}

//刪除指定鍵值對
PlayerPrefs.DeleteKey("myAge");
//刪除所有存儲的信息
PlayerPrefs.DeleteAll();

PlayerPrefs中存儲的數(shù)據(jù)存儲在哪里?

PC端: PlayerPrefs 存儲在 HKCU\Software[公司名稱][產(chǎn)品名稱] 項下的注冊表中
其中公司和產(chǎn)品名稱是 在“Project Settings”中設(shè)置的名稱。

安卓: /data/data/包名/shared_prefs/pkg-name.xml

PlayerPrefs中數(shù)據(jù)的唯一性,PlayerPrefs中數(shù)據(jù)的唯一性是由key決定的,不同的key決定了不同的數(shù)據(jù),同一個項目中如果不同數(shù)據(jù)key相同會造成數(shù)據(jù)丟失,要保證數(shù)據(jù)名稱命名的唯一性規(guī)則。

優(yōu)點(diǎn):使用簡單

缺點(diǎn):存儲數(shù)據(jù)類型有限、安全性差(直接找到在設(shè)備上的存儲的位置查看設(shè)置)

PlayerPrefs存儲工具類:

為了方便進(jìn)行數(shù)據(jù)的存儲,使用PlayerPrefs中進(jìn)行存儲方法的設(shè)置的存??!

主要實(shí)現(xiàn)功能是數(shù)據(jù)的讀和數(shù)據(jù)的取~ 通過反射進(jìn)行數(shù)據(jù)類型的獲取,利用PlayerPrefs進(jìn)行數(shù)據(jù)存儲。

using System;
using System.Collections;
using System.Reflection;
using UnityEngine;

namespace Framwork
{
    /// <summary>
    /// Playerprefs 存儲類
    /// </summary>
    public class PlayerPrefsManager
    {
        private static PlayerPrefsManager instance=new PlayerPrefsManager();

        public static PlayerPrefsManager Instance => instance;

        private PlayerPrefsManager()
        {
           
        }

        /// <summary>
        /// 存取數(shù)據(jù)的方法
        /// </summary>
        /// <param name="obj">數(shù)據(jù)實(shí)體</param>
        /// <param name="name">數(shù)據(jù)名稱</param>
        public void SaveData(object data, string keyName)
        {
            Type type = data.GetType();
            FieldInfo[] infos = type.GetFields();
            string tempKey="null";
            FieldInfo tempInfo = null;
            for (int i = 0; i < infos.Length; i++)
            {
                //獲取數(shù)據(jù)數(shù)據(jù)類型
                tempInfo = infos[i];
                Debug.Log("Types==="+tempInfo);
                //類的名字+類的類型 + 數(shù)據(jù)內(nèi)容名字+數(shù)據(jù)類型
                //作為存儲的keyName鍵
                tempKey = keyName + "_" + type.Name + "_" + tempInfo.Name
                            + "_" + tempInfo.FieldType.Name;
                SaveValue(tempInfo.GetValue(data),tempKey);
            }
            //進(jìn)行值的獲取
           //tempInfo.GetValue(data);
            PlayerPrefs.Save();
        }
        /// <summary>
        /// 讀取數(shù)據(jù)的類型
        /// </summary>
        /// <param name="type">要讀取的數(shù)據(jù)類型</param>
        /// <param name="name">要讀取的數(shù)據(jù)名稱</param>
        /// <returns>返回數(shù)據(jù)實(shí)體</returns>
        public object LoadData(Type type, string name)
        {
            //獲取數(shù)據(jù)中的類型
            FieldInfo[] infos = type.GetFields();
            //創(chuàng)建存儲數(shù)據(jù)信息的實(shí)體
            object data = Activator.CreateInstance(type);
            string tempName = null;
            FieldInfo tempInfo = null;
            for (int i = 0; i < infos.Length; i++)
            {
                tempInfo = infos[i];//數(shù)據(jù)結(jié)構(gòu)中的數(shù)據(jù)名稱
                tempName = name + "_" + type.Name + "_" +tempInfo.Name+"_"
                    +tempInfo.FieldType.Name;//數(shù)據(jù)結(jié)構(gòu)中的數(shù)據(jù)名稱類型
                //裝載的容器  容器中的數(shù)據(jù) 
                //進(jìn)行數(shù)據(jù)裝載
                tempInfo.SetValue(data,LoadValue(tempInfo.FieldType,tempName));
            }
            return data;
        }

        /// <summary>
        /// 進(jìn)行具體的類型數(shù)據(jù)的存儲
        /// </summary>
        /// <param name="data"></param>
        /// <param name="keyName"></param>
        private void SaveValue(object value, string keyName)
        {
            Type fieldType = value.GetType();
            if (fieldType == typeof(int))
            {
                Debug.Log("存儲int"+value);
                PlayerPrefs.SetInt(keyName,(int)value);
            }else if (fieldType == typeof(float))
            {
                Debug.Log("存儲float"+value);
                PlayerPrefs.SetFloat(keyName,(float)value);
            }else if (fieldType == typeof(string))
            {
                Debug.Log("存儲string"+value);
                PlayerPrefs.SetString(keyName,value.ToString());
            }
            //對于List存儲的設(shè)置
            //根據(jù)存儲的字段類型和IList是否是父子關(guān)系
            else if(typeof(IList).IsAssignableFrom(fieldType))
            {
                //父類裝子類
                IList list=value as IList;
                //存儲元素數(shù)量
                PlayerPrefs.SetInt(keyName,list.Count);
                Debug.Log("存儲List長度為"+list.Count);
                int index = 0;
                foreach (var obj in list)
                {
                    //存儲list列表中元素內(nèi)容
                    //命名形式是 list名字+索引編號
                    //遞歸調(diào)用存儲
                    SaveValue(obj,keyName+index);
                    index++;
                }
            }else if (typeof(IDictionary).IsAssignableFrom(fieldType))
            {
                IDictionary dictionary = value as IDictionary;
                //存儲數(shù)據(jù)個數(shù)
                PlayerPrefs.SetInt(keyName,dictionary.Count);
                Debug.Log("存儲Dic長度為"+dictionary.Count);
                int index = 0;
                foreach (var key in dictionary.Keys)
                {
                    //存儲鍵
                    SaveValue(key,keyName+"_key_"+index);
                    //存儲值 
                    SaveValue(dictionary[key],keyName+"_value_"+index);
                    index++;
                }
            }//自定義數(shù)據(jù)類型的存儲 進(jìn)行解析
            else 
            {
                SaveData(value,keyName);
            }
        }

        private object LoadValue(Type type, string name)
        {
            if (type == typeof(int))
            {
                return PlayerPrefs.GetInt(name,0);
            }else if (type == typeof(float))
            {
                return PlayerPrefs.GetFloat(name,0.0f);
            }else if (type == typeof(string))
            {
                return PlayerPrefs.GetString(name,"");
            }else if (typeof(IList).IsAssignableFrom(type))
            {
                //讀取列表
                int count = PlayerPrefs.GetInt(name);
                IList tempList=Activator.CreateInstance(type) as IList;
                for (int i = 0; i < count; i++)
                {
                    //獲取List中存儲元素的類型 type.GetGenericArguments()[0]
                    tempList.Add(LoadValue(type.GetGenericArguments()[0],name+i));
                }
                return tempList;
            }else if (typeof(IDictionary).IsAssignableFrom(type))
            {
                //進(jìn)行對字典的讀取
                int count = PlayerPrefs.GetInt(name);
                IDictionary tempDictionary=Activator.CreateInstance(type) as IDictionary;
                for (int i = 0; i < count; i++)
                {
                    tempDictionary.Add(LoadValue(type.GetGenericArguments()[0], name + "_key_" + i),
                        LoadValue(type.GetGenericArguments()[1], name + "_value_" + i));
                }
                return tempDictionary;
            }
            else
            {
                //讀取自定義類成員的設(shè)置
                return LoadData(type, name);
            }
        }
    }
}

附:

測試腳本文章來源地址http://www.zghlxwxcb.cn/news/detail-746807.html

using System.Collections.Generic;
using UnityEngine;

namespace Framwork
{
    //注意:
    //1 自定義數(shù)據(jù)結(jié)構(gòu)類型中要有有效的無參構(gòu)造函數(shù)
    
    public class PlayerInfo
    {
        public int age;
        public string name;
        public float height;
        public int sex;//0是女 1是男

        public ItemInfo ItemInfo;
        //list存儲測試
        public List<int> list;
        public Dictionary<int, string> dic;
        
    }

    public class ItemInfo
    {
        public int stu_no;//學(xué)號
        public int stu_class;//班級

        public ItemInfo()
        {
            
        }
        public ItemInfo(int no,int classNo)
        {
            stu_no = no;
            stu_class = classNo;
        }
    }
    /// <summary>
    /// 測試類
    /// </summary>
    public class TestPlayerPrefsTest:MonoBehaviour
    {
        private PlayerInfo playerInfo;
        private PlayerInfo playerInfo1;
        private void Start()
        {
             //讀取數(shù)據(jù)
             playerInfo = new PlayerInfo();         
            // Type fieldType = playerInfo.GetType();
             playerInfo.age = 18;
             playerInfo.name = "TonyChang";
             playerInfo.height = 175.8f;
             playerInfo.sex = 1;
             playerInfo.ItemInfo = new ItemInfo(2001, 2);

             playerInfo.list = new List<int>(){1,5,6,8};
             playerInfo.dic = new Dictionary<int, string>();
             playerInfo.dic.Add(1,"Tony");
             playerInfo.dic.Add(2,"Jeny");
             playerInfo.dic.Add(3,"JayChou");

             //進(jìn)行數(shù)據(jù)保存
             PlayerPrefsManager.Instance.SaveData(playerInfo,"Player1");
             
             playerInfo1 = PlayerPrefsManager.Instance.LoadData(typeof(PlayerInfo), "Player1") as PlayerInfo;

             Debug.Log("age=="+playerInfo1.age);
             Debug.Log("name=="+playerInfo1.name);
             Debug.Log("sex=="+playerInfo1.sex);
             Debug.Log("List[1]=="+playerInfo1.list[1]);
             Debug.Log("Dic[1]=="+playerInfo1.dic[1]);
        }
    }
}

到了這里,關(guān)于Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化之PlayerPrefs的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化Json

    Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化Json

    json是國際通用語言,可以跨平臺(游戲,軟件,網(wǎng)頁,不同OS)使用, json語法較為簡單,使用更廣泛。json使用鍵值對來存儲。 認(rèn)識json文件 //注意字典類型存儲時,鍵是以string類型存儲的 需要添加 “” Excel轉(zhuǎn)換為JSON文件: 使用網(wǎng)站來轉(zhuǎn)換:bejson 挖坑-----》開發(fā)一個工具,

    2024年02月05日
    瀏覽(25)
  • Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化XML文件(1)

    Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化XML文件(1)

    Xml是可拓展標(biāo)記語言,一種文件格式。我們使用xml來完成對數(shù)據(jù)持久化的存儲。等待我們有一程序運(yùn)行結(jié)束之后,將內(nèi)存中的數(shù)據(jù)進(jìn)行保存,(保存在硬盤/服務(wù)器)實(shí)現(xiàn)對數(shù)據(jù)的持久化存儲。 xml文件的讀取和保存以及修改 要點(diǎn): XMl文件的加載 XML文件節(jié)點(diǎn)的查找訪問 XML文件

    2024年02月05日
    瀏覽(26)
  • Unity筆記:數(shù)據(jù)持久化的幾種方式

    主要方法: ScriptableObject PlayerPrefs JSON XML 數(shù)據(jù)庫(如Sqlite) PlayerPrefs 存儲的數(shù)據(jù)是 全局共享 的,它們存儲在用戶設(shè)備的本地存儲中,并且可以被應(yīng)用程序的所有部分訪問。這意味著,無論在哪個場景、哪個腳本中,只要是同一個應(yīng)用程序中的代碼,都可以讀取和修改 Playe

    2024年02月19日
    瀏覽(23)
  • 【Unity學(xué)習(xí)日記03】數(shù)據(jù)持久化

    【Unity學(xué)習(xí)日記03】數(shù)據(jù)持久化

    這一篇只能說寫了一部分,并沒有把Unity里數(shù)據(jù)持久化的操作講完整,之后可能是學(xué)到一點(diǎn)就記一點(diǎn)的模式。 數(shù)據(jù)持久化就是將內(nèi)存中的數(shù)據(jù)模型轉(zhuǎn)換為存儲模型,以及將存儲模型轉(zhuǎn)換為內(nèi)存中的數(shù)據(jù)模型的統(tǒng)稱。 人話版:將游戲數(shù)據(jù)存儲到硬盤,硬盤中數(shù)據(jù)讀取到游戲中,

    2024年02月12日
    瀏覽(17)
  • 【Unity】二進(jìn)制文件 數(shù)據(jù)持久化(修改版)【個人復(fù)習(xí)筆記/有不足之處歡迎斧正/侵刪】

    ???????? 變量的本質(zhì)都是二進(jìn)制 ,在內(nèi)存中都以字節(jié)的形式存儲著,通過sizeof方法可以看到常用變量類型占用的字節(jié)空間長度( 1byte = 8bit,1bit(位)不是0就是1 ) ? ? ? ? 二進(jìn)制文件讀寫的本質(zhì): 將各類型變量轉(zhuǎn)換為字節(jié)數(shù)組,將字節(jié)數(shù)組直接存儲到文件中 ,不僅可以節(jié)

    2024年04月17日
    瀏覽(25)
  • 「學(xué)習(xí)筆記」可持久化線段樹

    「學(xué)習(xí)筆記」可持久化線段樹

    可持久化數(shù)據(jù)結(jié)構(gòu) (Persistent data structure) 總是可以保留每一個歷史版本,并且支持操作的不可變特性 (immutable)。 主席樹全稱是可持久化權(quán)值線段樹,給定 (n) 個整數(shù)構(gòu)成的序列 (a) ,將對于指定的閉區(qū)間 (left[l, rright]) 查詢其區(qū)間內(nèi)的第 (k) 小值。 圖片來自 (texttt{OI-w

    2024年02月02日
    瀏覽(23)
  • ?「學(xué)習(xí)筆記」可持久化線段樹?

    ?「學(xué)習(xí)筆記」可持久化線段樹?

    可持久化數(shù)據(jù)結(jié)構(gòu) (Persistent data structure) 總是可以保留每一個歷史版本,并且支持操作的不可變特性 (immutable)。 主席樹全稱是可持久化權(quán)值線段樹,給定 (n) 個整數(shù)構(gòu)成的序列 (a) ,將對于指定的閉區(qū)間 (left[l, rright]) 查詢其區(qū)間內(nèi)的第 (k) 小值。 圖片來自 (texttt{OI-w

    2024年02月02日
    瀏覽(18)
  • 「學(xué)習(xí)筆記」可持久化線段樹?

    「學(xué)習(xí)筆記」可持久化線段樹?

    可持久化數(shù)據(jù)結(jié)構(gòu) (Persistent data structure) 總是可以保留每一個歷史版本,并且支持操作的不可變特性 (immutable)。 主席樹全稱是可持久化權(quán)值線段樹,給定 (n) 個整數(shù)構(gòu)成的序列 (a) ,將對于指定的閉區(qū)間 (left[l, rright]) 查詢其區(qū)間內(nèi)的第 (k) 小值。 圖片來自 (texttt{OI-w

    2024年02月02日
    瀏覽(20)
  • Unity之?dāng)?shù)據(jù)持久化——Json

    JavaScript對象簡譜(JavaScript Object Notation) json是國際通用的一種輕量級的數(shù)據(jù)交換格式,主要在網(wǎng)絡(luò)通訊中用于傳輸數(shù)據(jù),或本地數(shù)據(jù)存儲和讀取,易于人閱讀和編寫,同時也易于機(jī)器解析和生成,并有效地提升網(wǎng)絡(luò)傳輸效率 游戲中可以把游戲數(shù)據(jù)按照J(rèn)son的格式標(biāo)準(zhǔn)存儲在

    2023年04月20日
    瀏覽(20)
  • 【Unity】數(shù)據(jù)持久化路徑Application.persistentDataPath

    【Unity】數(shù)據(jù)持久化路徑Application.persistentDataPath

    今天突然想到這個路徑Application.persistentDataPath,熱更的重要路徑,該文件夾可讀可寫,在移動端唯一一個可讀寫操作的文件夾。 移動端可以將本地的資源(資源MD5值配置表)等一些文件放到StreamingAssets文件夾下,通過Copy到persistentDataPath下與服務(wù)器的版本文件配置表作比對,

    2023年04月10日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包