207 lines
5.6 KiB
C#
207 lines
5.6 KiB
C#
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<Camera>();
|
|
}
|
|
|
|
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<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");
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|