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 string itemPrefabPath; public 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"); } } private void OnValidate() { } public void ProcessScreenshots() { StartCoroutine(Screenshot()); } public void ProcessScreenshotsFaster() { ScreenshotFaster(); } 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"); } void TakeScreenshotFaster(string fullPath, GameObject obj) { obj.SetActive(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 obj.SetActive(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 } }