Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 28181e2

Browse files
committed
Restoring Texture viewer/saver, and Static/Singleton class searching
1 parent 6dfa480 commit 28181e2

File tree

7 files changed

+372
-154
lines changed

7 files changed

+372
-154
lines changed

src/Config/ModConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static ModConfig()
2222
public KeyCode Main_Menu_Toggle = KeyCode.F7;
2323
public bool Force_Unlock_Mouse = true;
2424
public int Default_Page_Limit = 25;
25-
public string Default_Output_Path = ExplorerCore.EXPLORER_FOLDER;
25+
public string Default_Output_Path = ExplorerCore.EXPLORER_FOLDER + @"\Output";
2626
public bool Log_Unity_Debug = false;
2727
public bool Save_Logs_To_Disk = true;
2828

src/ExplorerCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace UnityExplorer
1616
public class ExplorerCore
1717
{
1818
public const string NAME = "UnityExplorer";
19-
public const string VERSION = "3.0.8";
19+
public const string VERSION = "3.1.0";
2020
public const string AUTHOR = "Sinai";
2121
public const string GUID = "com.sinai.unityexplorer";
2222
public const string EXPLORER_FOLDER = @"Mods\UnityExplorer";

src/Inspectors/Reflection/InstanceInspector.cs

Lines changed: 167 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using UnityExplorer.Helpers;
55
using UnityExplorer.UI;
66
using UnityEngine.UI;
7+
using UnityExplorer.Unstrip;
8+
using System.IO;
79

810
namespace UnityExplorer.Inspectors.Reflection
911
{
@@ -70,8 +72,8 @@ public void ConstructInstanceHelpers()
7072

7173
// WIP
7274

73-
//if (m_targetType == typeof(Texture2D))
74-
// ConstructTextureHelper();
75+
if (m_targetType == typeof(Texture2D))
76+
ConstructTextureHelper();
7577
}
7678

7779
internal void ConstructCompHelper(GameObject rowObj)
@@ -136,94 +138,169 @@ internal void ConstructUObjHelper(GameObject rowObj)
136138
//btn.onClick.AddListener(() => { InspectorManager.Instance.Inspect(comp.gameObject); });
137139
}
138140

