• Unity 截图优化


    代码片段:

        IEnumerator Cut(Camera camera, string path)
        {
    
            Debug.LogError("!!!!!__________" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
            RenderTexture rt = RenderTexture.GetTemporary(1024, 1024, 32);
            camera.targetTexture = rt;
            byte[] arr = new byte[] { };
            yield return new WaitForEndOfFrame();
    
            AsyncGPUReadbackRequest request=AsyncGPUReadback.Request(rt, 0, TextureFormat.ARGB32);
            yield return new WaitUntil(()=> {
                if (request.done || request.hasError)
                {
                    if (request.hasError)
                    {
                        VRDebug.LogError("保存图片失败 GPU readback error detected."+path);
                        return true;
                    }
                    Debug.LogError("____________________________" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                    var data = request.GetData<byte>();
    
                    System.IntPtr ptr = ToIntPtr(data);//用指针,比copy成byte[],一张1024*1024的图,快了100毫秒
                    Texture2D texture2D = new Texture2D(1024, 1024, TextureFormat.ARGB32, false);
                    texture2D.LoadRawTextureData(ptr, data.Length);
    
                    //byte[] array = new byte[data.Length];
                    //data.CopyTo(array);
                    //Texture2D texture2D = new Texture2D(1024, 1024, TextureFormat.ARGB32, false);
                    //texture2D.LoadRawTextureData(array);
    
                    byte[] png = ImageConversion.EncodeToPNG(texture2D);
                    File.WriteAllBytes(path, png);
                    Debug.LogError("____________________________" + System.DateTime.Now.ToString("HH:mm:ss.fff"));
                    return true;
                }
                else
                {
                    return false;
                }
            });
        }
    
        /// <summary>
        /// 找到NativeArray<byte>的指针
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        System.IntPtr ToIntPtr(NativeArray<byte> array)
        {
            unsafe
            {
                return (System.IntPtr)Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.GetUnsafePtr(array); // void* -> IntPtr explicit conversion.
            }
        }
    
        /// <summary>
        /// 通过指针,转为NativeArray<byte>
        /// </summary>
        /// <param name="ptr"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        NativeArray<byte> ToNativeArray(System.IntPtr ptr,int length)
        {
            unsafe
            {
                return Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<byte>((void*)ptr, length, Allocator.None);
    
            }
        }

    博客园Jason_C技术交流群

    扫描二维码加入qq群:623307256,共同探讨工作中遇到的Unity相关的问题!

  • 相关阅读:
    unix文件权限
    jira部署,主机迁移,数据库迁移,jira
    c函数习记
    常用软介质下载
    Matlab interpgui
    LightOJ 1422
    【CODEFORCES】 A. Keyboard
    leetcode 230: Kth Smallest Element in a BST
    Vertica7 Native Connection Load Balance
    vlc模块间共享变量
  • 原文地址:https://www.cnblogs.com/Jason-c/p/14486647.html
Copyright © 2020-2023  润新知