【问题标题】:How can I successfully store and load data from a game character?如何成功存储和加载游戏角色的数据?
【发布时间】:2019-11-17 20:51:26
【问题描述】:

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

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

如果你能告诉我哪里出了问题,我真的很感激。

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

【问题讨论】:

  • 添加调试信息,显示你检查了什么,你得到了什么等等,查看你的播放器首选项设置文件...
  • 一般来说:避免使用PlayerPrefs 存储敏感数据和用户进度.. 而是存储到服务器或文件并使用例如XML 或 JSON...
  • 一般来说:请按原样发布您的代码并在此处修复拼写错误...例如StartOnMouseDownOnMouseUp 都应该是大写,以便被 Unity 调用...publicprivate 但是不应该...... PublicPrivate 不作为关键字存在...

标签: c# unity3d serialization


【解决方案1】:

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

First PlayerPref 默认不支持布尔保存。

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

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

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

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

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

【讨论】:

  • 同意这可能就足够了,例如在onMouseUp ...顺便说一句,如果您希望Unity调用它,它应该是O的大写字母OnMouseDownOnMouseUp ...
猜你喜欢
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
相关资源
最近更新 更多