unity2018.4.2、vs2017
一、通过AssetPreview获得物体的缩略图
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public Image image;
public GameObject obj;
private Texture2D texture;
void Start()
{
StartCoroutine(GetTexture());
}
IEnumerator GetTexture()
{
while (texture==null)
{
yield return null;
texture = AssetPreview.GetAssetPreview(obj); //第一帧有时候获取不到 使用协成获取缩略图 默认128*128 暂时不知道怎么修改缩略图的大小
Debug.Log("获得中");
}
image.sprite = Sprite.Create(texture, new Rect(new Vector2(0, 0), new Vector2(texture.width, texture.height)), new Vector2(0.5f, 0.5f));
}
}
模型预览:
获取结果:
也可以将texture转换为png保存到本地
二、通过rendertexture以及相机获得模型缩略图
1、Assets右键Create->Render Texture
2、创建摄影机(只渲染模型那一层)
3、在UI层创建RawImage,将创建的rendertexture赋值到texture
结果如下: