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); }
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); }
static int DrawSelectionElement(Rect r, int i, int index, Texture2DArray texArray, string label, int elemSize) { if (GUI.Button(r, "", GUI.skin.box)) { index = i; } int hash = texArray.GetHashCode() * 7 + i * 31; Texture2D image = null; bool found = cachedSelectionImages.TryGetValue(hash, out image); if (!found || image == null) { var tmp = new Texture2D(texArray.width, texArray.height, texArray.format, false, false); Graphics.CopyTexture(texArray, i, 0, tmp, 0, 0); tmp.Apply(); RenderTexture rt = RenderTexture.GetTemporary(128, 128, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); Graphics.Blit(tmp, rt); RenderTexture.active = rt; image = new Texture2D(128, 128, TextureFormat.RGBA32, true, false); image.ReadPixels(new Rect(0, 0, 128, 128), 0, 0); image.Apply(); cachedSelectionImages[hash] = image; RenderTexture.active = null; RenderTexture.ReleaseTemporary(rt); } GUI.DrawTexture(r, image, ScaleMode.ScaleToFit, false); if (i == index) { GUI.DrawTexture(r, selectedTex, ScaleMode.ScaleToFit, true); } if (!string.IsNullOrEmpty(label)) { r.height = 18; var v = r.center; v.y += elemSize - 18; r.center = v; Color contentColor = GUI.contentColor; GUI.DrawTexture(r, labelBackgroundTex, ScaleMode.StretchToFill); GUI.contentColor = EditorGUIUtility.isProSkin ? Color.white : Color.black; GUI.Box(r, label); GUI.contentColor = contentColor; } return(index); }
/// <summary> /// A representative hash code. /// </summary> /// <returns>A number that is guaranteed to be the same when generated from two objects that are the same.</returns> public override int GetHashCode() { int hash = 0; if (!ReferenceEquals(colors, null)) { hash ^= colors.GetHashCode(); } if (!ReferenceEquals(directions, null)) { hash ^= directions.GetHashCode(); } if (!ReferenceEquals(shadowMasks, null)) { hash ^= shadowMasks.GetHashCode(); } return(hash); }
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); }