首页 > 解决方案 > NullReferenceException:对象引用未设置为 Unity Wave Function Collapse 中的对象实例

问题描述

我正在关注波函数崩溃教程,并想对其进行一些修改。在本教程中,波函数坍缩算法(即 WFC)使用预制件 4 侧的颜色来找到在该预制件的至少一侧具有相同颜色的可能邻居。作者使用一维数组来计算预制件 4 面的颜色。我想稍微修改一下 - 我想使用带有 [,] 的 2D 数组来查找 4 面的所有颜色。但是,当我统一播放时,Rotate90 方法出现错误,ColorsLeftNew[row, col] = ColorsBack[row, tileSideVoxels - col - 1];- “NullReferenceException:对象引用未设置为对象的实例”。

    public byte[,] ColorsRight;
    public byte[,] ColorsForward;
    public byte[,] ColorsBack;

    public void CalculateSidesColors()
    {
        ColorsLeft = new byte[tileSideVoxels, tileSideVoxels];
        ColorsRight = new byte[tileSideVoxels, tileSideVoxels];
        ColorsForward = new byte[tileSideVoxels, tileSideVoxels];
        ColorsBack = new byte[tileSideVoxels, tileSideVoxels];

        for (int row = 0; row < tileSideVoxels; row++)
        {
            for (int col = 0; col < tileSideVoxels; col++)
            {
                ColorsLeft[row, col] = GetVoxelColor(row, col, Direction.Left);
                ColorsRight[row, col] = GetVoxelColor(row, col, Direction.Right);
                ColorsForward[row, col] = GetVoxelColor(row, col, Direction.Forward);
                ColorsBack[row, col] = GetVoxelColor(row, col, Direction.Back);
            }
        }

    }

    public void Rotate90()
    {
        transform.Rotate(0, 90, 0);


        byte[,] ColorsLeftNew = new byte[tileSideVoxels, tileSideVoxels];
        byte[,] ColorsRightNew = new byte[tileSideVoxels, tileSideVoxels];
        byte[,] ColorsForwardNew = new byte[tileSideVoxels, tileSideVoxels];
        byte[,] ColorsBackNew = new byte[tileSideVoxels, tileSideVoxels];

        for (int row = 0; row < tileSideVoxels; row++)
        {
            for (int col = 0; col < tileSideVoxels; col++)
            {
                ColorsLeftNew[row, col] = ColorsBack[row, tileSideVoxels - col - 1];
                ColorsRightNew[row, col] = ColorsForward[row, tileSideVoxels - col - 1];
                ColorsForwardNew[row, col] = ColorsLeft[row, tileSideVoxels - col - 1];
                ColorsBackNew[row, col] = ColorsRight[row, tileSideVoxels - col - 1];
                print("Row: " + row + " Col: " + col + " NewCol: " + (tileSideVoxels - col - 1));

                //print("Myfunc: LeftNew: " + ColorsRightNew[row, col]);
                //print("myfunc Back: " + ColorsBack[row, col]);
            }
        }
        ColorsRight = ColorsRightNew;
        ColorsForward = ColorsForwardNew;
        ColorsLeft = ColorsLeftNew;
        ColorsBack = ColorsBackNew;


    }```

The method GetVoxelColor returns a RGB color index. The Rotate90 method rotates the prefab so its sides can match with other prefabs, the rotation type is preset by hand. 

This is soo time consuming, please help!

标签: c#unity3ddebugginggenerics

解决方案


我的问题是这些代码是什么意思,为什么c1's 函数有 2 个变量v1v2

这就是所谓的lambda 表达式

(input-parameters) => expression

在这种情况下,您正在调用IEnumerable.Count 扩展方法Vector4

这是它的签名:

public static int Count<TSource> (this IEnumerable<TSource> source, Func<TSource,bool> predicate);

关于你的问题的重要部分是Func<TSource,bool> predicate)

这将为数组中的每个项目调用,TSource也就是说它将枚举Vector4数组中的每一个并调用你的 lambda。与 相同Any

您可以将 lambda 视为一种方法或函数,其中在=>.

至于这行代码实际上做了什么:

int c1 = a.Count(v1 => b.Any(v2 => Vector4.Distance(v1, v2) < threshold));

它只是给出一个计数,其中每个项目a与任何项目之间的距离b低于阈值。


推荐阅读