104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
|
|
using Assets.MyGame.Scripts.Net.APIResponses.Enum;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TuJianItem : MonoBehaviour
|
|
{
|
|
public GameObject BossFlag;
|
|
public Image sprite_image;
|
|
public TextMeshProUGUI tMP_nameText;
|
|
public TextMeshProUGUI tMP_Mult;
|
|
[Header("兼容老式 UGUI Text:拖了就会一起填;只有它(没 TMP)时也照常显示")]
|
|
public Text nameText;
|
|
public Text multText;
|
|
public RoomFishDetailConfig roomFishDetailConfig;
|
|
Button btn_This;
|
|
private void Awake()
|
|
{
|
|
btn_This = GetComponent<Button>();
|
|
}
|
|
public void InItItem(RoomFishDetailConfig roomFishDetail, bool isGuDingBeiShu = true)
|
|
{
|
|
roomFishDetailConfig = roomFishDetail;
|
|
if (FishResourcesData.fishResourcesSpritesDic.ContainsKey(int.Parse(roomFishDetailConfig.fishNumber)))
|
|
{
|
|
sprite_image.sprite = FishResourcesData.fishResourcesSpritesDic[int.Parse(roomFishDetailConfig.fishNumber)];
|
|
}
|
|
|
|
// 图片等比例缩放,填满 Image 的宽高中较小的那条边
|
|
FitImageToBounds();
|
|
|
|
var fishCfg = GameManager.Instance.dic_fishTypeConfigs[roomFishDetailConfig.fishNumber];
|
|
|
|
// ⭐ Boss 角标:只有 Boss / 特殊Boss 显示,其它鱼隐藏
|
|
// (BossFlag 可要可不要:没拖引用就跳过,不报错)
|
|
if (BossFlag != null)
|
|
{
|
|
var fishType = (FishTypeEnum)fishCfg.fishType;
|
|
BossFlag.SetActive(fishType == FishTypeEnum.Boss鱼 || fishType == FishTypeEnum.特殊Boss);
|
|
}
|
|
|
|
// 倍数文字优先用服务端的 fishMultipleDesc(如"最高500倍"),没有时回退到本地拼接
|
|
string multStr;
|
|
if (!string.IsNullOrEmpty(fishCfg.fishMultipleDesc))
|
|
{
|
|
multStr = fishCfg.fishMultipleDesc;
|
|
}
|
|
else if (isGuDingBeiShu)
|
|
{
|
|
multStr = $"{fishCfg.fishScore}倍";
|
|
}
|
|
else
|
|
{
|
|
multStr = $"最高{fishCfg.fishScore}倍";
|
|
}
|
|
|
|
// TMP 和 UGUI Text 哪个拖了写哪个(两个都拖就都写)
|
|
SetItemText(tMP_nameText, nameText, fishCfg.fishName);
|
|
SetItemText(tMP_Mult, multText, multStr);
|
|
}
|
|
|
|
/// <summary>写入文本:TextMeshProUGUI 和 UGUI Text 通用(哪个有引用写哪个)</summary>
|
|
private static void SetItemText(TextMeshProUGUI tmp, Text legacy, string value)
|
|
{
|
|
if (tmp != null) tmp.text = value;
|
|
if (legacy != null) legacy.text = value;
|
|
}
|
|
|
|
private void FitImageToBounds()
|
|
{
|
|
if (sprite_image == null || sprite_image.sprite == null) return;
|
|
|
|
var rect = sprite_image.rectTransform;
|
|
var spriteTex = sprite_image.sprite.texture;
|
|
if (spriteTex == null) return;
|
|
|
|
// 用 rect(实际渲染大小)作为容器宽高
|
|
float imgW = rect.rect.width;
|
|
float imgH = rect.rect.height;
|
|
float spriteW = spriteTex.width;
|
|
float spriteH = spriteTex.height;
|
|
|
|
if (spriteW <= 0 || spriteH <= 0 || imgW <= 0 || imgH <= 0) return;
|
|
|
|
float scale = Mathf.Min(imgW / spriteW, imgH / spriteH);
|
|
float newW = spriteW * scale;
|
|
float newH = spriteH * scale;
|
|
|
|
// 四个边分离的锚点:通过 offsetMin/offsetMax 设置宽高
|
|
// 保持中心不变
|
|
float centerX = (rect.offsetMin.x + rect.offsetMax.x) * 0.5f;
|
|
float centerY = (rect.offsetMin.y + rect.offsetMax.y) * 0.5f;
|
|
|
|
float halfW = newW * 0.5f;
|
|
float halfH = newH * 0.5f;
|
|
|
|
rect.offsetMin = new Vector2(centerX - halfW, centerY - halfH);
|
|
rect.offsetMax = new Vector2(centerX + halfW, centerY + halfH);
|
|
}
|
|
}
|