c# - Unity数组中的nullreferenceexception
问题描述
我正在尝试制作一个类似于我的世界的游戏,当我为我的 Block 生成 UV 时,myscript 出于某种原因抛出了 NullReferenceException。我试图改变一些变量,但它没有帮助。
块类型.cs:
public class BlockType
{
public string name { get; private set; }
public bool isTransparent { get; private set; }
public bool everySideSame { get; private set; }
public Vector2[] topUV { private get; set; }
public Vector2[] sideUV { private get; set; }
public Vector2[] bottomUV { private get; set; }
public List<Vector2[]> blocktypeUVs = new List<Vector2[]>();
public BlockType(string name, bool isTransparent, bool everySideSame)
{
this.name = name;
this.isTransparent = isTransparent;
this.everySideSame = everySideSame;
}
public Vector2[] getUV(Block.BlockSide side)
{
if (everySideSame||(side!=Block.BlockSide.Top&&side!=Block.BlockSide.Bottom))
return sideUV;
if (side == Block.BlockSide.Top)
return topUV;
else
return bottomUV;
}
public void GenerateUVs()
{
if (sideUV.Length>0)
this.blocktypeUVs.Add(new Vector2[4] { sideUV[3], sideUV[2], sideUV[0], sideUV[1] });
if (topUV.Length > 0)
{
this.blocktypeUVs.Add(new Vector2[4] { topUV[3], topUV[2], topUV[0], topUV[1] });
}
if (bottomUV.Length>0)
{
this.blocktypeUVs.Add(new Vector2[4] { bottomUV[3], bottomUV[2], bottomUV[0], bottomUV[1] });
}
}
public Vector2[] getBlockUVs(Block.BlockSide side)
{
if (everySideSame || (side != Block.BlockSide.Top && side != Block.BlockSide.Bottom))
return blocktypeUVs[0];
if (side == Block.BlockSide.Top)
return blocktypeUVs[1];
else
return blocktypeUVs[2];
}
}
World.cs 代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
class World:MonoBehaviour
{
Material blockMaterial;
public Texture2D[] atlasTexture;
public static Dictionary<string, Rect> atlasDictionary = new Dictionary<string, Rect>();
public static Dictionary<string, Chunk> chunks = new Dictionary<string, Chunk>();
public int chunkSize = 16;
public int columnHeight = 16;
public int worldSize = 5;
public static List<BlockType> blockTypes = new List<BlockType>();
private void Start()
{
Texture2D atlas = GetTextureAtlas();
Material material = new Material(Shader.Find("Standard"));
material.mainTexture = atlas;
blockMaterial = material;
GenerateBlockType();
GenerateWorld();
StartCoroutine(BuildWorld());
}
IEnumerator BuildWorld()
{
foreach (KeyValuePair<string,Chunk> chunk in chunks )
{
chunk.Value.DrawChunk(chunkSize);
yield return null;
}
}
void GenerateWorld()
{
for (int z = 0; z < worldSize; z++)
{
for (int x = 0; x < worldSize; x++)
{
for (int y = 0; y < columnHeight; y++)
{
Vector3 chunkposition = new Vector3(x * chunkSize, y * chunkSize, z * chunkSize);
string chunkName = GetChunkName(chunkposition);
Chunk chunk = new Chunk(chunkName, chunkposition, blockMaterial);
chunk.chunkObject.transform.parent = transform;
chunks.Add(chunkName, chunk);
}
}
}
}
void GenerateBlockType()
{
BlockType dirt = new BlockType("dirt", false, true);
dirt.sideUV = setUVS("dirt");
print(dirt.getUV(Block.BlockSide.Left));
dirt.GenerateUVs();
blockTypes.Add(dirt);
BlockType grass = new BlockType("grass", false, false);
grass.topUV = setUVS("grass");
grass.sideUV = setUVS("grass_side");
grass.bottomUV = setUVS("dirt");
print(grass.getUV(Block.BlockSide.Top));
grass.GenerateUVs();
blockTypes.Add(grass);
BlockType brick = new BlockType("brick", false, true);
brick.sideUV = setUVS("brick");
print(brick.getUV(Block.BlockSide.Left));
brick.GenerateUVs();
blockTypes.Add(brick);
BlockType air = new BlockType("air", true, true);
air.sideUV = setUVS("air");
print(air.getUV(Block.BlockSide.Left));
air.GenerateUVs();
blockTypes.Add(air);
}
Vector2[] setUVS(string name)
{
if (name=="air")
{
return new Vector2[4] {
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
}
return new Vector2[4] {
new Vector2(atlasDictionary[name].x,
atlasDictionary[name].y),
new Vector2(atlasDictionary[name].x+atlasDictionary[name].width,
atlasDictionary[name].y),
new Vector2(atlasDictionary[name].x,
atlasDictionary[name].y+atlasDictionary[name].height),
new Vector2(atlasDictionary[name].x+atlasDictionary[name].width,
atlasDictionary[name].y+atlasDictionary[name].height),
};
}
string GetChunkName(Vector3 position)
{
return (int)position.x+"_"+(int)position.y+"_"+(int)position.z;
}
Texture2D GetTextureAtlas()
{
Texture2D textureAtlas = new Texture2D(8192, 8192);
Rect[] rectCoordinates = textureAtlas.PackTextures(atlasTexture, 0, 8192, false);
textureAtlas.Apply();
for (int i = 0; i < rectCoordinates.Length; i++)
{
atlasDictionary.Add(atlasTexture[i].name.ToLower(), rectCoordinates[i]);
}
return textureAtlas;
}
}
它显示错误恰好在 BlockType.cs 中的这一行:
this.blocktypeUVs.Add(new Vector2[4] { sideUV[3], sideUV[2], sideUV[0], sideUV[1] });
请有人帮助我。
解决方案
推荐阅读
- javascript - 使用 mongodb 驱动程序通过 mongoose 连接更改流
- google-cloud-platform - 在谷歌云中使用云代理连接VM和Paas实例有多安全
- airflow - 如何将 git 的凭据传递给 bitnami 气流头盔
- curl - 并行运行 curl 中测量的时间不匹配
- android - Android - 如何从电子邮件选择器列表中排除 PayPal?
- django - 如何在没有额外请求的情况下在 Django REST Framework 中进行动态调度?
- python - Pycharm IDE 没有在我的 Windows 10 上打开 ..非常需要一个建议
- windows - 带有递归 for 循环的批处理文件以移动文件
- javascript - 网络断开时,firebase 更新不会超时或出错
- blazor - 如何在任何地方在 blazor 中调用 js?