Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.1 kB
4
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using System.IO;

public class IconCreator : MonoBehaviour
{
    [SerializeField] List<IconDataClass> iconDataClassList;
    [SerializeField] Camera cam;
    [SerializeField] int width, height;

    private void Start()
    {
        if (cam == null)
        {
            cam = Camera.main;
        }
        CreateIcon();
    }

    void CreateIcon()
    {
        print("start Create Icon");
        foreach (IconDataClass iconDataClass in iconDataClassList)
        {
            //設定camera位置、燈光、素體
            GameObject character = null;
            cam.transform.position = iconDataClass.CameraTransform.position;
            cam.transform.rotation = iconDataClass.CameraTransform.rotation;
            iconDataClass.LightSetting.SetActive(true);
            if (iconDataClass.IsCreateCharacter)
            {
                character = Instantiate(iconDataClass.CharacterPrefab);
            }
            //存檔資訊
            string savePath = iconDataClass.SavePath;

            foreach (GameObject prefab in iconDataClass.PrefabList)
            {
                //生成物件
                GameObject currentObject = Instantiate(prefab);
                string fileNameFormat = iconDataClass.ClassName + prefab.name;
                print("Capturing"+fileNameFormat);
                //截圖
                CaptureScreenshot(savePath, fileNameFormat, currentObject);
                
            }
            //iconDataClass.LightSetting.SetActive(false);
            if (character != null)
            {
                Destroy(character);
            }
            print("end Create Icon");
        }
    }

    private void  CaptureScreenshot(string savePath, string fileNameFormat,GameObject currentObject)
    {
        print("start CaptureScreenshot");
        string directoryPath = Path.Combine(Application.dataPath, savePath);

        // Ensure the save directory exists
        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
        }

        // Generate the file name
        string filePath = Path.Combine(directoryPath, fileNameFormat + ".png");

        // Capture the screenshot
        ScreenCapture.CaptureScreenshot(filePath, SuperSize(width, height));

        Debug.Log($"Screenshot saved to: {filePath}");
        print("end CaptureScreenshotCoroutine");
        Destroy(currentObject);
    }

    // Calculate superSize based on target resolution and current screen resolution
    private int SuperSize(int targetWidth, int targetHeight)
    {
        int screenWidth = Screen.width;
        int screenHeight = Screen.height;

        float widthRatio = (float)targetWidth / screenWidth;
        float heightRatio = (float)targetHeight / screenHeight;

        return Mathf.CeilToInt(Mathf.Max(widthRatio, heightRatio));
    }
}
Leave a Comment