繁体   English   中英

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

[英]How can I successfully store and load data from a game character?

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

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

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

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);
        }
    }
}

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

First PlayerPref 默认不支持 boolean 保存。

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

这就是为什么每次您开始游戏时,您保存的所有数据都会被删除。 您可以尝试此解决方法来保存和加载 boolean 类型How to save bool to PlayerPrefs Unity

现在,您到底是如何决定在更新 function 时保存和加载数据的?,。 如果您的数据很大,这很糟糕,并且会产生巨大的性能成本。 你不能这样做。

相反,尝试制作存储数据并加载它的事件,比如一旦玩家进入触发框,数据就会被保存,当然如果保存了你不需要重新加载它,因为你必须在之后加载数据它被保存了?!

再一次,不要一直像这样在更新 Function 上读写,这是糟糕的设计。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM