46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
|
using System.IO;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
namespace Runtime
|
|||
|
{
|
|||
|
public class TestHunHePic : MonoBehaviour
|
|||
|
{
|
|||
|
public Texture2D bottomTexture;
|
|||
|
public Texture2D topTexture;
|
|||
|
public Shader blendShader;
|
|||
|
|
|||
|
[ContextMenu("Blend and Export")]
|
|||
|
public void BlendAndExport()
|
|||
|
{
|
|||
|
// 创建临时RenderTexture
|
|||
|
RenderTexture rt = new RenderTexture(bottomTexture.width, bottomTexture.height, 0);
|
|||
|
|
|||
|
// 创建材质并设置纹理
|
|||
|
Material blendMaterial = new Material(blendShader);
|
|||
|
blendMaterial.SetTexture("_MainTex", bottomTexture);
|
|||
|
blendMaterial.SetTexture("_BlendTex", topTexture);
|
|||
|
|
|||
|
// 执行混合
|
|||
|
Graphics.Blit(null, rt, blendMaterial);
|
|||
|
|
|||
|
// 转换RenderTexture为Texture2D
|
|||
|
Texture2D output = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
|
|||
|
RenderTexture.active = rt;
|
|||
|
output.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
|
|||
|
output.Apply();
|
|||
|
|
|||
|
// 保存为PNG
|
|||
|
byte[] bytes = output.EncodeToPNG();
|
|||
|
string path = Path.Combine(Application.streamingAssetsPath, "BlendedImage.png");
|
|||
|
File.WriteAllBytes(path, bytes);
|
|||
|
|
|||
|
// 清理资源
|
|||
|
RenderTexture.active = null;
|
|||
|
DestroyImmediate(rt);
|
|||
|
Destroy(output);
|
|||
|
|
|||
|
Debug.Log($"Saved blended image to: {path}");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|