Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 同一材料有多个网格的_C#_Unity3d_Tiles_Tile - Fatal编程技术网

C# 同一材料有多个网格的

C# 同一材料有多个网格的,c#,unity3d,tiles,tile,C#,Unity3d,Tiles,Tile,我目前正在寻找一种在Unity中绘制平铺贴图的有效方法。我的系统工作的方式是,我通过将一堆由瓷砖组成的房间粘在一起来构建一个标高。我跟随本教程学习了一年 现在,我正在为我拥有的每个房间生成一个新网格,然后将我想要的纹理放置在材质的正确位置。但是,我目前使用的是所有房间都使用的一种材料预制件。这意味着,由于所有房间都覆盖相同的材质,因此瓷砖不再适合其他房间 这就是我的意思。这是我只有一个房间的时候,你可以看到,因为这是唯一一个修改材质的房间,瓷砖都是正确的: 但是当我创建几个房间时,它们都修改了相

我目前正在寻找一种在Unity中绘制平铺贴图的有效方法。我的系统工作的方式是,我通过将一堆由瓷砖组成的房间粘在一起来构建一个标高。我跟随本教程学习了一年

现在,我正在为我拥有的每个房间生成一个新网格,然后将我想要的纹理放置在材质的正确位置。但是,我目前使用的是所有房间都使用的一种材料预制件。这意味着,由于所有房间都覆盖相同的材质,因此瓷砖不再适合其他房间

这就是我的意思。这是我只有一个房间的时候,你可以看到,因为这是唯一一个修改材质的房间,瓷砖都是正确的:

但是当我创建几个房间时,它们都修改了相同的纹理。这意味着修改纹理的最后一个房间的瓷砖放置正确,而其他房间的瓷砖放置不正确:

这是材料工作的实际房间:

我的问题是,有没有一种方法可以保存我为房间创建的网格,而不使用相同的材质?当然,为每个房间分别制作100种材料是可行的,但这只是胡说八道。我希望将为房间构建的网格“按原样”保存,或者使我创建的所有不同网格不依赖于单个材质

(大致上)我现在用来制作房间正确材料的代码:

public void BuildTextures(Room room)
{
    Sprite ground = Resources.Load<Sprite>("Background Sprites/Tile 2");

    int texWidth = (room.Width + 1) * (int)ground.textureRect.width;
    int texHeight = (room.Height + 1) * (int)ground.textureRect.height;
    int tileWidth = (int)ground.textureRect.width;
    int tileHeight = (int)ground.textureRect.height;

    Texture2D texture = new Texture2D(texWidth, texHeight);

    texture.SetPixels(x * (int)ground.textureRect.width, y * (int)ground.textureRect.height, (int)ground .textureRect.width, (int)ground .textureRect.height, ground.GetPixels()); //ground.GetPixels technically doesn't exist for a sprite, but I created a function which yields the same result. This is not important

    texture.filterMode = FilterMode.Point;
    texture.wrapMode = TextureWrapMode.Clamp;
    texture.Apply();

    MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
    meshRenderer.sharedMaterials[0].mainTexture = texture;
}
公共空间建筑纹理(房间)
{
精灵地面=Resources.Load(“背景精灵/地砖2”);
int texWidth=(room.Width+1)*(int)ground.Width;
int texHeight=(room.Height+1)*(int)ground.Height;
int tileWidth=(int)ground.textureWidth.width;
int tileHeight=(int)ground.structure.height;
纹理2d纹理=新纹理2d(texWidth,texHeight);
texture.SetPixels(x*(int)ground.textureUplater.width,y*(int)ground.textureUplater.height,(int)ground.textureUplater.width,(int)ground.height,ground.GetPixels();//ground.GetPixels从技术上讲对于精灵来说是不存在的,但我创建了一个产生相同结果的函数。这并不重要
texture.filterMode=filterMode.Point;
texture.wrapMode=TextureWrapMode.Clamp;
纹理。应用();
MeshRenderer=GetComponent();
meshRenderer.sharedMaterials[0].mainTexture=texture;
}

标签的第一句话是“不要在关于UNITY游戏引擎的问题上使用(使用:取而代之)”哦,我没有真正注意到这一点。很抱歉给您带来不便,您正在访问
sharedMaterials
,该材质在具有该材质的所有网格之间共享一个材质实例。如果您改为访问
材质
,Unity将在内部为每个网格复制一个新材质实例。哇!谢谢拉特,这正是我所需要的!我很高兴这最终是一个非常简单的解决方案:D