private void OnEnable() { Reset(); if (Selection.activeObject != null && (Selection.activeObject is Texture || Selection.activeObject is Sprite)) { Sprite sp = Selection.activeObject as Sprite; if (sp != null) { m_sourceTexture = sp.texture; } else { m_sourceTexture = Selection.activeObject as Texture; } } m_bgTexture = RenderTexture.GetTemporary(256, 256, 0, RenderTextureFormat.ARGB32); m_mat = new Material(Shader.Find("ASTC/ColorMask")); for (EChanelMask i = 0; i < EChanelMask.Max; ++i) { channel_button_texts[(int)i] = i.ToString(); } if (m_sourceTexture == null) { return; } m_encodeType = m_willEncodeType = EASTCENCODE.None; m_quality = m_willQuality = ECompressQuality.QUALITY_MEDIUM; InitCaculateTextureShow(); }
private void Reset() { m_scale = 1; m_sourceTexture = null; m_previewTexture = null; m_willEncodeType = EASTCENCODE.None; }
private bool TryRefreshEncode(EASTCENCODE t, out Texture image) { image = null; if (m_sourceTexture == null || !m_previewDirty) { return(false); } int sw_alpha = 4; bool need_pack = false; switch (t) { case EASTCENCODE.None: //show the source need_pack = false; break; case EASTCENCODE.E4x4: case EASTCENCODE.E5x5: case EASTCENCODE.E6x6: case EASTCENCODE.E8x8: case EASTCENCODE.E10x10: case EASTCENCODE.E12x12: need_pack = true; break; default: EditorUtility.DisplayDialog("提示", "暂不支持该格式", "确定"); break; } if (need_pack) { string file_path = GetSourceTextureFullPath(); var ptr = ASTCWrap.pack_and_unpack_image(file_path, (int)t, 0, sw_alpha, 0); image = DecodeImageData(ptr); } return(need_pack); }
private void OnGUI() { bool need_repaint = false; if (m_sourceTexture == null) { m_sourceTexture = EditorGUILayout.ObjectField("放置预览texture", m_sourceTexture, typeof(Texture), false) as Texture; if (m_sourceTexture != null) { InitCaculateTextureShow(); } } else { if (Event.current.type == EventType.Repaint) { EditorGUI.DrawTextureTransparent(new Rect(0, 0, this.position.width, this.position.height), m_bgTexture); } GUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.Width(this.position.width)); { if (GUILayout.Button("Reset", EditorStyles.toolbarButton, GUILayout.Width(50))) { Reset(); Repaint(); return; } m_mask = (EChanelMask)GUILayout.SelectionGrid((int)m_mask, channel_button_texts, channel_button_texts.Length, EditorStyles.toolbarButton, GUILayout.Width(180)); m_willEncodeType = (EASTCENCODE)EditorGUILayout.EnumPopup(m_willEncodeType, EditorStyles.toolbarPopup, GUILayout.Width(80)); if (m_willEncodeType != EASTCENCODE.None) { m_willQuality = (ECompressQuality)EditorGUILayout.EnumPopup(m_willQuality, EditorStyles.toolbarPopup, GUILayout.Width(80)); } if (GUILayout.Button("PreView", EditorStyles.toolbarButton, GUILayout.Width(100))) { Texture image; if (TryRefreshEncode(m_willEncodeType, out image)) { m_encodeType = m_willEncodeType; m_quality = m_willQuality; //show the astc m_previewTexture = image; } } } GUILayout.EndHorizontal(); if (Event.current.type == EventType.MouseDrag) { m_scrollVec.x = m_scrollVec.x - Event.current.delta.x; m_scrollVec.y = m_scrollVec.y - Event.current.delta.y; need_repaint = true; } else if (Event.current.type == EventType.ScrollWheel) { m_scale += Event.current.delta.y * 0.01f; need_repaint = true; } Color col = new Color(); m_mat.SetInt("_AlphaToggle", 1); switch (m_mask) { case EChanelMask.R: col.r = 1; col.g = 0; col.b = 0; col.a = 1; break; case EChanelMask.G: col.r = 0; col.g = 1; col.b = 0; col.a = 1; break; case EChanelMask.B: col.r = 0; col.g = 0; col.b = 1; col.a = 1; break; case EChanelMask.RGB: col.r = 1; col.g = 1; col.b = 1; col.a = 1; m_mat.SetInt("_AlphaToggle", 0); break; case EChanelMask.SO: col.r = 1; col.g = 1; col.b = 1; col.a = 1; break; default: col.r = col.r = col.b = col.a = 1; break; } m_mat.SetColor("_Color", col); //自定义材质,需手动开启裁剪 m_mat.EnableKeyword("UNITY_UI_CLIP_RECT"); m_mat.SetVector("_ClipRect", new Vector4(0, 0, this.position.width, this.position.height)); float content_width = m_previewTexture.width * m_scale; float content_height = m_previewTexture.height * m_scale; float x = -m_scrollVec.x; float y = 25 - m_scrollVec.y; m_textureRect = new Rect(x, y, content_width, content_height); GUI.BeginClip(new Rect(0, 16, this.position.width, this.position.height)); EditorGUI.DrawPreviewTexture(m_textureRect, m_previewTexture, m_mat, ScaleMode.StretchToFill, 0, 0); EditorGUI.DrawTextureAlpha(new Rect(m_textureRect.x, m_textureRect.y + m_textureRect.height + 10, m_textureRect.width, m_textureRect.height), m_previewTexture, ScaleMode.StretchToFill, 0, 0); GUI.EndClip(); float right = Math.Max(content_width + 30, this.position.width); float left = -30; m_scrollVec.x = GUI.HorizontalScrollbar(new Rect(0, this.position.height - 16, this.position.width - 16, 16), m_scrollVec.x, this.position.width, left, right); right = Math.Max(content_height * 2 + 70, this.position.height); left = -30; m_scrollVec.y = GUI.VerticalScrollbar(new Rect(this.position.width - 16, 0, 16, this.position.height), m_scrollVec.y, this.position.height, left, right); } if (need_repaint) { Repaint(); } }