【Unity】Cube の6面にテクスチャを割り当てる

DEVELOP, Unity

以下の様な1枚のテクスチャを用意して、それを Cube の各面に張り付けてみます。

目次

サンプルコード

このスクリプトを Cube オブジェクトに AddComponent します。各面に対して UV を適切に割り当てている感じです。

using UnityEngine;
public class Cube : MonoBehaviour
{
    [SerializeField, Header("メッシュレンダラー")]
    private MeshRenderer m_meshRenderer;
    public MeshRenderer meshRenderer
    {
        get { return m_meshRenderer; }
    }
    void Start()
    {
        if (meshRenderer != null)
        {
            var meshFilter = meshRenderer.GetComponent<MeshFilter>();
            var mesh = meshFilter.mesh;
            var uvs = new Vector2[mesh.vertices.Length];
            // 1
            uvs[0] = new Vector2(0.0f, 0.0f);
            uvs[1] = new Vector2(0.333f, 0.0f);
            uvs[2] = new Vector2(0.0f, 0.333f);
            uvs[3] = new Vector2(0.333f, 0.333f);
            // 22
            uvs[4] = new Vector2(0.334f, 0.333f);
            uvs[5] = new Vector2(0.666f, 0.333f);
            uvs[8] = new Vector2(0.334f, 0.0f);
            uvs[9] = new Vector2(0.666f, 0.0f);
            // 3
            uvs[6] = new Vector2(1.0f, 0.0f);
            uvs[7] = new Vector2(0.667f, 0.0f);
            uvs[10] = new Vector2(1.0f, 0.333f);
            uvs[11] = new Vector2(0.667f, 0.333f);
            // 4
            uvs[12] = new Vector2(0.0f, 0.334f);
            uvs[13] = new Vector2(0.0f, 0.666f);
            uvs[14] = new Vector2(0.333f, 0.666f);
            uvs[15] = new Vector2(0.333f, 0.334f);
            // 5
            uvs[16] = new Vector2(0.334f, 0.334f);
            uvs[17] = new Vector2(0.334f, 0.666f);
            uvs[18] = new Vector2(0.666f, 0.666f);
            uvs[19] = new Vector2(0.666f, 0.334f);
            // 6
            uvs[20] = new Vector2(0.667f, 0.334f);
            uvs[21] = new Vector2(0.667f, 0.666f);
            uvs[22] = new Vector2(1.0f, 0.666f);
            uvs[23] = new Vector2(1.0f, 0.334f);
            mesh.uv = uvs;
        }
    }
}

結果

参照

Change texture of cube sides – Unity Answers

Posted by kazupon