unity Scriptable对象在构建中不起作用

unity Scriptable object not working in bulid

嗨,我是 unity 和 C# 的新手,我正在制作我的第一款游戏。我在其中有一个名为 "sss" 的 Scriptable 对象。它包含硬币、速度、跳跃力等值。它在编辑器中工作正常但在我构建它之后它不会 work.what 我的意思是它不会工作是在玩家在游戏中死亡后取决于他们活了多久他们去时会得到一定数量的金币他们会在商店看到这些硬币显示为 UI,他们可以将它们花在速度和跳跃提升等方面。这在编辑器中有效,但在构建中无效。有谁知道这是为什么吗?这是我的脚本对象中的代码

using UnityEngine;

[CreateAssetMenu(fileName = "data",menuName = "sss",order = 1)]
public class sss : ScriptableObject
{
   public float coins = 0f;
   public float speed = 10f;
   public float jump = -9.81f;
   public bool set = true;
   public int face = 1;

}

这是我用来显示硬币浮动的代码

coinstext.text = sss.coins.ToString();

这是用于购买东西的整个商店经理脚本

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Security;
using UnityEngine;
using UnityEngine.UI;

public class storemanager : MonoBehaviour
{
    public sss sss;
    public Text coinstext;
    public Slider sliders;
    public Slider sliderj;
    public int maxcap = 30;
    public int maxcapj = 500;
    public void Start()
    {


    coinstext.text = sss.coins.ToString();

    }

public void speedbuy()
{


    if (sss.coins >= 5 && sss.speed < maxcap)
    {
        sss.speed += 1;
        sss.buyspeed = true;
        sss.coins -= 5;
        sliders.value += 1;
        coinstext.text = sss.coins.ToString();
    }
}

public void jumpbuy()
{


    if (sss.coins >= 7 && sss.jump < maxcapj)
    {
        sss.jump += 10;
        sss.buyjump = true;
        sss.coins -= 7;
        sliderj.value += 10;
        coinstext.text = sss.coins.ToString();

    }
}
}

虽然 UnityEditor 中 ScriptableObject 中的更改是持久的,但在 build![=19 中它们不是持久的 =]

When you use the Editor, you can save data to ScriptableObjects while editing and at run time because ScriptableObjects use the Editor namespace and Editor scripting. In a deployed build, however, you can’t use ScriptableObjects to save data, but you can use the saved data from the ScriptableObject Assets that you set up during development.

所以在构建之后,资产中的数据有点 baked 并且您可以在运行时更改它,但是在重新启动应用程序时,您将始终重新加载数据构建。


您需要将数据存储在设备本身的本地文件中,例如使用 Json 喜欢

using UnityEngine;
using System.IO;
...

public class sss : ScriptableObject
{
    public float coins = 0f;
    public float speed = 10f;
    public float jump = -9.81f;
    public bool set = true;
    public int face = 1;

    private const string FILENAME = "sss.dat";

    public void SaveToFile()
    {
        var filePath = Path.Combine(Application.persistentDataPath, FILENAME);

        if(!File.Exists(filePath))
        {
            File.Create(filePath);
        }
       
        var json = JsonUtility.ToJson(this);
        File.WriteAllText(filePath, json);
    }
        

    public void LoadDataFromFile()
    {
        var filePath = Path.Combine(Application.persistentDataPath, FILENAME);

        if(!File.Exists(filePath))
        {
            Debug.LogWarning($"File \"{filePath}\" not found!", this);
            return;
        }

        var json = File.ReadAllText(filePath);
        JsonUtility.FromJsonOverwrite(json, this);
    }
}

然后在您的管理器脚本中调用加载和保存方法,例如

public class storemanager : MonoBehaviour
{
    public sss sss;

    public Text coinstext;
    public Slider sliders;
    public Slider sliderj;
    public int maxcap = 30;
    public int maxcapj = 500;

    public void Start()
    {
        sss.LoadFromFile();

        coinstext.text = sss.coins.ToString();
    }

    // Called when the application is going to quit
    private void OnApplicationQuit()
    {
        sss.SaveToFile();
    }

注意:在智能手机上打字,但我希望思路清晰