如何从C#_ Unity中其他类的函数访问Dictionary [英] How to access Dictionary from other Class's function in C#_ Unity

查看:56
本文介绍了如何从C#_ Unity中其他类的函数访问Dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我不太了解如何继续获取附加到GameObjects的脚本中的元数据.如何在分支中看到此信息以在检查器中显示?

I am new to C# and I don't have much idea how to proceed to get metadata that is in a script that is attached to the GameObjects. How can I see this information in branch to display it in the Inspector?

namespace Speckle.ConnectorUnity
{
    /// <summary>
    /// This class gets attached to GOs and is used to store Speckle's metadata when sending / receiving
    /// </summary>
    public class SpeckleData : MonoBehaviour
    {
        public Dictionary<string, object> Data { get; set; } 
    }
}

您的答案会很有帮助,在此先感谢您.

Your answer would be of great help, thanks in advance.

推荐答案

您需要检查文档来检查您可以在检查器中看到的类型.

You need to check the documentation to check what are the types that you can see in the inspector.

如您所见,序列化是通过内部Unity序列化系统完成的;.NET的序列化功能不支持.因此,并非所有的c#类都可以在编辑器中显示为序列化字段.另一方面,您的字典是一个属性.检查您是否也无法在文档中序列化属性.

As you will be able to read, the serialization is done with an internal Unity serialization system; not with .NET's serialization functionality. So not all the c# classes are able to be shown in the editor as serialized fields. On the other hand your dictionary is a property. Check that you cannot serialize properties also in the docs.

我的建议是,如果要执行此操作,请使模板类在编辑器中显示为可序列化,并且将您知道的字段显示在编辑器中,作为类的变量散布在您的类中,例如这个:

My recomendation is that if you want to do that, make your template classes to be shown in the editor as serializable, and with the fields that you know can be shown in the editor spreaded in your class as variables of it, like this:

    [Serializable]
    class MyInspectorCoolClass {
        int myInt;
        string myString;
    }

    class MyMonoBehaviour: MonoBehaviour {
        public MyInspectorCoolClass myFields; //your fields will be displayed in the inspector
    }

因此,对于您的特定情况,您需要知道要序列化的 object 具体类型.如果您需要在检查器中显示一组我认为与字典有关的键,值对象,我建议您按照我的示例为检查器类型持有者类创建一个 List 这个:

So, for your specific case you would need to know the object concrete type to be serialized. In case you need to show in the inspector a set of key, value objects as I think you intend with your dictionary, I recomend that you make a List of your inspector typeholder class, following my example, like this:

class MyMonoBehaviour: MonoBehaviour {
    public List<MyInspectorCoolClass> myFields; 
}

或者是一个私有序列化字段,将在编辑器中显示,以作为要附加的引用,但对于代码中对变量的访问,请保留为私有.

Or a private serialize field, to be shown in the editor for a reference to be attached, but keep it private regarding the access to the variable in the code.

class MyMonoBehaviour: MonoBehaviour {
    [SerializeField]
    private MyInspectorCoolClass myFields; //your fields will be displayed in the inspector
}

以防万一,C#

In case you might be tempted, c# Tuples cannot be shown as serialized fields in the inspector neither.

这篇关于如何从C#_ Unity中其他类的函数访问Dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