前两天用后处理写了一个界面打开效果,结果在手机上显示黑屏,郁闷上的日志上并没有报错,在Unity中,可以方便的使用FrameDebug进行调试,如下图
可以使用Click+鼠标左键点开缩略图,查看对shader的赋值是否正确。
然而在手上,连接真机调试却是这样:
没有办法看到输入的纹理是否是正确的,所以第一想法就是把纹理输出的屏幕上,然后打真机进行调试,仔细观察FrameDebug的渲染顺序(如下图),发现UGUI.RenderOverlays在URP的最后渲染,也就是说我的截图操作在UGUI的RenderOverlays之前,并不会出现套娃的情况,所以我写一个简单的代码,将图片显示到UGUI的Image组件上(项目使用的是NGUI)

首先做一个UGUI的预设
cs代码比较简陋(这里主要是提供思想)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace PF.URP.PostProcessing
{
public class PFPostProcessingDebug : MonoBehaviour
{
public Image mImgNo1;
public Image mImgNo2;
public Image mImgNo3;
private static PFPostProcessingDebug mThis;
private static PFPostProcessingDebug _this
{
get
{
if (mThis == null)
{
var res = Resources.Load("PFPostProcessingDebug");
var go = GameObject.Instantiate(res) as GameObject;
mThis = go.GetComponent<PFPostProcessingDebug>();
if(mThis == null)
mThis = go.AddComponent<PFPostProcessingDebug>();
}
return mThis;
}
}
private static void ShowTex(Image img, RenderTexture rt)
{
int width = rt.width;
int height = rt.height;
Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
RenderTexture.active = rt;
texture2D.ReadPixels(new Rect(0, 0, width, height), 1, 1);
texture2D.Apply();
img.sprite = Sprite.Create(texture2D, new Rect(0, 0, width, height), Vector2.zero);
}
public static void ShowNo1(RenderTexture rt)
{
ShowTex(_this.mImgNo1, rt);
}
public static void ShowNo2(RenderTexture rt)
{
ShowTex(_this.mImgNo2, rt);
}
public static void ShowNo3(RenderTexture rt)
{
ShowTex(_this.mImgNo3, rt);
}
}
}
注意,此方式实际上是截图比较费,不可以频发调用,可以在某个关键点上调用
最后贴出效果(只用了前两个)

文章评论