简体   繁体   English

Unity:无法在 Inspector 中编辑公共 Sprite[][]

[英]Unity: Can't edit public Sprite[][] in the Inspector

Everytime I want to play my Game with this script attached:每次我想用这个脚本玩我的游戏时:

//...

    public Sprite[][] ObjectTypestobuy;
    public Sprite[] Characters;     
    public Sprite[] Helmets;
    public Sprite[] Weapons;
    public Sprite[] Mantles;
    public Sprite[] Shields;

void Start()
{
        ObjectTypestobuy[0] = Characters; //This is the line where the error message points to
        ObjectTypestobuy[1] = Helmets;
        ObjectTypestobuy[2] = Weapons;
        ObjectTypestobuy[3] = Mantles;
        ObjectTypestobuy[4] = Shields;
}

...it throws the following error: NullReferenceException: ...它抛出以下错误:NullReferenceException:

> Object reference not set to an instance of an object (wrapper
> stelemref) object:stelemref (object,intptr,object) Shop_Handler.Start
> () (at Assets/Shop_Handler.cs:88)

The line which is marked as error is this one:标记为错误的行是这一行:

ObjectTypestobuy[0] = Characters;

I think the problem is, because it says I should edit public Sprite[][] ObjectTypestobuy;我认为问题是,因为它说我应该编辑public Sprite[][] ObjectTypestobuy; in the Inspector.在检查员。 But I can't find it in the inspector.但是我在检查器中找不到它。

Any help would be precated.任何帮助都会被预先处理。

Before you can set a value in an array, the array must be created.在数组中设置值之前,必须先创建数组。

void Start()
{
        ObjectTypestobuy = new Sprite[5][10]; // for example
        ObjectTypestobuy[0] = Characters; //this is the error line
        ObjectTypestobuy[1] = Helmets;
        ObjectTypestobuy[2] = Weapons;
        ObjectTypestobuy[3] = Mantles;
        ObjectTypestobuy[4] = Shields;
}

Without creating the array, you can't put anything in it.如果不创建数组,则不能在其中放置任何内容。 You get the null exception because you're trying to put something in a non-existent object.您会收到 null 异常,因为您试图将某些内容放入不存在的对象中。

Unfortunately, you haven't actually initialized the array yet.不幸的是,您还没有真正初始化数组。 This type of array is called a "Jagged" array.这种类型的数组称为“锯齿状”数组。

So, the answer is here in this page here from Microsoft.所以,答案就在这里在这页这里从微软。

int[][] jaggedArray = new int[3][];

And then using initializers, the array can be filled:然后使用初始化器,可以填充数组:

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

It's unfortunate that Unity doesn't serialize Dictionary collections.遗憾的是 Unity 不会序列化 Dictionary 集合。 Given that limitation, a common work around to accomplish what I think you're trying to achieve is to do the following:鉴于此限制,完成我认为您要实现的目标的常见工作是执行以下操作:

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

[Serializable]
public struct InventoryCollection
{
    public string Name;
    public List<Sprite> Sprites;
}

public class Inventory: MonoBehaviour
{
    public List<InventoryCollection> ObjectTypesToBuy = new List<InventoryCollection>();
}

You'll notice that now you can enter the items directly in to the Inspector window in Unity, and the "name" field will also name the items in the Inspector, as a convenience.您会注意到,现在您可以直接在 Unity 中的 Inspector 窗口中输入项目,为了方便起见,“名称”字段还将为 Inspector 中的项目命名。

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

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