预制体名字为ObjectPrefab,生成的图片为SavedScreen.png,保存在Asset文件夹下。
- 导入UnityEditor 和 System.IO包。
- 将ObjectPrefab预制体放在Assets 下的Resources 文件夹下。
//using UnityEditor; //using System.IO; GameObject prefab = Resources.Load("ObjectPrefab"); Texture2D Tex = AssetPreview.GetAssetPreview(prefab); // Encode texture into PNG byte[] bytes = Tex.EncodeToPNG(); File.WriteAllBytes(Application.dataPath + "/../Assets/SavedScreen.png", bytes);