简体   繁体   English

尝试在不继承 MonoBehaviour 的脚本中存储值

[英]Trying to store values in a script that does not inherit from MonoBehaviour

I'm trying to make it so that once you click "Accept" on a UI button that it will store all the values from the quest into the following script:我正在尝试这样做,一旦您在 UI 按钮上单击“接受”,它就会将任务中的所有值存储到以下脚本中:

[System.Serializable]
public class DataHolder
{
    //Active Quest
    public bool isActive;
    public string title;
    public string description;
    public int goldReward;
    public QuestGoal goal;
    
}

The script that stores it is here:存储它的脚本在这里:

public class QuestGiver : MonoBehaviour
{
    public Quest quest;
    
    public DataHolder data;
        
    public GameObject questWindow;
    public Text titleText;
    public Text descriptionText;
    public Text goldText;
    
    public void OpenQuestWindow()
    {
        Debug.Log("Quest Window Opened");
        questWindow.SetActive(true);
        titleText.text = quest.title;
        descriptionText.text = quest.description;
        goldText.text = quest.goldReward.ToString();
    }
    
    public void AcceptQuest()
    {
        questWindow.SetActive(false);
        data.isActive = true;
        data.title = quest.title;
        data.description = quest.description;
        data.goldReward = quest.goldReward;
    }
}

and the script that is in the scene so that when a button is pressed (Tab) it will pop up a UI that will show what the current quest is, which should be stored in the first "DataHolder" script:以及场景中的脚本,当按下按钮(Tab)时,它会弹出一个 UI,显示当前任务是什么,它应该存储在第一个“DataHolder”脚本中:

public class QuestChecker : MonoBehaviour
{
    DataHolder data;
    
    public GameObject questWindow;
    public Text titleText;
    public Text descriptionText;
    public Text goldText;
    
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            CheckQuest();
        }
    }
    
    public void CheckQuest()
    {
        questWindow.SetActive(true);
        
            titleText.text = data.title;
            descriptionText.text = data.description;
            goldText.text = data.goldReward.ToString();
        
        
    }
    
    public void CloseQuestWindow()
    {
        questWindow.SetActive(false);
    }

}

The reason I want it stored in "DataHolder" is because I want it to be stored between scenes.我希望它存储在“DataHolder”中的原因是因为我希望它存储在场景之间。 I believe my main problem is not knowing how to properly reference the "DataHolder" script as Unity gives me the warning:我相信我的主要问题是不知道如何正确引用“DataHolder”脚本,因为 Unity 给了我警告:

Assets\\Scripts\\QuestChecker.cs(8,13): warning CS0649: Field 'QuestChecker.data' is never assigned to, and will always have its default value null Assets\\Scripts\\QuestChecker.cs(8,13): 警告 CS0649: 字段 'QuestChecker.data' 从未分配给,并且其默认值始终为 null

Any help would be appreciated.任何帮助,将不胜感激。 I'm pretty new to programming, please let me know if this won't work for what I want it to do and need to do it another way or if you need more info.我对编程很陌生,如果这对我想要的东西不起作用并且需要以另一种方式来做,或者如果您需要更多信息,请告诉我。 Thanks谢谢

If all you want to do is store some data between scenes, you could simply do this:如果您只想在场景之间存储一些数据,您可以简单地执行以下操作:

public static class DataHolder
{
    //Active Quest
    public static bool isActive;
    public static string title;
    public static string description;
    public static int goldReward;
    public static QuestGoal goal;    
}

Then just reference it as DataHolder.isActive etc. Note that you're not actually "saving" the data anywhere (serialising).然后只需将其引用为DataHolder.isActive等。请注意,您实际上并没有在任何地方“保存”数据(序列化)。 The SerializableAttribute just indicates that the item CAN be serialised. SerializableAttribute 只是表明该项目可以被序列化。 But, this WILL store data between scenes.但是,这将在场景之间存储数据。

Is it the best way to do so?这是最好的方法吗? Well, if it's just some simple data you need to store between scenes, then there's nothing wrong with doing it this way.好吧,如果只是需要在场景之间存储一些简单的数据,那么这样做并没有错。 And it'll probably fit in nicely with what you've already programmed.它可能与您已经编程的内容非常吻合。

You're never actually creating an instance of DataHolder.您实际上从未创建过 DataHolder 的实例。

Somewhere along the line you need a new DataHolder() that is assigned to data before trying to set any of its properties.沿着这条线的某个地方,您需要一个new DataHolder()在尝试设置其任何属性之前分配给data

You need to create a relationship between QuestGiver and QuestChecker您需要在QuestGiverQuestChecker之间创建关系

eg.例如。

public class QuestChecker : MonoBehaviour
{
    public QuestGiver giver;
}

Now drag your QuestGiver game object onto the QuestChecker.giver field in the inspector.现在将您的QuestGiver游戏对象拖到检查器中的QuestChecker.giver字段上。 Then you are able to access your saved data.然后您就可以访问您保存的数据。

public void CheckQuest()
{
    titleText.text = saver.data.title;
}

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

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