private static Texture2D GetCachedTexture(Texture2DArray ta, int textureIndex)
        {
            var hash   = ta.GetHashCode() * (textureIndex + 7);
            var hashed = FindInPreviewCache(hash);

            if (hashed != null)
            {
                return(hashed);
            }

            hashed = new Texture2D(ta.width, ta.height, ta.format, false);
            Graphics.CopyTexture(ta, textureIndex, 0, hashed, 0, 0);
            hashed.Apply(false, false);

            var hd = new TextureArrayPreviewCache
            {
                Hash    = hash,
                Texture = hashed
            };

            PreviewCache.Add(hd);
            if (PreviewCache.Count > 100)
            {
                hd = PreviewCache[0];
                PreviewCache.RemoveAt(0);
                if (hd.Texture != null)
                {
                    DestroyImmediate(hd.Texture);
                }
            }

            return(hashed);
        }
Esempio n. 2
0
        public static int DrawTextureSelector(int textureIndex, Texture2DArray ta, bool compact = false)
        {
            if (ta == null)
            {
                return(textureIndex);
            }
            int count = ta.depth;

            if (count > 16)
            {
                count = 16;
            }
            Texture2D disp = Texture2D.blackTexture;

            if (ta != null)
            {
                int       hash   = ta.GetHashCode() * (textureIndex + 7);
                Texture2D hashed = FindInPreviewCache(hash);
                if (hashed == null)
                {
                    hashed = new Texture2D(ta.width, ta.height, ta.format, false);
                    Graphics.CopyTexture(ta, textureIndex, 0, hashed, 0, 0);
                    hashed.Apply(false, false);
                    //FixUnityEditorLinearBug(ref hashed);
                    var hd = new TextureArrayPreviewCache();
                    hd.hash    = hash;
                    hd.texture = hashed;
                    previewCache.Add(hd);
                    if (previewCache.Count > 20)
                    {
                        hd = previewCache[0];
                        previewCache.RemoveAt(0);
                        if (hd.texture != null)
                        {
                            GameObject.DestroyImmediate(hd.texture);
                        }
                    }
                }
                disp = hashed;
            }
            if (compact)
            {
                EditorGUILayout.BeginVertical();
                EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(GUILayout.Width(110), GUILayout.Height(96)), disp);
                textureIndex = EditorGUILayout.IntSlider(textureIndex, 0, count - 1, GUILayout.Width(120));
                EditorGUILayout.EndVertical();
            }
            else
            {
                textureIndex = EditorGUILayout.IntSlider("index", textureIndex, 0, count - 1);
                EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(GUILayout.Width(128), GUILayout.Height(128)), disp);
            }
            return(textureIndex);
        }
        public static int DrawTextureSelector(int textureIndex, Texture2DArray ta, bool compact = false)
        {
            if (ta == null)
            {
                return(textureIndex);
            }
            int count = ta.depth;

            if (count > 32)
            {
                count = 32;
            }

            if (cachedArray != ta)
            {
                cachedArray = ta;
                var path = AssetDatabase.GetAssetPath(ta);
                path         = path.Replace("_diff_tarray", "");
                cachedConfig = AssetDatabase.LoadAssetAtPath <TextureArrayConfig> (path);
            }

            Texture2D disp = Texture2D.blackTexture;

            if (ta != null)
            {
                int       hash   = ta.GetHashCode() * (textureIndex + 7);
                Texture2D hashed = FindInPreviewCache(hash);
                if (hashed == null)
                {
                    hashed = new Texture2D(ta.width, ta.height, ta.format, false);
                    Graphics.CopyTexture(ta, textureIndex, 0, hashed, 0, 0);
                    hashed.Apply(false, false);
                    //FixUnityEditorLinearBug(ref hashed);
                    var hd = new TextureArrayPreviewCache();
                    hd.hash    = hash;
                    hd.texture = hashed;
                    previewCache.Add(hd);
                    if (previewCache.Count > 20)
                    {
                        hd = previewCache[0];
                        previewCache.RemoveAt(0);
                        if (hd.texture != null)
                        {
                            GameObject.DestroyImmediate(hd.texture);
                        }
                    }
                }
                disp = hashed;
            }
            if (compact)
            {
                EditorGUILayout.BeginVertical();
                EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(GUILayout.Width(110), GUILayout.Height(96)), disp);
                textureIndex = EditorGUILayout.IntSlider(textureIndex, 0, count - 1, GUILayout.Width(120));
                EditorGUILayout.EndVertical();
            }
            else
            {
                string name = "";
                if (cachedConfig != null)
                {
                    if (textureIndex < cachedConfig.sourceTextures.Count && textureIndex >= 0)
                    {
                        var conf = cachedConfig.sourceTextures [textureIndex];
                        if (conf != null && conf.diffuse != null)
                        {
                            name = conf.diffuse.name;
                        }
                    }
                }

                Rect r = EditorGUILayout.GetControlRect(false, GUILayout.Height(128));
                r.width -= 2;
                float width = r.width;


                r.width = 128;
                EditorGUI.DrawPreviewTexture(r, disp);
                r.height = 20;
                r.width  = width - 148;
                r.x     += 148;
                r.y     += 36;

                Rect ridx = r;
                ridx.y += 20;

                ridx.width  -= 44;
                textureIndex = EditorGUI.IntSlider(ridx, textureIndex, 0, count - 1);
                if (textureIndex <= 0)
                {
                    textureIndex = 0;
                    GUI.enabled  = false;
                }
                else
                {
                    GUI.enabled = true;
                }
                ridx.x    += ridx.width;
                ridx.x    += 4;
                ridx.width = 22;
                if (GUI.Button(ridx, "<"))
                {
                    textureIndex -= 1;
                }
                if (textureIndex >= count - 1)
                {
                    textureIndex = count - 1;
                    GUI.enabled  = false;
                }
                else
                {
                    GUI.enabled = true;
                }
                ridx.x += 22;
                if (GUI.Button(ridx, ">"))
                {
                    textureIndex += 1;
                }
                GUI.enabled = true;

                EditorGUI.LabelField(r, name);
            }
            return(textureIndex);
        }