texture export

This commit is contained in:
左右看
2023-06-27 06:30:11 +08:00
parent 8bdc393613
commit ce6ae8f45c
5 changed files with 773 additions and 23 deletions
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7e5cf234fec296c4cb0231cc8ad204ea
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+111
View File
@@ -0,0 +1,111 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json.Linq;
using System;
public class TextureExporter
{
static public Texture2D duplicateTexture(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
static public void exportAllTexture(GameObject gameobj)
{
string m_Path = Application.dataPath;
string savePath = m_Path + "/../Texture2D/" + gameobj.name + "/";
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
foreach (SkinnedMeshRenderer skin_mesh in gameobj.transform.GetComponentsInChildren<SkinnedMeshRenderer>())
{
foreach (Material mat in skin_mesh.sharedMaterials)
{
//获得材质的所有属性名,如果改属性名对应的是贴图,提取该贴图
Shader mat_shader = mat.shader;
int p_num = mat_shader.GetPropertyCount();
Debug.Log(p_num);
for (int i = 0; i < p_num; i++)
{
string p_name = mat_shader.GetPropertyName(i);
if (mat_shader.GetPropertyType(i) == UnityEngine.Rendering.ShaderPropertyType.Texture)
{
//Debug.Log("Hello!");
Debug.Log(p_name);
Texture2D mat_texture = (Texture2D)mat.GetTexture(p_name);
if (mat_texture)
{
//Debug.Log("There is a Name!");
string tex_name = mat_texture.name;
string tex_path = savePath + tex_name + ".png";
if (!File.Exists(tex_path))
{
mat_texture = duplicateTexture(mat_texture);
byte[] bytes = mat_texture.EncodeToPNG();
File.WriteAllBytes(tex_path, bytes);
}
}
}
}
}
}
foreach (MeshRenderer mr in gameobj.transform.GetComponentsInChildren<MeshRenderer>())
{
foreach (Material mat in mr.sharedMaterials)
{
//获得材质的所有属性名,如果改属性名对应的是贴图,提取该贴图
Shader mat_shader = mat.shader;
int p_num = mat_shader.GetPropertyCount();
Debug.Log(p_num);
for (int i = 0; i < p_num; i++)
{
//Debug.Log(mat_shader.GetPropertyName(i));
//Debug.Log(mat_shader.GetPropertyType(i));
string p_name = mat_shader.GetPropertyName(i);
if (mat_shader.GetPropertyType(i) == UnityEngine.Rendering.ShaderPropertyType.Texture)
{
//Debug.Log("Hello!");
Debug.Log(p_name);
Texture2D mat_texture = (Texture2D)mat.GetTexture(p_name);
if (mat_texture)
{
//Debug.Log("There is a Name!");
string tex_name = mat_texture.name;
string tex_path = savePath + tex_name + ".png";
if (!File.Exists(tex_path))
{
mat_texture = duplicateTexture(mat_texture);
byte[] bytes = mat_texture.EncodeToPNG();
File.WriteAllBytes(tex_path, bytes);
}
}
}
}
}
}
Debug.Log("提取成功");
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7af2b716c09cdaa459a69078c84d6e9a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+8
View File
@@ -1309,4 +1309,12 @@ public class UmaViewerUI : MonoBehaviour
MessageScrollRect.gameObject.SetActive(true);
MessageScrollRect.verticalNormalizedPosition = 0;
}
public void ExportTextures()
{
if (Builder.CurrentUMAContainer)
{
TextureExporter.exportAllTexture(Builder.CurrentUMAContainer.gameObject);
}
}
}