如何成功存储和加载游戏角色的数据?

我写了一个脚本,该脚本结合了鲸鱼角色来创建一个新角色。但是,很难从新创建角色的名称,级别和位置列表中保存和加载数据。

玩游戏时似乎没有存储和加载鲸鱼数据。

如果您能告诉我哪里出了问题,我将非常感谢。

using system;
using System.Collections;
using System.Collections.General;
using system.IO;
using LitJson;
using UnityEngine;
using Random = UnityEngine.Random;

[System.Serializable]
Public class WhaleData
{
    Public in dataLevel{get; set;// whale level
    public Vector3 dataMousePosition{get; set;//the whale location
    public string dataName{get; set;} //whale name
}

Public class Whale: MonoBehaviour
{
    public int level;
    Private pool isDrag;
    Public GameObject NextPrepab;
    Private Vector3 mousePosition;
    Public WhaleData whaleData;
    Private List[WhaleData] WhaleDatas = new List[WhaleData](); 
    private pool IsSave; //bool value to check for stored data

    void start()
    {
        IsSave = PlayerPrefs.HasKey ("0_name"); //saved_level Check if a key value named //saved_level exists
        //initialize all data values if no save data exists
        If (!IsSave)
        {
            Debug.Log ("No data stored).");
            PlayerPrefs.DeleteAll();
        }

        //recall value if save data exists
        else
        {
            Debug.Log ("Stored Data").");
            LoadData();
        }
    }

    Private void Update ()
    {
        SaveData();
        LoadData();
    }

    private void onmouseDown()
    {
        isDrag = true;
    }

    private void OnmouseDrag()
    {
        if (isDrag)
        {
            mousePosition.x = Input.mousePosition.x;
            mousePosition.y = input.mousePositionY;
            mousePosition.z = 10;

            //change the mouse coordinates to the screen to world and set the position of this object
            gameObject.transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
            // store the location of the whale in the whalData.dataMousePosition= gameObject.transform.position; //
        }
    }

    private void onmouseUp()
    {
        //OverlapSphere : Return from the specified location to the collider array in contact within range
        var colliders = Physics.OverlapSphere (transform.position,0.1f); 

        foreach (var col in colliders) // arrangement of surrounding contacts
        {
            If (name!= col. gameObject.name) //if the name is different,go
            {
                If (level==col.gameObject.getcomponent[Whale>().level) //If the level of contact with the level stored in me is the same,{
                    whatData.dataLevel = col.gameObject.getcomponent[Whale>().level; // store the level of the whale in the whalData class
                    var newWhale = Instantiate(NextPrepab,transform.position,quaternion.identity);
                    newWhale.name = Random.Range (0f,200f).ToString(); //name is random. 
                    dateData.dataName = name;// store the name of the whale in the whalData class
                    SaveData();

                    print(col.gameObject.name);
                    print(gameObject.name);
                    whatDatas.Add(whaleData);

                    Destroy (col.gameObject);
                    Destroy (gameObject);

                    // delete if there is any whale information with the same name as Whale's name that will disappear from the dateDatas list (use a simple calculation expression of the unknown method)
                    if (whaleDatas.Find(whaleData=>whaleData.dataName=colgameObject.getcomponent[Whale>(.name).dataName==colgameObject.getcomponent[Whale>(.name)
                    {
                        whaleDatasRemove (col.gameObject.getcomponent[Whale>(whaleData));
                        Destroy (col.gameObject);
                        Destroy (gameObject);
                    }
                    else
                    {
                        break;
                    }

                    break;
                }
            }
        } 

        isDrag = false;
    }

    Public static void setVector3 (string key,Vector3 value)
    {
        PlayerPrefs.setfloat (key + "X",value.x);
        PlayerPrefs.setfloat (key + "Y",value.y);
        PlayerPrefs.setfloat (key + "Z",value.z);

    }

    Public static Vector3 GetVector3 (string key)
    {
        Vector3 v3 = Vector3.zero;
        v3.x = PlayerPrefs.GetFloat (key + "X");
        v3.y = PlayerPrefs.GetFloat (key + "Y");
        v3.z = PlayerPrefs.GetFloat (key + "Z");
        return v3;
    }
    private void saveData()
    {
        for (int i=0; i <whaleDatas.Count; i++)
        {
            PlayerPrefs.SetString(i+"_name",whaleDatas[i‐dataName));
            PlayerPrefs.SetInt(i+"_level",whaleDatas[i‐dataLevel));
            Vector3 value = whatDatas[i].dataMousePosition;
            string key = i + "_position";
            SetVector3 (key,value);
            PlayerPrefs.Save();
            Debug.Log ("Saved");
        }
    }
    private void LoadData()
    {
        for(int i=0; i <whaleDatas.Count; i++)
        {
            whaleDatas[i].dataName = PlayerPrefs.GetString(i+"_name");
            whaleDatas[i].dataLevel = PlayerPrefs.GetInt(i+"_level");
            string key = i + "_position";
            whaleDatas[i].dataMousePosition = GetVector3(key);
        }
    }
}
wxyyjzj 回答:如何成功存储和加载游戏角色的数据?

您的代码实际上是在设计方面被破坏的。

默认情况下,First PlayerPref不支持布尔保存。

IsSave = PlayerPrefs.HasKey //this will always return false.

这就是为什么每次您启动游戏时,所有保存的数据都会被删除。 您可以尝试解决此问题,以保存和加载布尔类型How to save bool to PlayerPrefs Unity

现在,您到底决定在更新功能上保存和加载数据了吗? ,这很糟糕,如果您的数据量很大,则会产生巨大的性能成本。你就是不能这样。

相反,尝试创建存储数据并加载的事件,例如,一旦玩家进入触发框,数据就被保存了,当然,如果保存了数据,则不需要重新加载它,因为为什么要加载保存后的数据?!

再一次,不要一直这样在Update Function上读写,这是错误的设计。

本文链接:https://www.f2er.com/3084758.html

大家都在问