コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】ゲーム中にオブジェクトやマテリアルのプレビューテクスチャを生成できる「Runtime Preview Generator for Unity 3D」紹介

はじめに

「Runtime Preview Generator for Unity 3D」を Unity プロジェクトに導入することで
ゲーム中にオブジェクトやマテリアルのプレビューテクスチャを生成できるようになります

使用例

using UnityEngine;

public sealed class Example : MonoBehaviour
{
    public Transform model;
    public Texture2D texture;

    private void Update()
    {
        if ( Input.GetKeyDown( KeyCode.Z ) )
        {
            texture = RuntimePreviewGenerator
                .GenerateModelPreview( model, 256, 256 );
        }
    }
}

使い方

// マテリアルのサムネイルを生成
public static Texture2D GenerateMaterialPreview
( 
    Material material, 
    PrimitiveType previewObject, 
    int width = 64, 
    int height = 64 
);

// オブジェクトのサムネイルを生成
public static Texture2D GenerateModelPreview
( 
    Transform model, 
    int width = 64, 
    int height = 64, 
    bool shouldCloneModel = false 
);

オプション

  • RuntimePreviewGenerator.Padding
    • サムネイルの余白、指定できる値の範囲は ( -0.25, 0.25 )。デフォルトは 0
  • RuntimePreviewGenerator.BackgroundColor
    • サムネイルの背景色、デフォルトは ( 0.3, 0.3, 0.3, 1 )
  • RuntimePreviewGenerator.OrthographicMode
    • 正投影なら true、透視投影なら false
  • RuntimePreviewGenerator.TransparentBackground
    • 背景を透明でレンダリングする場合 true にする

関連記事