#if (UNITY_EDITOR) using System; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using SimpleInventorySystem; using UnityEngine.UI; public class Photoshoot : MonoBehaviour { public string OutputPath; public new Camera camera; public GameObject items; private void Awake() { if (camera == null) { camera = GetComponent(); } if (String.IsNullOrEmpty(OutputPath)) { Debug.LogError("Output Path should not be empty"); } /*if(items != null) { SetActiveRecursively(items.transform, true); }*/ } static void SetActiveRecursively(Transform trans, bool active) { trans.gameObject.SetActive(active); if (trans.GetComponent() != null) { return; } foreach (Transform child in trans) { SetActiveRecursively(child, active); } } static void SetActiveRecursivelyUpward(Transform trans, bool active) { trans.gameObject.SetActive(active); if (trans.parent != null) { SetActiveRecursivelyUpward(trans.parent, active); } } private void Start() { } private void OnValidate() { SetActiveRecursively(items.transform, false); } public void ProcessScreenshots() { StartCoroutine(Screenshot()); } public void ProcessScreenshotsFaster() { ScreenshotFasterPlus(); } private IEnumerator Screenshot() { List gameObjects = new List(); List activeGameObjects = new List(); for (int i = 0; i < items.transform.childCount; i++) { GameObject obj = items.transform.GetChild(i).gameObject; ItemUI item = obj.GetComponentInChildren(); if (obj.activeSelf) { activeGameObjects.Add(obj); } obj.SetActive(false); if (item != null) { gameObjects.Add(obj); } } int count = gameObjects.Count; Debug.Log($"Screenshot: Found {count} items."); for (int i = 0; i < gameObjects.Count; i++) { GameObject obj = gameObjects[i]; ItemUI item = gameObjects[i].GetComponentInChildren(); obj.gameObject.SetActive(true); yield return null; TakeScreenshot($"{Application.dataPath}/{OutputPath}/{item.itemName}_icon.png"); yield return null; obj.gameObject.SetActive(false); Sprite s = AssetDatabase.LoadAssetAtPath($"Assets/{OutputPath}/{item.itemName}_icon.png"); if (s != null) { item.image.sprite = s; EditorUtility.SetDirty(item); } Debug.Log($"Screenshot: {i + 1}/{count}"); yield return null; } for (int i = 0; i < activeGameObjects.Count; i++) { GameObject obj = activeGameObjects[i]; obj.SetActive(true); } Debug.Log($"Screenshot: Complete"); } private void ScreenshotFaster() { List gameObjects = new List(); List activeGameObjects = new List(); for (int i = 0; i < items.transform.childCount; i++) { GameObject obj = items.transform.GetChild(i).gameObject; ItemUI item = obj.GetComponentInChildren(); if (obj.activeSelf) { activeGameObjects.Add(obj); } obj.SetActive(false); if (item != null) { gameObjects.Add(obj); } } int count = gameObjects.Count; Debug.Log($"Screenshot: Found {count} items."); for (int i = 0; i < gameObjects.Count; i++) { GameObject obj = gameObjects[i]; ItemUI item = gameObjects[i].GetComponentInChildren(); TakeScreenshotFaster($"{Application.dataPath}/{OutputPath}/{item.itemName}_icon.png", obj); // TODO: Hopefully find a way to automatically assign to prefab /* Sprite s = AssetDatabase.LoadAssetAtPath($"Assets/{OutputPath}/{item.itemName}_icon.png"); if (s != null) { item.transform.Find("Image").GetComponent().sprite = s; EditorUtility.SetDirty(item); } Debug.Log($"Screenshot: {i + 1}/{count}");*/ } for (int i = 0; i < activeGameObjects.Count; i++) { GameObject obj = activeGameObjects[i]; obj.SetActive(true); } Debug.Log($"Screenshot: Complete"); } private void ScreenshotFasterPlus() { if (items != null) { items.SetActive(true); SetActiveRecursively(items.transform, true); } ItemUI[] itemUIs = items.GetComponentsInChildren(); if (items != null) { SetActiveRecursively(items.transform, false); items.SetActive(true); } Debug.Log($"Screenshot: Found {itemUIs.Length} items."); for (int i = 0; i < itemUIs.Length; i++) { if (items != null) { //items.SetActive(true); } ItemUI item = itemUIs[i].GetComponentInChildren(); GameObject obj = item.transform.parent.gameObject; // This assumed ItemUIs are correctly parented. TakeScreenshotFaster($"{Application.dataPath}/{OutputPath}/{item.itemName}_icon.png", obj); } Debug.Log($"Screenshot: Complete"); } void TakeScreenshotFaster(string fullPath, GameObject obj) { SetActiveRecursivelyUpward(obj.transform, true); SetActiveRecursively(obj.transform, true); RenderTexture rt = new RenderTexture(256, 256, 24); camera.targetTexture = rt; Texture2D screenShot = new Texture2D(256, 256, TextureFormat.RGBA32, false); camera.Render(); RenderTexture.active = rt; screenShot.ReadPixels(new Rect(0, 0, 256, 256), 0, 0); camera.targetTexture = null; RenderTexture.active = null; if (Application.isEditor) { DestroyImmediate(rt); } else { Destroy(rt); } byte[] bytes = screenShot.EncodeToPNG(); System.IO.File.WriteAllBytes(fullPath, bytes); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif SetActiveRecursively(obj.transform, false); SetActiveRecursivelyUpward(obj.transform, false); } void TakeScreenshot(string fullPath) { RenderTexture rt = new RenderTexture(256, 256, 24); camera.targetTexture = rt; Texture2D screenShot = new Texture2D(256, 256, TextureFormat.RGBA32, false); camera.Render(); RenderTexture.active = rt; screenShot.ReadPixels(new Rect(0, 0, 256, 256), 0, 0); camera.targetTexture = null; RenderTexture.active = null; if (Application.isEditor) { DestroyImmediate(rt); } else { Destroy(rt); } byte[] bytes = screenShot.EncodeToPNG(); System.IO.File.WriteAllBytes(fullPath, bytes); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } } #endif