using System; using System.Collections; using System.Collections.Generic; using System.IO; using HK; using UnityEngine; public class RenderPrintingManager : MonoBehaviour { public static RenderPrintingManager Instance; [SerializeField] Camera renderCam; [SerializeField] Transform parent; RenderPrintingManagerOption option; // RenderPrintingManagerModelOption modelOption; private RenderTexture rendtx; string dirPath = Path.Combine(Application.dataPath, "TestPic"); public string FolderPath; private void Awake() { Instance = this; var path = Path.Combine(Application.streamingAssetsPath, "PrintingSavePath.txt"); if (File.Exists(path) && !string.IsNullOrEmpty(File.ReadAllText(path))) dirPath = File.ReadAllText(path); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } } private void Update() { // if (Input.GetKeyDown(KeyCode.K)) // { // StartCoroutine(WaitSavePreview()); // } // // if (Input.GetKeyDown(KeyCode.L)) // { // StartCoroutine(WaitSavePrinting()); // } } public void StartRenderPrinting(GameObject preview, GameObject design, Action> callback = null) { FolderPath = Path.Combine(dirPath, $"{DateTime.Now.Ticks}"); if (!Directory.Exists(FolderPath)) { Directory.CreateDirectory(FolderPath); } previewData = new byte[] { }; printingDatas = new List(); StartCoroutine(WaitSaveFinish(preview, design, callback)); } byte[] previewData; List printingDatas; IEnumerator WaitSaveFinish(GameObject preview, GameObject design, Action> callback) { if (preview != null) yield return StartCoroutine(WaitSavePreview(preview)); var designGo = GameObject.Instantiate(design, parent); this.option = designGo.GetComponent(); option.GetComponent().ShowAllPrintings(); option.GetComponent().HideAllPrintings(); yield return null; for (var i = 0; i < option.objectsToRender.Count; i++) { foreach (var t in option.objectsToRender) { t.gameObject.SetActive(false); } option.objectsToRender[i].gameObject.SetActive(true); yield return StartCoroutine(WaitSavePrinting(designGo)); } GameObject.Destroy(designGo); callback?.Invoke(previewData, printingDatas); } IEnumerator WaitSavePreview(GameObject preview) { var previewGo = GameObject.Instantiate(preview, parent); previewGo.GetComponent().anchoredPosition = Vector2.zero; var modelOption = previewGo.GetComponent(); modelOption.GetComponent().ShowAllPreviews(); modelOption.GetComponent().HideAllPreviews(); yield return null; float smallerScale = Mathf.Max(modelOption.renderPreviewSize.x / 400f, modelOption.renderPreviewSize.y / 400f); rendtx = new RenderTexture((int)(modelOption.renderPreviewSize.x / smallerScale), (int)(modelOption.renderPreviewSize.y / smallerScale), 24, RenderTextureFormat.ARGB32); modelOption.pivot.transform.localScale = Vector3.one / smallerScale; renderCam.targetTexture = rendtx; yield return null; Texture2D tex = ToTexture2D(rendtx); var bytes = tex.EncodeToPNG(); var path = Path.Combine(FolderPath, $"Preview - {DateTime.Now.Ticks}.png"); File.WriteAllBytes(path, bytes); previewData = File.ReadAllBytes(path); Debug.Log("Preview finish"); rendtx.Release(); GameObject.Destroy(previewGo); } IEnumerator WaitSavePrinting(GameObject design) { option.pivot.GetComponent().anchoredPosition = Vector2.zero; option.pivot.transform.localScale = Vector3.one; Vector2 printSize = option.renderPrintSize.x == 0 ? option.renderPreviewSize : option.renderPrintSize; rendtx = new RenderTexture((int)printSize.x, (int)printSize.y, 24, RenderTextureFormat.ARGB32); renderCam.targetTexture = rendtx; yield return null; Texture2D print_tex = ToTexture2D(rendtx); string uploadTmp = SpecialSaveImageFile(print_tex, "printing"); Debug.Log(uploadTmp); } private string SpecialSaveImageFile(Texture2D tex, string name) { byte[] bytes = tex.EncodeToPNG(); printingDatas.Add(bytes); if (!Directory.Exists(FolderPath)) { Directory.CreateDirectory(FolderPath); } Debug.Log(FolderPath); string filePath = Path.Combine(FolderPath, $"printing-{DateTime.Now.Ticks}"); File.WriteAllBytes(filePath + ".png", B83.Image.PNGTools.ChangePPI(bytes, 300, 300)); string tmpPath = FolderPath + DateTime.Now.ToString("yyyyMMHHmmss") + ".png"; // File.WriteAllBytes(tmpPath, B83.Image.PNGTools.ChangePPI(bytes, 300, 300)); return tmpPath; } Texture2D ToTexture2D(RenderTexture rTex) { RenderTexture currentActiveRT = RenderTexture.active; Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.ARGB32, false); RenderTexture.active = rTex; tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0); tex.Apply(); RenderTexture.active = currentActiveRT; return tex; } }