Unity で、 ZENRIN さんが出してる otakuc city のビルのオブジェクトをXBOXのコントローラーの joystick button 5 を押しているときだけテクスチャを、自前の物に変えたいのですが、前回の質問で回答をいただいて修正したところ、ボタンを押している時はビル全体のテクスチャを、自分で設定したテクスチャに変えることが出来、離した後も取得したテクスチャを再度貼り付けている様子が伺えて進展が有ったのですが、どうもそのテクスチャがビルの一部のテクスチャがビルのすべての部位に貼り付けられてぐちゃぐちゃになってしまいます。
それがこの下のコードです。
using UnityEngine;
using System.Collections;
public class allmat1ver2 : MonoBehaviour { //ボタン離したら戻るが最初に取得したテクスチャを全部に複製してる
//テクスチャを用意 public Texture image; public Texture image2; public Texture image3; // Use this for initialization void Start() { image = GetComponent<Renderer>().material.GetTexture("_MainTex"); //問題の行 } // Update is called once per frame void Update() { Material[] mats = GetComponent<Renderer>().materials; if (Input.GetKey(KeyCode.Joystick1Button5)) { foreach (Material mat in mats) { mat.mainTexture = image2; } } if (Input.GetKeyUp(KeyCode.Joystick1Button5)) { foreach (Material mat in mats) { mat.mainTexture = image; } } }
}
そこで最初に元のテクスチャをすべて取得して、ボタンを離した際にすべての部位に適応したいと思い以下のコードを書きましたが、今度はボタンを離しても、押したときのテクスチャのまま変わりません。
using UnityEngine;
using System.Collections;
public class allmat1ver3 : MonoBehaviour//離しても戻らない
{
//テクスチャを用意 public Texture image; public Texture image2; public Texture image3; // Use this for initialization void Start() { Material[] mats2 = GetComponent<Renderer>().materials; foreach (Material mat2 in mats2) { mat2.mainTexture = GetComponent<Renderer>().material.GetTexture("_MainTex"); Debug.Log("mat2 is : " + mat2); } } // Update is called once per frame void Update() { Material[] mats = GetComponent<Renderer>().materials; Material[] mats2 = GetComponent<Renderer>().materials; //image3 = GetComponent<Renderer>().material.GetTexture("_MainTex"); if (Input.GetKey(KeyCode.Joystick1Button5)) { foreach (Material mat2 in mats2) { mat2.mainTexture = GetComponent<Renderer>().material.GetTexture("_MainTex"); } foreach (Material mat in mats) { mat.mainTexture = image2; } } if (Input.GetKeyUp(KeyCode.Joystick1Button5)) { foreach (Material mat in mats) { foreach (Material mat2 in mats2) { mat.mainTexture = mat2.mainTexture; } } } } }
これは恐らくupdate()の中でmats2を取得しているためボタンを押した際の自前のテクスチャをまたmats2に入れてしまっているためだと思われます。
なのでテクスチャの取得をstart()内で行う方法を考え以下のコードにしました。
using UnityEngine;
using System.Collections;
public class allmat1ver4 : MonoBehaviour//離しても元のまま。foreachでまわしまくった。
{
//テクスチャを用意 public Texture image; public Texture image2; public Texture image3; public Material[] mats; public Material[] mats2; // Use this for initialization void Start() { Material[] mats = GetComponent<Renderer>().materials; Material[] mats2 = GetComponent<Renderer>().materials; foreach (Material mat2 in mats2) { mat2.mainTexture = GetComponent<Renderer>().material.GetTexture("_MainTex"); Debug.Log("mat2 is : " + mat2); } } // Update is called once per frame void Update() { //Material[] mats = GetComponent<Renderer>().materials; //Material[] mats2 = GetComponent<Renderer>().materials; //image3 = GetComponent<Renderer>().material.GetTexture("_MainTex"); if (Input.GetKey(KeyCode.Joystick1Button5))//R1ボタンを押したときに { foreach (Material mat in mats)//全てのマテリアルに対して { mat.mainTexture = image2;//マテリアルのテクスチャにimage2を入れる //Debug.Log("mat is : " + mat);//デバックで表示 } } if (Input.GetKeyUp(KeyCode.Joystick1Button5))//R1ボタンを離した時に { foreach (Material mat in mats)//全てのマテリアルに対して { foreach (Material mat2 in mats2)//全てのマテリアル2に対して { mat.mainTexture = mat2.mainTexture;//マテリアルのテクスチャにマテリアル2のテクスチャを入れる Debug.Log("mat is : " + mat);//デバックで表示 } } } }
}
このコードでやると、ボタンを押す前からビルらしくないテクスチャが貼られています。よく見ると、ビルの各部位のテクスチャは、もともとゼンリンさんが用意しているテクスチャの大本のデータがあり、その一部をビルの一階のテナントの看板など、細かい部分に適用させているようで、その大本のテクスチャが壁面全面に貼られておかしくなってしまっています。そしてボタンを押しても無反応です。
これを改善するためにはどうすればよいでしょうか?
自分ひとりで考えていてもなかなか分からないのでご助言頂けると幸いです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/27 06:51