|
4 | 4 | using UnityExplorer.Helpers;
|
5 | 5 | using UnityExplorer.UI;
|
6 | 6 | using UnityEngine.UI;
|
| 7 | +using UnityExplorer.Unstrip; |
| 8 | +using System.IO; |
7 | 9 |
|
8 | 10 | namespace UnityExplorer.Inspectors.Reflection
|
9 | 11 | {
|
@@ -70,8 +72,8 @@ public void ConstructInstanceHelpers()
|
70 | 72 |
|
71 | 73 | // WIP
|
72 | 74 |
|
73 |
| - //if (m_targetType == typeof(Texture2D)) |
74 |
| - // ConstructTextureHelper(); |
| 75 | + if (m_targetType == typeof(Texture2D)) |
| 76 | + ConstructTextureHelper(); |
75 | 77 | }
|
76 | 78 |
|
77 | 79 | internal void ConstructCompHelper(GameObject rowObj)
|
@@ -136,94 +138,169 @@ internal void ConstructUObjHelper(GameObject rowObj)
|
136 | 138 | //btn.onClick.AddListener(() => { InspectorManager.Instance.Inspect(comp.gameObject); });
|
137 | 139 | }
|
138 | 140 |
|
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 | + } |
227 | 304 |
|
228 | 305 | public void ConstructInstanceFilters(GameObject parent)
|
229 | 306 | {
|
|
0 commit comments