private void DrawTextureControls() { GUIHelper.BeginHorizontal(); GUILayout.Label("Save folder:", new GUILayoutOption[] { GUILayout.Width(80f) }); saveFolder = GUIHelper.TextField(saveFolder, new GUILayoutOption[0]); GUIHelper.Space(10f); GUILayout.EndHorizontal(); if (GUILayout.Button("Save to PNG", new GUILayoutOption[] { GUILayout.Width(100f) })) { var name = RemoveInvalidFilenameChars(currentTex.name ?? ""); if (string.IsNullOrEmpty(name)) { if (OwnerCacheObject is CacheMember cacheMember) { name = cacheMember.MemInfo.Name; } else { name = "UNTITLED"; } } Texture2DHelpers.SaveTextureAsPNG(currentTex, saveFolder, name, false); ExplorerCore.Log($@"Saved to {saveFolder}\{name}.png!"); } }
public override void GetGUIContent() { // Check if the Sprite.textureRect is just the entire texture if (refSprite.textureRect != new Rect(0, 0, currentTex.width, currentTex.height)) { // It's not, do a sub-copy. currentTex = Texture2DHelpers.Copy(refSprite.texture, refSprite.textureRect); } base.GetGUIContent(); }
private static Color[] DecompressImage(Stream data, ushort width, ushort height, VTFImageFormat imageFormat) { Color[] vtfColors = new Color[width * height]; Texture2DHelpers.TextureFormat format; if (imageFormat == VTFImageFormat.IMAGE_FORMAT_DXT1 || imageFormat == VTFImageFormat.IMAGE_FORMAT_DXT1_ONEBITALPHA) { format = Texture2DHelpers.TextureFormat.DXT1; } else if (imageFormat == VTFImageFormat.IMAGE_FORMAT_DXT3) { format = Texture2DHelpers.TextureFormat.DXT3; } else if (imageFormat == VTFImageFormat.IMAGE_FORMAT_DXT5) { format = Texture2DHelpers.TextureFormat.DXT5; } else if (imageFormat == VTFImageFormat.IMAGE_FORMAT_BGR888) { format = Texture2DHelpers.TextureFormat.BGR888; } else if (imageFormat == VTFImageFormat.IMAGE_FORMAT_BGRA8888) { format = Texture2DHelpers.TextureFormat.BGRA8888; } else { format = Texture2DHelpers.TextureFormat.BGR888; Debug.LogError("SourceTexture: Unsupported format " + imageFormat + ", will read as " + format); } vtfColors = Texture2DHelpers.DecompressRawBytes(data, width, height, format); Texture2DHelpers.FlipVertical(vtfColors, width, height); return(vtfColors); }
internal void ConstructTextureViewerArea(GameObject parent) { constructedTextureViewer = true; var tex = Target as Texture2D; #if CPP if (!tex) { tex = (Target as Il2CppSystem.Object).TryCast <Texture2D>(); } #endif if (!tex) { ExplorerCore.LogWarning("Could not cast the target instance to Texture2D! Maybe its null or destroyed?"); return; } // Save helper var saveRowObj = UIFactory.CreateHorizontalGroup(parent, new Color(0.1f, 0.1f, 0.1f)); var saveRow = saveRowObj.GetComponent <HorizontalLayoutGroup>(); saveRow.childForceExpandHeight = true; saveRow.childForceExpandWidth = true; saveRow.padding = new RectOffset() { left = 2, bottom = 2, right = 2, top = 2 }; saveRow.spacing = 2; var btnObj = UIFactory.CreateButton(saveRowObj, new Color(0.2f, 0.2f, 0.2f)); var btnLayout = btnObj.AddComponent <LayoutElement>(); btnLayout.minHeight = 25; btnLayout.minWidth = 100; btnLayout.flexibleWidth = 0; var saveBtn = btnObj.GetComponent <Button>(); var saveBtnText = btnObj.GetComponentInChildren <Text>(); saveBtnText.text = "Save .PNG"; var inputObj = UIFactory.CreateInputField(saveRowObj); var inputLayout = inputObj.AddComponent <LayoutElement>(); inputLayout.minHeight = 25; inputLayout.minWidth = 100; inputLayout.flexibleWidth = 9999; var inputField = inputObj.GetComponent <InputField>(); var name = tex.name; if (string.IsNullOrEmpty(name)) { name = "untitled"; } var savePath = $@"{Config.ModConfig.Instance.Default_Output_Path}\{name}.png"; inputField.text = savePath; saveBtn.onClick.AddListener(() => { if (tex && !string.IsNullOrEmpty(inputField.text)) { var path = inputField.text; if (!path.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) { ExplorerCore.LogWarning("Desired save path must end with '.png'!"); return; } var dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (File.Exists(path)) { File.Delete(path); } if (!tex.IsReadable()) { tex = Texture2DHelpers.ForceReadTexture(tex); } #if CPP byte[] data = tex.EncodeToPNG(); #else byte[] data = tex.EncodeToPNGSafe(); #endif File.WriteAllBytes(path, data); } }); // Actual texture viewer var imageObj = UIFactory.CreateUIObject("TextureViewerImage", parent); var image = imageObj.AddComponent <Image>(); var sprite = ImageConversionUnstrip.CreateSprite(tex); image.sprite = sprite; var fitter = imageObj.AddComponent <ContentSizeFitter>(); fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; //fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; var imageLayout = imageObj.AddComponent <LayoutElement>(); imageLayout.preferredHeight = sprite.rect.height; imageLayout.preferredWidth = sprite.rect.width; }