简体   繁体   English

无法统一引用游戏对象的 2D 数组

[英]Unable to reference 2D array of game objects in unity

I'm creating chess in unity, and I want to create the chess board using a 2D array of cells.我正在统一创建国际象棋,并且我想使用 2D 单元格数组创建棋盘。 I was able to create an array of pieces and could reference them with their respective prefabs in Unity.我能够创建一个片段数组,并可以在 Unity 中使用它们各自的预制件来引用它们。 But I have no option to reference the 2D array.但我没有选择引用二维数组。

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

public class gameManager : MonoBehaviour
{
    public GameObject[,] cell = new GameObject[7,7];
    public GameObject[] Wpawn = new GameObject[7];
    public GameObject[] Bpawn  = new GameObject[7];
    public GameObject[] Wrook = new GameObject[1];
    public GameObject[] Brook = new GameObject[1];
    public GameObject[] Wknight = new GameObject[1];
    public GameObject[] Bknight = new GameObject[1];
    public GameObject[] Wbishop = new GameObject[1];
    public GameObject[] Bbishop = new GameObject[1];
    public GameObject Wking;
    public GameObject Bking;
    public GameObject Wqueen;
    public GameObject Bqueen;

    bool[] Bpawn2 = new bool[7];
    bool[] Wpawn2 = new bool[7];

    short turn = 0;

    // Start is called before the first frame update
    void Start()
    {
        #region Board
        for (float y = 0F; y <= 7; y+= 1)
        {
            for (float x = 0F; x <= 7F; x+= 1)
            {
                if (y % 2 == 0)
                {
                    if (x % 2 == 0)
                    {
                       cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.gray; 
                    } else if (x % 2 == 1)
                    {
                        cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.white; 
                    }
                
                } else if (y % 2 == 1)
                {
                    if (x % 2 == 0)
                    {
                       cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.white; 
                    } else if (x % 2 == 1)
                    {
                        cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.gray; 
                    }
                }
                Instantiate(cell[(int)x,(int)y], new Vector3(x - 3.5F, y -3.5F), transform.rotation);
                
            }
        }
        #endregion
    }
}

As you can see I've properly initialized my 2D array.如您所见,我已经正确初始化了我的二维数组。 I only showed you the setboard Region because there were no problems in the rest.我只向您展示了设置板区域,因为 rest 没有问题。

Here is the gamemanager script component and what it is showing me.这是游戏管理器脚本组件以及它向我展示的内容。

在此处输入图像描述

There is no option to reference the Cell Prefab没有引用 Cell Prefab 的选项

TL;DR TL;博士

The default Inspector can only show variables that are Serializable by Unity's Serializer, ie files which can be written-to and read-from a file by Unity for saving/loading purposes.默认 Inspector 只能显示可由 Unity 的 Serializer 序列化的变量,即 Unity 可以出于保存/加载目的写入和读取文件的文件。 2D arrays are not Serializable, thus they're not visible in the Inspector. 2D arrays 不可序列化,因此它们在检查器中不可见。

Please familiarize yourself with the Serialization rules in Unity.请熟悉 Unity 中的序列化规则。 Here's an answer about what Serialization is.这是关于什么是序列化的答案。

Alternative选择

Even List<List> or T[][] type 2D arrays are not Serializable.甚至 List<List> 或 T[][] 类型 2D arrays 也不是可序列化的。 This has to do with limitations regarding generic serialization in Unity.这与 Unity 中通用序列化的限制有关。 A simple alternative to define a concrete class as follows:定义具体 class 的简单替代方法如下:

[Serializable]
public class GameObjectList //not a monobehavior
{
    public List<GameObject> gameObjects;
}

using it later on as field稍后将其用作字段

public GameObjectList[] pieces;

This should show up a list of lists in the Inspector, letting you reference your GameObjects.这应该会在 Inspector 中显示一个列表列表,让您参考您的 GameObjects。 Of course, this method is a bit cumbersome, having to define a new class every time, but it works.当然,这种方法有点麻烦,每次都要定义一个新的class,但是可以。

Notes:笔记:

A lot of the limitations surrounding generic serialization have been lifted in Unity 2020.2 update, but you still can't serialize 2D arrays/lists directly. Unity 2020.2 更新中解除了围绕通用序列化的许多限制,但您仍然不能直接序列化 2D 数组/列表。

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

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