简体   繁体   English

组织字典以交换数据的最佳方式是什么?

[英]What's the best way to organize a dictionary to exchange data?

In unity I have a dictionary where I keep a lot of data.团结起来,我有一本字典,里面保存着很多数据。 I'll need to access the data, read it, and sometimes overwrite it .我需要访问数据、读取数据,有时还需要覆盖它 I have it inside a class that's written more or less like我把它放在 class 里面,它或多或少像

public class myClass
{
    public int myNumber;
    public string myString;
}

public class MyData : MonoBehaviour
{
    public Dictionary<string, myClass> someData = new Dictionary<string, myClass>();

    private void Awake()
    {
        someData.Add("one", new myClass() { myNumber = 1, myString = "first" });
        someData.Add("two", new myClass() { myNumber = 2, myString = "second" });
        someData.Add("three", new myClass() { myNumber = 3, myString = "third" });
    }
}

To avoid having to call gameObject.GetComponent<MyData>().someData everytime I need the Dictionary's data (since I need it across many GameObjects and scripts) I was thinking about setting the Dictionary as static which would allow to reference it with MyData.someData (and hopefully having better performance):为了避免每次我需要字典的数据时都必须调用gameObject.GetComponent<MyData>().someData (因为我需要在许多 GameObjects 和脚本中使用它),我正在考虑将字典设置为static ,这将允许使用MyData.someData (并希望有更好的性能):

public static Dictionary<string, myClass> someData = new Dictionary<string, myClass>();

However, when I try accessing the Dictionary data in the Start method of other classes (that should therefore be executed after the Awake method of MyData ), MyData.someData is always null .但是,当我尝试在其他类的Start方法中访问 Dictionary 数据时(因此应该在MyDataAwake方法之后执行), MyData.someData始终是null I don't know if it has something to do with the calling order or if there's a problem with setting the Dictionary as static.我不知道这是否与调用顺序有关,或者将 Dictionary 设置为 static 是否有问题。 I'm not experienced enough to understand.我没有足够的经验来理解。

Is static the correct usage here for the Dictionary? static是字典的正确用法吗? Or are there better solutions?还是有更好的解决方案?

Moreover, if there's a Dictionary I only want to read but never modify, are there ways to define it so that it remains immutable?此外,如果有一本我只想阅读但从不修改的字典,有没有办法定义它以使其保持不变?

instantiate the dictionary in Awake.在 Awake 中实例化字典。 add items to the dictionary in Start.将项目添加到开始中的字典。 Awake is always called before start so this will guarantee your dictionary is initialized before trying to access it. Awake 总是在开始之前被调用,所以这将保证你的字典在尝试访问它之前被初始化。

Make the SomeData a static field and then you can call it as MyData.SomeData throughout your app.SomeData设为 static 字段,然后您可以在整个应用程序中将其称为MyData.SomeData You need to explicitly call the Awake() method in order to add the three MyClass instances to SomeData dictionary.您需要显式调用Awake()方法才能将三个MyClass实例添加到SomeData字典。 You can create a static constructor and call the Awake() inside it.您可以创建一个 static 构造函数并在其中调用Awake()

public class MyData : MonoBehaviour
{
    public static Dictionary<string, myClass> someData = new Dictionary<string, myClass>();

    static MyData()
    {
        Awake();
    }

    private static void Awake()
    {
        someData.Add("one", new myClass() { myNumber = 1, myString = "first" });
        someData.Add("two", new myClass() { myNumber = 2, myString = "second" });
        someData.Add("three", new myClass() { myNumber = 3, myString = "third" });
    }
}

There is a readonly dictionary in .NET 4.5... https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.readonlydictionary-2?redirectedfrom=MSDN&view=netcore-3.1 .NET 4.5 中有一个只读字典...

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

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