如何为 3d 模型统一制作序列化字典?

How do I make a serialized dictionary in unity for 3d models?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public struct ImageInfo
{
    public Texture texture;
    public int width;
    public int height;
}

public class ImagesData : MonoBehaviour
{
    public ImageInfo[] images;

    public static Vector2 AspectRatio(float width, float height)
    {
        Vector2 scale = Vector2.one;
    }
}

这是为 2D 图像制作序列化字典的代码,如何让它接受 3D 模型?我希望它像照片中的那样。

This is the code for making a serialized dictionary for images which are 2D

不,不是。您显示的是 ImageInfo.

的序列化 array

对于您在图像中显示的内容,您可以简单地做

[Serializable]
public class ModeInfo
{
    public string Id;
    public GameObject Value;
}

public class ModelsDataManager : MonoBehaviour
{
    public ModeInfo[] InteractionModes;
}

或者,如果您希望进一步支持一些字典功能

public class ModelsDataManager : MonoBehaviour
{
    [SerializeField]
    private List<ModeInfo> InteractionModes;

    private bool TryGetInfo(string id, out ModeInfo value)
    {
        foreach (var info in InteractionModes)
        {
            if (info.Id == id)
            {
                value = info;
                return true;
            }
        }

        value = default;
        return false;
        
        // or same thing using Linq (needs "using System.Linq;")
        //value = InteractionModes.FirstOrDefault(info => info.Id == id);
        //return value != null;
    }
    
    public bool TryGetValue(string id, out GameObject value)
    {
        if (!TryGetInfo(id, out var info))
        {
            value = default;
            return false;
        }

        // note that just like in normal dictionaries the value can still be "null" though
        value = info.Value;
        return true;
    }

    public void AddOrOverwrite(string id, GameObject value)
    {
        if (!TryGetInfo(id, out var info))
        {
            info = new ModeInfo()
            {
                Id = id
            };

            InteractionModes.Add(info);
        }

        info.Value = value;
    }
}

如果您真的想要整个字典功能,包括键检查、无需迭代的更便宜的值访问 等我建议不要从头开始实施,而是使用现有的解决方案,例如