Exemple #1
0
        /// <summary>
        /// Loads an image from bytes with the specified width and height. Use this instead of someTexture.LoadImage() if you're compiling to an assembly. Unity has moved the method in 2017,
        /// and Unity's assembly updater is not able to fix it for you. This searches for a proper LoadImage method in multiple locations, and also handles type name conflicts.
        /// </summary>
        public static Texture2D LoadImage(int width, int height, byte[] bytes)
        {
            if (tryLoadImage == null)
            {
                System.Reflection.MethodInfo loadImageMethodInfo = typeof(Texture2D).GetMethod("LoadImage", new Type[] { typeof(byte[]) });

                if (loadImageMethodInfo == null)
                {
                    loadImageMethodInfo = AppDomain.CurrentDomain.GetAssemblies()
                                          .Where(x => x.FullName.Contains("UnityEngine"))
                                          .Select(x => x.GetType("UnityEngine.ImageConversion"))
                                          .Where(x => x != null)
                                          .Select(x =>
                    {
                        return(x.GetMethod("LoadImage", new Type[] { typeof(Texture2D), typeof(byte[]) }) ??
                               x.GetMethod("LoadImage", new Type[] { typeof(Texture2D), typeof(byte[]), typeof(bool) }));
                    })
                                          .Where(x => x != null)
                                          .FirstOrDefault();
                }

                if (loadImageMethodInfo == null)
                {
                    Debug.LogError("No LoadMethod was found in either UnityEngine.Texture2D or UnityEngine.ImageConversion. All Odin editor icons will be broken.");
                    tryLoadImage = (w, h, b) =>
                    {
                        var t = new Texture2D(1, 1);
                        t.SetPixel(0, 0, new Color(1, 0, 1));
                        t.Apply();
                        return(t);
                    };
                }
                else
                {
                    if (loadImageMethodInfo.IsStatic())
                    {
                        if (loadImageMethodInfo.GetParameters().Count() == 3)
                        {
                            tryLoadImage = (w, h, b) =>
                            {
                                var tex = new Texture2D(w, h, TextureFormat.ARGB32, false, true);
                                loadImageMethodInfo.Invoke(null, new object[] { tex, b, false });
                                return(tex);
                            };
                        }
                        else
                        {
                            tryLoadImage = (w, h, b) =>
                            {
                                var tex = new Texture2D(w, h, TextureFormat.ARGB32, false, true);
                                loadImageMethodInfo.Invoke(null, new object[] { tex, b });
                                return(tex);
                            };
                        }
                    }
                    else
                    {
                        tryLoadImage = (w, h, b) =>
                        {
                            var tex = new Texture2D(w, h, TextureFormat.ARGB32, false, true);
                            loadImageMethodInfo.Invoke(tex, new object[] { b });
                            return(tex);
                        };
                    }
                }
            }

            return(tryLoadImage(width, height, bytes));
        }