using System; using System.Collections; using System.IO; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Processing; using UnityEngine; using UnityEngine.UI; using Image = UnityEngine.UI.Image; namespace HK.FUJIFILM { public class TransparentCaptureSystem_FUJIFILM : TransparentCaptureSystem { protected override void Init() { Instance = this; base.Init(); } void Update() { if (Input.GetKeyDown(captureKey)) { CaptureAndCropNow(); } } [ContextMenu("立即截图并裁剪1")] public override void CaptureAndCropNow(Action callback = null) { StartCoroutine(WaitCaptureAndCropFinished(callback)); } public void PreviewCaptureAndCropFinished(Action callback = null) { StartCoroutine(WaitPreviewCaptureAndCropFinished(callback)); } IEnumerator WaitPreviewCaptureAndCropFinished(Action callback = null) { var mugActualModelRenderFixed = uiRoot.GetComponent(); var image = mugActualModelRenderFixed.designParent.GetChild(0).GetComponent(); image.enabled = true; yield return StartCoroutine(CaptureAndCrop(callback)); Debug.Log("02 finished"); image.enabled = false; } public void DesignCaptureAndCropFinished(Action callback = null) { StartCoroutine(WaitDesignCaptureAndCropFinished(callback)); } public IEnumerator WaitDesignCaptureAndCropFinished(Action callback = null) { var mugActualModelRenderFixed = uiRoot.GetComponent(); var image = mugActualModelRenderFixed.designParent.GetChild(0).GetComponent(); image.enabled = false; var verticalLayoutGroup = mugActualModelRenderFixed.designParent.GetComponent(); verticalLayoutGroup.enabled = true; // verticalLayoutGroup.childAlignment = TextAnchor.LowerLeft; var tmpWidth = cropWidth; var tmpHeight = cropHeight; var screenshotSize = mugActualModelRenderFixed.productScriptableObject.screenshotSize; cropWidth = (int)screenshotSize.x; cropHeight = (int)screenshotSize.y; cropOffset = new Vector2(0.5f, 0.5f); yield return StartCoroutine(CaptureAndCrop(callback)); // 加载图像 using (var sImage = SixLabors.ImageSharp.Image.Load(FullPath)) { // 等比缩放至宽度为 200(高度自动计算以保持比例) sImage.Mutate(x => x.Resize(new ResizeOptions { Size = new Size(324, 204), // 高度设为 0 即自动按比例计算 Mode = ResizeMode.Max // 确保图像完全显示,不裁剪 })); var directoryFullName = new FileInfo(FullPath).Directory.FullName; // 保存为 JPEG(设置质量) sImage.SaveAsPng($"{directoryFullName}/output.png", new PngEncoder()); } // verticalLayoutGroup.childAlignment = TextAnchor.MiddleCenter; verticalLayoutGroup.enabled = false; cropWidth = tmpWidth; cropHeight = tmpHeight; cropOffset = new Vector2(0f, 0f); Debug.Log("01 finished"); image.enabled = false; } IEnumerator WaitCaptureAndCropFinished(Action callback = null) { var mugActualModelRenderFixed = uiRoot.GetComponent(); var image = mugActualModelRenderFixed.designParent.GetChild(0).GetComponent(); image.enabled = false; yield return StartCoroutine(CaptureAndCrop()); Debug.Log("01 finished"); image.enabled = true; // var tmpWidth = cropWidth; // var tmpHeight = cropHeight; // var screenshotSize = mugActualModelRenderFixed.productScriptableObject.screenshotSize; // cropWidth = (int)screenshotSize.x; // cropHeight = (int)screenshotSize.y; yield return StartCoroutine(CaptureAndCrop(callback)); Debug.Log("02 finished"); // cropWidth = tmpWidth; // cropHeight = tmpHeight; image.enabled = false; } } }