2022-12-11 03:03:30 +00:00
|
|
|
|
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 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");
|
|
|
|
|
}
|
2023-01-22 12:10:22 +00:00
|
|
|
|
|
|
|
|
|
/*if(items != null)
|
|
|
|
|
{
|
|
|
|
|
SetActiveRecursively(items.transform, true);
|
|
|
|
|
}*/
|
2022-12-11 03:03:30 +00:00
|
|
|
|
}
|
2023-01-22 12:10:22 +00:00
|
|
|
|
|
|
|
|
|
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)
|
2022-12-11 03:03:30 +00:00
|
|
|
|
{
|
2023-01-22 12:10:22 +00:00
|
|
|
|
trans.gameObject.SetActive(active);
|
|
|
|
|
if (trans.parent != null)
|
|
|
|
|
{
|
|
|
|
|
SetActiveRecursivelyUpward(trans.parent, active);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-11 03:03:30 +00:00
|
|
|
|
|
2023-01-22 12:10:22 +00:00
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
SetActiveRecursively(items.transform, false);
|
2022-12-11 03:03:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ProcessScreenshots()
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(Screenshot());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ProcessScreenshotsFaster()
|
|
|
|
|
{
|
2023-01-22 12:10:22 +00:00
|
|
|
|
ScreenshotFasterPlus();
|
2022-12-11 03:03:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
2023-01-22 12:10:22 +00:00
|
|
|
|
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");
|
|
|
|
|
}
|
2022-12-11 03:03:30 +00:00
|
|
|
|
|
|
|
|
|
void TakeScreenshotFaster(string fullPath, GameObject obj)
|
|
|
|
|
{
|
2023-01-22 12:10:22 +00:00
|
|
|
|
SetActiveRecursivelyUpward(obj.transform, true);
|
|
|
|
|
SetActiveRecursively(obj.transform, true);
|
2022-12-11 03:03:30 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-01-22 12:10:22 +00:00
|
|
|
|
SetActiveRecursively(obj.transform, false);
|
|
|
|
|
SetActiveRecursivelyUpward(obj.transform, false);
|
2022-12-11 03:03:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|