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;
        }
    }
}
相关推荐
2501_9445264210 小时前
Flutter for OpenHarmony 万能游戏库App实战 - 笑话生成器实现
android·javascript·python·flutter·游戏
2501_9445264210 小时前
Flutter for OpenHarmony 万能游戏库App实战 - 21点游戏实现
android·javascript·flutter·游戏·harmonyos
Alex老夫子10 小时前
android room数据库增加字段注意事项
android·数据库
2501_9159184111 小时前
iOS App 测试方法,Xcode、TestFlight与克魔(KeyMob)等工具组合使用
android·macos·ios·小程序·uni-app·iphone·xcode
冬奇Lab11 小时前
Android 15 显示子系统深度解析(一):显示框架总览与SurfaceFlinger核心机制
android·性能优化
代码煮茶君11 小时前
MySQL 数据库死锁及核心机制全解析
android·数据库·mysql
Kratzdisteln12 小时前
【1902】0120-3 Dify变量引用只能引用一层
android·java·javascript
2501_9159214312 小时前
iOS 描述文件制作过程,从 Bundle ID、证书、设备到描述文件生成后的验证
android·ios·小程序·https·uni-app·iphone·webview
冬奇Lab12 小时前
【Kotlin系列10】协程原理与实战(上):结构化并发让异步编程不再是噩梦
android·开发语言·kotlin
A-花开堪折13 小时前
RK3568 Android 11 驱动开发(四):添加产品配置和内核设备树选择
android·驱动开发