LoginSignup
3
3

More than 5 years have passed since last update.

シーン上のゲームオブジェクトに別々のテクスチャを貼りたい時

Posted at

シーン上に置かれたゲームオブジェクトに対して別々のテクスチャをアサインしたい場合の方法になります。

やり方としては、シーン上のオブジェクトを配列で取得した後で
Resourceフォルダの中になる画像をアサインする方法になります。

スクリーンショット 2014-09-28 23.22.32.png
※タグで見ているのでスクリプトはどこに貼っても大丈夫です。

using UnityEngine;
using System.Collections;

public class ResourcesTextureTag : MonoBehaviour {
    private GameObject[] _instance;

    void Start () {
        _instance = GameObject.FindGameObjectsWithTag("ObjTag");

        for (int i = 0; i < _instance.Length; i++)
        {
            Debug.Log(i);
            _instance[i].renderer.material.mainTexture = Resources.Load("p"+i) as Texture2D;
        }   
    }
}

結果としては、以下のようになります。
スクリーンショット 2014-09-28 23.22.39.png

オブジェクトの参照を取得する方法は以下のサイトが参考になります。
http://www.happytrap.jp/blogs/2011/12/20/6310/

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3