ND_SimulationAutomaticControl/Assets/Scripts/UI/UIPanel/UI_ReportUploadPanel.cs

204 lines
7.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 提交面板
/// </summary>
public class UI_ReportUploadPanel : BasePanel
{
public Button CloseBtn; //关闭按钮
private CanvasGroup canvasGroup; //屏幕显示
public GameObject ReportUpload_Image; //报告图片
public TMP_InputField ReportUpload_Input; //实验报告输入框
// Start is called before the first frame update
protected override void Awake()
{
base.Awake();
OnInit();
}
public void OnInit()
{
CloseBtn = GetControl<Button>("关闭按钮");
canvasGroup = GetComponent<CanvasGroup>();
canvasGroup.alpha = 0f; // 默认隐藏
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
}
public override void ShowMe()
{
base.ShowMe();
FadeIn(0.5f);
if (ReportUpload_Image == null)
{
Debug.LogError("ReportUpload_Image组件未赋值");
return;
}
foreach (Toggle toggle in UI_ExperimentChooesPanel.instance?.SubjectTog)
{
if (toggle == null) continue;
if (toggle.isOn)
{
string toggleName = toggle.name;
string imagePath = string.Empty;
switch (toggleName)
{
case "5-1-1":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/比例1倍_511.png");
break;
case "5-1-2":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/比例1倍_522.png");
break;
case "5-2-1":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/1uf积分_521.png");
break;
case "5-2-2":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/1uf积分_522.png");
break;
case "5-3-1":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/比例积分0.1.png");
break;
case "5-3-2":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/比例积分1.png");
break;
case "5-4-1":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/比例微分1.png");
break;
case "5-4-2":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/比例微分2.png");
break;
case "5-5-1":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/惯性环节1.png");
break;
case "5-5-2":
imagePath = Path.Combine(Application.streamingAssetsPath, "实验报告图片/惯性环节2.png");
break;
}
Image ReportUploadImage = ReportUpload_Image.GetComponent<Image>();
Sprite targetSprite = LoadSpriteFromStreamingAssets(imagePath, new Vector2(736, 414));
if (targetSprite != null)
{
ReportUploadImage.sprite = targetSprite;
// 固定Image尺寸为736×414
ReportUploadImage.rectTransform.sizeDelta = new Vector2(736, 414);
// 确保Image的显示模式为“填充”避免自动缩放
ReportUploadImage.type = Image.Type.Simple;
ReportUploadImage.preserveAspect = false;
}
}
}
}
public override void HideMe()
{
base.HideMe();
FadeOut(0.5f);
}
protected override void OnClick(string btnPath)
{
base.OnClick(btnPath);
switch (btnPath)
{
case "关闭按钮":
FadeOut(0.5f);
break;
case "提交Btn":
string content = ReportUpload_Input.text;
if (string.IsNullOrEmpty(content))
{
Debug.Log("请输入内容!");
return;
}
StartCoroutine(ReportUploadManager.instance.ReportUpload(content));
break;
}
}
/// <summary>
/// 提问渐显
/// </summary>
public void FadeIn(float time)
{
canvasGroup.DOFade(1f, time);
canvasGroup.interactable = true;
canvasGroup.blocksRaycasts = true;
}
/// <summary>
/// 提问渐隐
/// </summary>
public void FadeOut(float time)
{
canvasGroup.DOFade(0f, time);
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
}
/// <summary>
/// 加载图片并裁剪/缩放为指定尺寸(保持比例)
/// </summary>
/// <param name="filePath">图片路径</param>
/// <param name="targetSize">目标尺寸736×414</param>
/// <returns>固定尺寸的Sprite</returns>
private Sprite LoadSpriteFromStreamingAssets(string filePath, Vector2 targetSize)
{
if (!File.Exists(filePath))
{
Debug.LogError($"图片文件不存在:{filePath}");
return null;
}
try
{
byte[] imageBytes = File.ReadAllBytes(filePath);
Texture2D texture = new Texture2D(2, 2); // 初始尺寸任意LoadImage会自动调整
if (!texture.LoadImage(imageBytes)) // LoadImage支持PNG/JPG自动识别格式
{
Debug.LogError($"加载图片失败:{filePath}可能不是有效的PNG/JPG格式");
Destroy(texture); // 释放无效Texture
return null;
}
// 计算目标比例和原始比例
float targetRatio = targetSize.x / targetSize.y; // 736/414 ≈ 1.777816:9
float originalRatio = (float)texture.width / texture.height;
Rect cropRect;
if (originalRatio > targetRatio)
{
// 原始图片更宽,裁剪左右两侧
float cropWidth = texture.height * targetRatio;
float startX = (texture.width - cropWidth) / 2;
cropRect = new Rect(startX, 0, cropWidth, texture.height);
}
else
{
// 原始图片更高,裁剪上下两侧
float cropHeight = texture.width / targetRatio;
float startY = (texture.height - cropHeight) / 2;
cropRect = new Rect(0, startY, texture.width, cropHeight);
}
// 创建固定尺寸的Sprite居中裁剪
Sprite sprite = Sprite.Create(
texture,
cropRect, // 裁剪后的区域
new Vector2(0.5f, 0.5f), // 中心锚点
100, // PPI
0,
SpriteMeshType.Tight
);
return sprite;
}
catch (System.Exception e)
{
Debug.LogError($"读取图片异常:{e.Message}");
return null;
}
}
}