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;
        }
    }
}
相关推荐
方白羽28 分钟前
Android17 重写 MessageQueue,解决 Handler 隐性卡顿
android·app
蜡台1 小时前
Kotlin 零基础完整版实战教程|从语法入门到Android工程实战
android·开发语言·kotlin
律宏阔2 小时前
Android 车载 USB 开发笔记:USB Host、USB 串口、USB-CAN、HID 与系统 API
android
律宏阔2 小时前
Android 车载串口开发笔记:UART、RS232、RS485、串口配置与数据通信
android
YF02113 小时前
遥控器APP端自动重连方案
android
执明wa4 小时前
Android Studio 打包 APK
android·ide·android studio
waiting9711184 小时前
Ubuntu 26.04 + Android14安装与编译教程
android·linux·ubuntu
hunterandroid4 小时前
Android 测试全景:从单元测试到 UI 自动化的完整实践
android·前端
蜡台4 小时前
Kotlin 五大作用域函数详解|let/run/apply/also/with 选型指南+实战避坑
android·java·kotlin
2401_833269306 小时前
Android registerForActivityResult详解
android