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;
        }
    }
}
相关推荐
GitLqr8 小时前
Flutter 3.44 插件内置 Kotlin (KGP) 双向兼容适配指南
android·flutter·dart
随遇丿而安13 小时前
第11周:Activity 跳转与传值 + 跳转优化
android
私人珍藏库14 小时前
[Android] BBLL 开源第三方B哩电视TV端
android·app·生活·工具·多功能
杉氧16 小时前
跨平台资源管理:一套代码如何搞定 Android、iOS 和 Web 的图片与多语言?
android·架构·android jetpack
安卓修改大师17 小时前
安卓修改大师实战:从反编译到定制的完整APK修改指南
android
柚鸥ASO优化18 小时前
安卓APP推广的“降本增效”密码:守好商店内,打好商店外
android·aso优化
我是一颗柠檬19 小时前
【Java项目技术亮点】EXPLAIN深度分析与慢查询治理
android·java·开发语言
Android-Flutter19 小时前
android compose shadow 阴影 使用
android·kotlin·compose
帅次19 小时前
Android 高级工程师面试:Java 多线程与并发 近1年高频追问 22 题
android·java·面试
2501_9437823520 小时前
【共创季稿事节】摩斯电码转换器:编码表与双向转换的实现
android·华为·鸿蒙·鸿蒙系统