Unity의 모델은 그림으로 변환됩니다.

원본 작업: (32개 메시지) Unity에서 모델이 그림으로 변환됩니다_unity가 그림을 생성합니다_ʚ화면 이름의 블로그를 입력하세요-CSDN 블로그

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ExportPNG : MonoBehaviour
{
    public GameObject[] prefabs;
    void Start()
    {
        for (int i = 0; i < prefabs.Length; i++)
        {
            Debug.Log(prefabs[i].name);
            EditorUtility.SetDirty(prefabs[i]);
            Texture2D image = AssetPreview.GetAssetPreview(prefabs[i]);

            System.IO.File.WriteAllBytes(Application.dataPath + "/Resources/Images/" + prefabs[i].name + ".png", image.EncodeToPNG());
        }
        //for (int i = 0; i < prefabs.Length; i++)
        //{
        //    Debug.Log(prefabs[i].name);
        //    EditorUtility.SetDirty(prefabs[i]);
        //    Texture2D image = AssetPreview.GetAssetPreview(prefabs[i]);
        //    image = ResizeTexture(image, 512, 512);
        //    System.IO.File.WriteAllBytes(Application.dataPath + "/Resources/Images/" + prefabs[i].name + ".png", image.EncodeToPNG());
        //}
    }

    private static Texture2D ResizeTexture(Texture2D source, int width, int height)
    {
        if (source != null)
        {
            // 创建临时的RenderTexture
            RenderTexture renderTex = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
            // 复制source的纹理到RenderTexture里
            Graphics.Blit(source, renderTex);
            // 开启当前RenderTexture激活状态
            RenderTexture previous = RenderTexture.active;
            RenderTexture.active = renderTex;
            // 创建修改后的纹理,保持与源纹理相同压缩格式
            Texture2D resizedTexture = new Texture2D(width, height,TextureFormat.RGB24, false);
            // 读取像素到创建的纹理中
            resizedTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            // 应用修改到GPU上
            resizedTexture.Apply();
            // 停止当前RenderTexture工作
            RenderTexture.active = previous;
            // 释放内存
            RenderTexture.ReleaseTemporary(renderTex);
            return resizedTexture;
        }
        else
        {
            return null;
        }
    }
    IEnumerator UploadPNG()
    {
        // We should only read the screen buffer after rendering is complete
        yield return new WaitForEndOfFrame();

        // Create a texture the size of the screen, RGB24 format
        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        // Read screen contents into the texture
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Apply();

        // Encode texture into PNG
        byte[] bytes = tex.EncodeToPNG();
        Object.Destroy(tex);

        // For testing purposes, also write to a file in the project folder
        // File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);


        // Create a Web Form
        WWWForm form = new WWWForm();
        form.AddField("frameCount", Time.frameCount.ToString());
        form.AddBinaryData("fileUpload", bytes);

        // Upload to a cgi script
        WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form);
        yield return w;

        if (w.error != null)
        {
            Debug.Log(w.error);
        }
        else
        {
            Debug.Log("Finished Uploading Screenshot");
        }
    }





}

Guess you like

Origin blog.csdn.net/qq_37524903/article/details/131455262