记一次DllNotFoundException(缺少vs C++ 环境导致)

起因:

        unity无法直接使用webp格式文件,需要用到第三方插件:

unity使用webp方法

        private unsafe List<(Texture2D, int)> LoadAnimation(byte[] bytes)
        {
            List<(Texture2D, int)> ret = new List<(Texture2D, int)>();
            WebPAnimDecoderOptions option = new WebPAnimDecoderOptions
            {
                use_threads = 1,
                color_mode = WEBP_CSP_MODE.MODE_RGBA,
            };
            option.padding[5] = 1;

            NativeLibwebpdemux.WebPAnimDecoderOptionsInit(&option);
            fixed (byte* p = bytes)
            {
                WebPData webpdata = new WebPData
                {
                    bytes = p,
                    size = new UIntPtr((uint)bytes.Length)
                };
                WebPAnimDecoder* dec = NativeLibwebpdemux.WebPAnimDecoderNew(&webpdata, &option);
                //dec->config_.options.flip = 1;

                WebPAnimInfo anim_info = new WebPAnimInfo();

                NativeLibwebpdemux.WebPAnimDecoderGetInfo(dec, &anim_info);

                Debug.LogWarning($"{anim_info.frame_count} {anim_info.canvas_width}/{anim_info.canvas_height}");

                uint size = anim_info.canvas_width * 4 * anim_info.canvas_height;

                int timestamp = 0;

                IntPtr pp = new IntPtr();
                byte** unmanagedPointer = (byte**)&pp;
                for (int i = 0; i < anim_info.frame_count; ++i)
                {
                    int result = NativeLibwebpdemux.WebPAnimDecoderGetNext(dec, unmanagedPointer, ×tamp);
                    Assert.AreEqual(1, result);

                    int lWidth = (int)anim_info.canvas_width;
                    int lHeight = (int)anim_info.canvas_height;
                    bool lMipmaps = false;
                    bool lLinear = false;

                    Texture2D texture = Texture2DExt.CreateWebpTexture2D(lWidth, lHeight, lMipmaps, lLinear);
                    texture.LoadRawTextureData(pp, (int)size);

                    {// Flip updown.
                     // ref: https://github.com/netpyoung/unity.webp/issues/25
                     // ref: https://github.com/netpyoung/unity.webp/issues/21
                     // ref: https://github.com/webmproject/libwebp/blob/master/src/demux/anim_decode.c#L309
                        Color[] pixels = texture.GetPixels();
                        Color[] pixelsFlipped = new Color[pixels.Length];
                        for (int y = 0; y < anim_info.canvas_height; y++)
                        {
                            Array.Copy(pixels, y * anim_info.canvas_width, pixelsFlipped, (anim_info.canvas_height - y - 1) * anim_info.canvas_width, anim_info.canvas_width);
                        }
                        texture.SetPixels(pixelsFlipped);
                    }

                    texture.Apply();
                    ret.Add((texture, timestamp));
                }
                NativeLibwebpdemux.WebPAnimDecoderReset(dec);
                NativeLibwebpdemux.WebPAnimDecoderDelete(dec);
            }
            return ret;
        }

打包exe后,试了很多电脑,都没问题。但是在一台新电脑上却出现了bug

NativeLibwebpdemux.WebPAnimDecoderOptionsInit(&option);

这里报错

问题描述:

一开始,怀疑是下载过程出了问题,后来发现,下载的bytes[]写入本地后,webp可以正常打开。

所以排除是网络问题。

然后使用repoter真机看log发现:

 

记一次DllNotFoundException(缺少vs C++ 环境导致)_第1张图片

然后在公司非开发人员的电脑上测试发现也是正常的,就卡在这里好久。

 

 

结果:

 

后来经过和同事们讨论,发现使用的插件是c++写的,那么就很可能是缺少c++环境导致的。

确实,我们开发人员在安装vs的时候,会自动安装。新电脑应该是没有配置这种环境。

果然,在安装vs c++ 2015环境后,就可以正常使用了。

 

你可能感兴趣的:(Unity,webp,DllNotFound,c++,unity,c#)