projectEli/Assets/Inventory System/Scripts/Photoshoot.cs
2023-11-03 21:02:30 -04:00

274 lines
7.5 KiB
C#

#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<Camera>();
}
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<ItemUI>() != 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<GameObject> gameObjects = new List<GameObject>();
List<GameObject> activeGameObjects = new List<GameObject>();
for (int i = 0; i < items.transform.childCount; i++)
{
GameObject obj = items.transform.GetChild(i).gameObject;
ItemUI item = obj.GetComponentInChildren<ItemUI>();
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<ItemUI>();
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<Sprite>($"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<GameObject> gameObjects = new List<GameObject>();
List<GameObject> activeGameObjects = new List<GameObject>();
for (int i = 0; i < items.transform.childCount; i++)
{
GameObject obj = items.transform.GetChild(i).gameObject;
ItemUI item = obj.GetComponentInChildren<ItemUI>();
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<ItemUI>();
TakeScreenshotFaster($"{Application.dataPath}/{OutputPath}/{item.itemName}_icon.png", obj);
// TODO: Hopefully find a way to automatically assign to prefab
/* Sprite s = AssetDatabase.LoadAssetAtPath<Sprite>($"Assets/{OutputPath}/{item.itemName}_icon.png");
if (s != null)
{
item.transform.Find("Image").GetComponent<UnityEngine.UI.Image>().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<ItemUI>();
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<ItemUI>();
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