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# 将不同纹理应用于多个材质(Unity3D)_C#_Unity3d_Textures_Texture Mapping - Fatal编程技术网

C# 将不同纹理应用于多个材质(Unity3D)

C# 将不同纹理应用于多个材质(Unity3D),c#,unity3d,textures,texture-mapping,C#,Unity3d,Textures,Texture Mapping,我在Ressorce文件夹中有一个名为“Walls”的文件夹,其中包含多个纹理,称为“wallRoi[1-26]”。现在我想通过C#脚本将这些应用到Unity中的模型中。 解释我的代码:模型对整个墙有多个分段,每个分段都有一个标记(“墙[1-26]”)。整面墙的标签是“墙”。现在,我尝试在整个墙中循环,并将文件夹中不同的纹理应用于墙的每个分段。我的代码不起作用,有什么目的吗?谢谢大家! private UnityEngine.Object[] walltextures; void ma

我在Ressorce文件夹中有一个名为“Walls”的文件夹,其中包含多个纹理,称为“wallRoi[1-26]”。现在我想通过C#脚本将这些应用到Unity中的模型中。 解释我的代码:模型对整个墙有多个分段,每个分段都有一个标记(“墙[1-26]”)。整面墙的标签是“墙”。现在,我尝试在整个墙中循环,并将文件夹中不同的纹理应用于墙的每个分段。我的代码不起作用,有什么目的吗?谢谢大家!

private UnityEngine.Object[] walltextures;

    void mapTexturesOverWalls() {
        walltextures = Resources.LoadAll ("Walls", typeof(Texture2D));

        Texture2D[] wallTex = (Texture2D)walltextures [walltextures.Length];

        GameObject walls = GameObject.FindGameObjectWithTag ("Wall");
        Renderer[] renders = walls.GetComponentsInChildren<Renderer> ();

        for (int i = 0; i < wallTex.Length; i++) {
            foreach (Renderer r in renders) {
                r.material.mainTexture = wallTex[i];
                UnityEngine.Debug.Log (wallTex + "");
            }
        }
    }
private UnityEngine.Object[]墙纹理;
void MapTextureOverwalls(){
walltextures=Resources.LoadAll(“墙”,类型为(Texture2D));
Texture2D[]wallTex=(Texture2D)walltextures[walltextures.Length];
GameObject walls=GameObject.FindGameObjectWithTag(“墙”);
Renderer[]renders=walls.GetComponentsInChildren();
对于(int i=0;i
创建一个名为TextureOverwall.cs的新脚本,然后复制并粘贴以下代码。您所需要做的就是使用要应用的纹理的相同名称命名每个墙分割游戏对象。希望这对您有所帮助:)

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类结构隔墙:单一行为
{
私有纹理2d[]墙纹理;
无效唤醒()
{
MapTextureOverwalls();
}
void MapTextureOverwalls()
{
walltextures=Resources.LoadAll(“墙”);
GameObject walls=GameObject.FindGameObjectWithTag(“墙”);
Renderer[]renders=walls.GetComponentsInChildren();
foreach(渲染器中的渲染器r){
//您所需要做的就是使用要应用的纹理的相同名称命名每个墙分割游戏对象
r、 material.mainTexture=getTextureByName(r.gameObject.name);
}
}
Texture2D getTextureByName(字符串名称)
{
foreach(墙纹理中的var-tex){
if(tex.name==name)
返回tex;
}
返回null;
}
}

如果您的项目需要帮助,请告诉我:)返回您的报价-您能帮我解决这个问题吗?我将非常感激!:)让我看看。等一下,你的问题很难理解。进入这里,让cha了解一下你的问题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TexturesOverWall : MonoBehaviour
{

private Texture2D[] walltextures;

void Awake ()
{
    mapTexturesOverWalls ();
}

void mapTexturesOverWalls ()
{
    walltextures = Resources.LoadAll<Texture2D> ("Walls");

    GameObject walls = GameObject.FindGameObjectWithTag ("Wall");
    Renderer[] renders = walls.GetComponentsInChildren<Renderer> ();

    foreach (Renderer r in renders) {
        // all you need to do is name each wall segmentation gameobject with the same name of the texture to apply
        r.material.mainTexture = getTextureByName(r.gameObject.name);
    }
}

Texture2D getTextureByName (string name)
{
    foreach (var tex in walltextures) {
        if (tex.name == name)
            return tex;
    }

    return null;
    }
}