2017-07-05 23 views
1

"wallRoi [1-26]"と呼ばれる複数のテクスチャを含むRessorceフォルダに "Walls"というフォルダがあります。今度はそれらをC#スクリプトを使ってUnityの私のモデルに適用したいと思います。 私のコードを説明する:モデルは壁全体を複数に分割し、それぞれにタグ(「壁面[1-26]」)があります。壁全体のタグは「壁」です。今私は壁全体をループし、壁の各セグメンテーションにフォルダとは異なるテクスチャを適用しようとしています。私のコードはどんな目的でも機能しませんか?ありがとうございました!複数の素材に異なるテクスチャを適用する(Unity3D)

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 + ""); 
      } 
     } 
    } 

答えて

3

TexturesOverWall.csという名前の新しいスクリプトを作成し、次のコードをコピーして貼り付けます。あなたがする必要があるのは、適用するテクスチャの同じ名前を持つ各壁セグメンテーションのゲームオブジェクトに名前を付けることだけです。これがあなたに役立つことを願っています:)

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; 
    } 
} 
+0

ありがとう、それは完璧に機能しました! – Viktoria

+1

あなたのプロジェクトに助けが必要な場合は、私に知らせてください: – COBO

+0

申し込みに戻るには - この質問で私を助けてくれますか?https://stackoverflow.com/questions/45020915/how-to-detect -all-gameobjects-within-the-camera-fov-unity3d 私は本当に感謝しています! :) – Viktoria

関連する問題