1

I have a folder, called "Walls" in the Ressorce-folder, which contains multiple textures, calling "wallRoi[1-26]". Now I would like to apply those to my model in Unity via C# scripting. Explaning my code: The model has multiple segmentation of the whole wall, each of them has a tag ("Wall[1-26]"). The tag of the whole wall is "Wall". Now I'm trying to loop through the whole wall and apply to each segmenation of the wall a different texture from the folder. My Code doesn't work, any purposes? Thank you!

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 + "");
            }
        }
    }
Viktoria
  • 533
  • 2
  • 7
  • 24

1 Answers1

3

Create a new script named TexturesOverWall.cs Then copy and paste the below code. All you need to do is name each wall segmentation gameobject with the same name of the texture to apply. Hope this help you :)

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;
    }
}
COBO
  • 377
  • 1
  • 5
  • 1
    If you need help with your project just let me know :) – COBO Jul 05 '17 at 17:47
  • To come back to your offer - could you please help me out with that question?https://stackoverflow.com/questions/45020915/how-to-detect-all-gameobjects-within-the-camera-fov-unity3d I would be truly grateful! :) – Viktoria Jul 12 '17 at 14:38
  • Let me take a look. Wait – COBO Jul 12 '17 at 14:54
  • Your question is hard to understand. – COBO Jul 12 '17 at 15:05
  • Enter here and lets cha a bit about your question https://chat.stackoverflow.com/rooms/149023/detect-all-gameobjects-within-the-camera-fov – COBO Jul 12 '17 at 15:05