Render Texture在Android下的异常渲染问题--最终用动态生成Render Texture解决

今天出现了一个奇怪的问题,人物角色在UI上的显示,采用的是Render Texture方案。在PC上正常,但在Android下的渲染异常(不显示)。

尝试改了兼容性比较好的色彩模式(如下图),也不行。最后动态生成Render Texture,可以了。我没有搞明白这是为什么,如果有明白的朋友,请不吝赐教。

改了参数也不行

下面是动态生成Render Texture

动态生成的代码,根据你项目适当修改即可:

cs 复制代码
using UnityEngine.UI;


public class DynamicRenderTexture_Role : MonoBehaviour
{
    // 获取 RawImage 组件并应用 Render Texture
    public RawImage rawImage;

    // 最新更改为动态生成 Render Texture
    [Header("动态生成--RenderTexture")]
    public int textureWidth = 1024;
    public int textureHeight = 1024;
    // 格式, RenderTextureFormat.ARGB32 或 RenderTextureFormat.Default 
    // 貌似都是 [ R8B8G8A8_UNORM ]
    public RenderTextureFormat textureFormat = RenderTextureFormat.Default;  
    public RenderTexture renderTexture;
    public Camera cam;


    void Start()
    {
        // ---------- 用于动态生成 RT ---------
        // 动态找相机
        cam = GameObject.Find("Camera_For_RT").GetComponent<Camera>();
        // 创建 Render Texture
        renderTexture = new RenderTexture(textureWidth, textureHeight, 16, textureFormat);
        renderTexture.Create();
        // 将 Render Texture 附加到像机
        cam.targetTexture = renderTexture;
        // 将其应用到UI
        if (rawImage != null)
        {
            rawImage.texture = renderTexture;
        }
        // ----------------------------------
    }

    void OnDisable()
    {
        // 清理 Render Texture
        if (renderTexture != null)
        {
            renderTexture.Release();
            renderTexture = null;
        }
    }
}
相关推荐
方白羽23 分钟前
Android 开发中,准确判断应用处于“前台(Foreground)”还是“后台(Background)
android·app·客户端
Mart!nHu1 小时前
Android 10&15 Framework 允许设置系统时间早于编译时间
android
编程之路从0到12 小时前
ReactNative新架构之Android端TurboModule机制完全解析
android·react native·源码阅读
iloveAnd3 小时前
Android开发中痛点解决(二)兼容性:AndroidX和gradle版本的兼容性
android·兼容性·androidx
stevenzqzq3 小时前
DataStore基本使用教程
android
LawrenceMssss4 小时前
由于创建一个完整的App涉及到多个层面(如前端、后端、数据库等),并且每种语言通常有其特定的用途(如Java/Kotlin用于Android开发,Swift/Objective-C用于iOS开发,Py
android·java·ios
chen_mangoo5 小时前
HDMI简介
android·linux·驱动开发·单片机·嵌入式硬件
阿里-于怀5 小时前
AgentScope AutoContextMemory:告别 Agent 上下文焦虑
android·java·数据库·agentscope
Larry_Yanan6 小时前
Qt安卓开发(三)双摄像头内嵌布局
android·开发语言·c++·qt·ui
粲然忧生6 小时前
腾讯云终端性能监控SDK正式上线,为鸿蒙开发适配保驾护航
android·腾讯云·harmonyos