139-
//internal bool showingTextureHelper;
140-
//internal bool constructedTextureViewer;
141-
142-
//internal void ConstructTextureHelper()
143-
//{
144-
// var rowObj = UIFactory.CreateHorizontalGroup(Content, new Color(0.1f, 0.1f, 0.1f));
145-
// var rowLayout = rowObj.AddComponent<LayoutElement>();
146-
// rowLayout.minHeight = 25;
147-
// rowLayout.flexibleHeight = 0;
148-
// var rowGroup = rowObj.GetComponent<HorizontalLayoutGroup>();
149-
// rowGroup.childForceExpandHeight = true;
150-
// rowGroup.childForceExpandWidth = false;
151-
// rowGroup.padding.top = 3;
152-
// rowGroup.padding.left = 3;
153-
// rowGroup.padding.bottom = 3;
154-
// rowGroup.padding.right = 3;
155-
// rowGroup.spacing = 5;
156-
157-
// var showBtnObj = UIFactory.CreateButton(rowObj, new Color(0.2f, 0.2f, 0.2f));
158-
// var showBtnLayout = showBtnObj.AddComponent<LayoutElement>();
159-
// showBtnLayout.minWidth = 50;
160-
// showBtnLayout.flexibleWidth = 0;
161-
// var showText = showBtnObj.GetComponentInChildren<Text>();
162-
// showText.text = "Show";
163-
// var showBtn = showBtnObj.GetComponent<Button>();
164-
165-
// var labelObj = UIFactory.CreateLabel(rowObj, TextAnchor.MiddleLeft);
166-
// var labelText = labelObj.GetComponent<Text>();
167-
// labelText.text = "Texture Viewer";
168-
169-
// var textureViewerObj = UIFactory.CreateScrollView(Content, out GameObject scrollContent, out _, new Color(0.1f, 0.1f, 0.1f));
170-
// var viewerGroup = scrollContent.GetComponent<VerticalLayoutGroup>();
171-
// viewerGroup.childForceExpandHeight = false;
172-
// viewerGroup.childForceExpandWidth = false;
173-
// viewerGroup.childControlHeight = true;
174-
// viewerGroup.childControlWidth = true;
175-
// var mainLayout = textureViewerObj.GetComponent<LayoutElement>();
176-
// mainLayout.flexibleHeight = -1;
177-
// mainLayout.flexibleWidth = 2000;
178-
// mainLayout.minHeight = 25;
179-
180-
// textureViewerObj.SetActive(false);
181-
182-
// showBtn.onClick.AddListener(() =>
183-
// {
184-
// showingTextureHelper = !showingTextureHelper;
185-
186-
// if (showingTextureHelper)
187-
// {
188-
// if (!constructedTextureViewer)
189-
// ConstructTextureViewerArea(scrollContent);
190-
191-
// showText.text = "Hide";
192-
// textureViewerObj.SetActive(true);
193-
// }
194-
// else
195-
// {
196-
// showText.text = "Show";
197-
// textureViewerObj.SetActive(false);
198-
// }
199-
// });
200-
//}
201-
202-
//internal void ConstructTextureViewerArea(GameObject parent)
203-
//{
204-
// constructedTextureViewer = true;
205-
206-
// var tex = Target as Texture2D;
207-
208-
// if (!tex)
209-
// {
210-
// ExplorerCore.LogWarning("Could not cast the target instance to Texture2D!");
211-
// return;
212-
// }
213-
214-
// var imageObj = UIFactory.CreateUIObject("TextureViewerImage", parent, new Vector2(1, 1));
215-
// var image = imageObj.AddComponent<Image>();
216-
// var sprite = UIManager.CreateSprite(tex);
217-
// image.sprite = sprite;
218-
219-
// var fitter = imageObj.AddComponent<ContentSizeFitter>();
220-
// fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
221-
// //fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
222-
223-
// var imageLayout = imageObj.AddComponent<LayoutElement>();
224-
// imageLayout.preferredHeight = sprite.rect.height;
225-
// imageLayout.preferredWidth = sprite.rect.width;
226-
//}
141+
internal bool showingTextureHelper;
142+
internal bool constructedTextureViewer;
143+
144+
internal GameObject m_textureViewerObj;
145+
146+
internal void ConstructTextureHelper()
147+
{
148+
var rowObj = UIFactory.CreateHorizontalGroup(Content, new Color(0.1f, 0.1f, 0.1f));
149+
var rowLayout = rowObj.AddComponent<LayoutElement>();
150+
rowLayout.minHeight = 25;
151+
rowLayout.flexibleHeight = 0;
152+
var rowGroup = rowObj.GetComponent<HorizontalLayoutGroup>();
153+
rowGroup.childForceExpandHeight = true;
154+
rowGroup.childForceExpandWidth = false;
155+
rowGroup.padding.top = 3;
156+
rowGroup.padding.left = 3;
157+
rowGroup.padding.bottom = 3;
158+
rowGroup.padding.right = 3;
159+
rowGroup.spacing = 5;
160+
161+
var showBtnObj = UIFactory.CreateButton(rowObj, new Color(0.2f, 0.6f, 0.2f));
162+
var showBtnLayout = showBtnObj.AddComponent<LayoutElement>();
163+
showBtnLayout.minWidth = 50;
164+
showBtnLayout.flexibleWidth = 0;
165+
var showText = showBtnObj.GetComponentInChildren<Text>();
166+
showText.text = "Show";
167+
var showBtn = showBtnObj.GetComponent<Button>();
168+
169+
var labelObj = UIFactory.CreateLabel(rowObj, TextAnchor.MiddleLeft);
170+
var labelText = labelObj.GetComponent<Text>();
171+
labelText.text = "Texture Viewer";
172+
173+
var textureViewerObj = UIFactory.CreateScrollView(Content, out GameObject scrollContent, out _, new Color(0.1f, 0.1f, 0.1f));
174+
var viewerGroup = scrollContent.GetComponent<VerticalLayoutGroup>();
175+
viewerGroup.childForceExpandHeight = false;
176+
viewerGroup.childForceExpandWidth = false;
177+
viewerGroup.childControlHeight = true;
178+
viewerGroup.childControlWidth = true;
179+
var mainLayout = textureViewerObj.GetComponent<LayoutElement>();
180+
mainLayout.flexibleHeight = 9999;
181+
mainLayout.flexibleWidth = 9999;
182+
mainLayout.minHeight = 100;
183+
184+
textureViewerObj.SetActive(false);
185+
186+
m_textureViewerObj = textureViewerObj;
187+
188+
showBtn.onClick.AddListener(() =>
189+
{
190+
showingTextureHelper = !showingTextureHelper;
191+
192+
if (showingTextureHelper)
193+
{
194+
if (!constructedTextureViewer)
195+
ConstructTextureViewerArea(scrollContent);
196+
197+
showText.text = "Hide";
198+
ToggleTextureViewer(true);
199+
}
200+
else
201+
{
202+
showText.text = "Show";
203+
ToggleTextureViewer(false);
204+
}
205+
});
206+
}
207+
208+
internal void ConstructTextureViewerArea(GameObject parent)
209+
{
210+
constructedTextureViewer = true;
211+
212+
var tex = Target as Texture2D;
213+
#if CPP
214+
if (!tex)
215+
tex = (Target as Il2CppSystem.Object).TryCast<Texture2D>();
216+
#endif
217+
218+
if (!tex)
219+
{
220+
ExplorerCore.LogWarning("Could not cast the target instance to Texture2D! Maybe its null or destroyed?");
221+
return;
222+
}
223+
224+
// Save helper
225+
226+
var saveRowObj = UIFactory.CreateHorizontalGroup(parent, new Color(0.1f, 0.1f, 0.1f));
227+
var saveRow = saveRowObj.GetComponent<HorizontalLayoutGroup>();
228+
saveRow.childForceExpandHeight = true;
229+
saveRow.childForceExpandWidth = true;
230+
saveRow.padding = new RectOffset() { left = 2, bottom = 2, right = 2, top = 2 };
231+
saveRow.spacing = 2;
232+
233+
var btnObj = UIFactory.CreateButton(saveRowObj, new Color(0.2f, 0.2f, 0.2f));
234+
var btnLayout = btnObj.AddComponent<LayoutElement>();
235+
btnLayout.minHeight = 25;
236+
btnLayout.minWidth = 100;
237+
btnLayout.flexibleWidth = 0;
238+
var saveBtn = btnObj.GetComponent<Button>();
239+
240+
var saveBtnText = btnObj.GetComponentInChildren<Text>();
241+
saveBtnText.text = "Save .PNG";
242+
243+
var inputObj = UIFactory.CreateInputField(saveRowObj);
244+
var inputLayout = inputObj.AddComponent<LayoutElement>();
245+
inputLayout.minHeight = 25;
246+
inputLayout.minWidth = 100;
247+
inputLayout.flexibleWidth = 9999;
248+
var inputField = inputObj.GetComponent<InputField>();
249+
250+
var name = tex.name;
251+
if (string.IsNullOrEmpty(name))
252+
name = "untitled";
253+
254+
var savePath = $@"{Config.ModConfig.Instance.Default_Output_Path}\{name}.png";
255+
inputField.text = savePath;
256+
257+
saveBtn.onClick.AddListener(() =>
258+
{
259+
if (tex && !string.IsNullOrEmpty(inputField.text))
260+
{
261+
var path = inputField.text;
262+
if (!path.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
263+
{
264+
ExplorerCore.LogWarning("Desired save path must end with '.png'!");
265+
return;
266+
}
267+
268+
var dir = Path.GetDirectoryName(path);
269+
if (!Directory.Exists(dir))
270+
Directory.CreateDirectory(dir);
271+
272+
if (File.Exists(path))
273+
File.Delete(path);
274+
275+
var data = tex.EncodeToPNG();
276+
File.WriteAllBytes(path, data);
277+
}
278+
});
279+
280+
// Actual texture viewer
281+
282+
var imageObj = UIFactory.CreateUIObject("TextureViewerImage", parent);
283+
var image = imageObj.AddComponent<Image>();
284+
var sprite = ImageConversionUnstrip.CreateSprite(tex);
285+
image.sprite = sprite;
286+
287+
var fitter = imageObj.AddComponent<ContentSizeFitter>();
288+
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
289+
//fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
290+
291+
var imageLayout = imageObj.AddComponent<LayoutElement>();
292+
imageLayout.preferredHeight = sprite.rect.height;
293+
imageLayout.preferredWidth = sprite.rect.width;
294+
}
295+
296+
internal void ToggleTextureViewer(bool enabled)
297+
{
298+
m_textureViewerObj.SetActive(enabled);
299+
300+
m_filterAreaObj.SetActive(!enabled);
301+
m_memberListObj.SetActive(!enabled);
302+
m_updateRowObj.SetActive(!enabled);
303+
}
227304

228305
public void ConstructInstanceFilters(GameObject parent)
229306
{

src/Inspectors/Reflection/ReflectionInspector.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ internal void UpdateWidths()
363363

364364
#region UI CONSTRUCTION
365365

366+
internal GameObject m_filterAreaObj;
367+
internal GameObject m_updateRowObj;
368+
internal GameObject m_memberListObj;
369+
366370
internal void ConstructUI()
367371
{
368372
var parent = InspectorManager.Instance.m_inspectorContent;
@@ -423,7 +427,7 @@ internal void ConstructTopArea()
423427

424428
ConstructFilterArea();
425429

426-
ConstructOptionsArea();
430+
ConstructUpdateRow();
427431
}
428432

429433
internal void ConstructFilterArea()
@@ -444,6 +448,8 @@ internal void ConstructFilterArea()
444448
filterGroup.padding.top = 4;
445449
filterGroup.padding.bottom = 4;
446450

451+
m_filterAreaObj = filterAreaObj;
452+
447453
// name filter
448454

449455
var nameFilterRowObj = UIFactory.CreateHorizontalGroup(filterAreaObj, new Color(1, 1, 1, 0));
@@ -540,7 +546,7 @@ private void AddFilterButton(GameObject parent, MemberTypes type, bool setEnable
540546
btn.colors = colors;
541547
}
542548

543-
internal void ConstructOptionsArea()
549+
internal void ConstructUpdateRow()
544550
{
545551
var optionsRowObj = UIFactory.CreateHorizontalGroup(Content, new Color(1, 1, 1, 0));
546552
var optionsLayout = optionsRowObj.AddComponent<LayoutElement>();
@@ -551,6 +557,8 @@ internal void ConstructOptionsArea()
551557
optionsGroup.childAlignment = TextAnchor.MiddleLeft;
552558
optionsGroup.spacing = 10;
553559

560+
m_updateRowObj = optionsRowObj;
561+
554562
// update button
555563

556564
var updateButtonObj = UIFactory.CreateButton(optionsRowObj, new Color(0.2f, 0.2f, 0.2f));
@@ -578,11 +586,12 @@ internal void ConstructOptionsArea()
578586
autoUpdateToggle.isOn = false;
579587
autoUpdateToggle.onValueChanged.AddListener((bool val) => { m_autoUpdate = val; });
580588
}
581-
589+
582590
internal void ConstructMemberList()
583591
{
584592
var scrollobj = UIFactory.CreateScrollView(Content, out m_scrollContent, out m_sliderScroller, new Color(0.05f, 0.05f, 0.05f));
585593

594+
m_memberListObj = scrollobj;
586595
m_scrollContentRect = m_scrollContent.GetComponent<RectTransform>();
587596

588597
var scrollGroup = m_scrollContent.GetComponent<VerticalLayoutGroup>();

0 commit comments

Comments
 (0)