2018.3版本的unity有新的嵌套预制体功能,现在预览图很不方便。可以生成一个。
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
[CustomPreview(typeof(GameObject))]
public class UIPreview : ObjectPreview
{
Texture preview;
const string cachePreviewPath = "CachePreviews";
public override bool HasPreviewGUI()
{
return true;
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
base.OnPreviewGUI(r, background);
if (target == null)
return;
var targetGameObject = target as GameObject;
if (targetGameObject == null)
return;
GUI.Label(r, target.name + " is being previewed");
preview = AssetPreview.GetAssetPreview(target);
if (preview == null)
{
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetGameObject));
string pathname = Path.Combine(cachePreviewPath, guid + ".png");
preview = AssetDatabase.LoadAssetAtPath(Path.Combine("Assets",pathname));
if (preview == null)
{
preview = GetAssetPreview(targetGameObject);
if (SaveTexture2D(preview as Texture2D, Path.Combine(Application.dataPath, pathname)))
{
AssetDatabase.ImportAsset(pathname);
AssetDatabase.Refresh();
Debug.Log("SaveTextureToPNG " + pathname);
}
}
}
GUI.DrawTexture(r, preview);
}
public static Texture GetAssetPreview(GameObject obj)
{
GameObject canvas_obj = null;
GameObject clone = GameObject.Instantiate(obj);
Transform cloneTransform = clone.transform;
GameObject cameraObj = new GameObject("render camera");
Camera renderCamera = cameraObj.AddComponent();
renderCamera.backgroundColor = new Color(0.8f, 0.8f, 0.8f, 1f);
renderCamera.clearFlags = CameraClearFlags.Color;
renderCamera.cameraType = CameraType.SceneView;
renderCamera.cullingMask = 1 << 21;
renderCamera.nearClipPlane = -100;
renderCamera.farClipPlane = 100;
bool isUINode = false;
if (cloneTransform is RectTransform)
{
//如果是UGUI节点的话就要把它们放在Canvas下了
canvas_obj = new GameObject("render canvas", typeof(Canvas));
Canvas canvas = canvas_obj.GetComponent
参考:https://blog.csdn.net/yudianxia/article/details/79639292
可以预览缩略图的功能:https://blog.csdn.net/x_studying/article/details/89102314