126 lines
3.9 KiB
C#
126 lines
3.9 KiB
C#
using Assets.Framework.Tool;
|
|
using Assets.MyGame.Scripts.Net;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[Serializable]
|
|
public enum PropEnum
|
|
{
|
|
none=0,
|
|
炮台 = 1,
|
|
翅膀 = 2,
|
|
炮塔碎片 = 3,
|
|
翅膀碎片 = 4,
|
|
战神 = 5,
|
|
战神碎片 = 6,
|
|
刮刮卡 = 7,
|
|
改名卡 = 8,
|
|
大喇叭 = 9,
|
|
技能 = 10,
|
|
召唤卡 = 11,
|
|
}
|
|
|
|
/// <summary>
|
|
/// BackPackPropItem 初始化数据
|
|
/// </summary>
|
|
[Serializable]
|
|
public class BackPackPropItemData
|
|
{
|
|
/// <summary>道具图标地址(完整 URL,含 baseURL)。giftType 为金币/钻石时可为空</summary>
|
|
public string url;
|
|
/// <summary>数量</summary>
|
|
public int count;
|
|
/// <summary>奖励类型 1=金币 2=钻石 3=道具(默认道具)</summary>
|
|
public int giftType = 3;
|
|
/// <summary>道具子类型(仅 giftType==3 时有意义,用于背景框等)</summary>
|
|
public PropEnum propEnum = PropEnum.none;
|
|
}
|
|
|
|
public class BackPackPropItem : MonoBehaviour
|
|
{
|
|
public List<Image> item_Bg;
|
|
public Dictionary<PropEnum, Image> item_Type=new Dictionary<PropEnum, Image>();
|
|
public Image img_Prop;
|
|
public TextMeshProUGUI propCount;
|
|
|
|
// 金币/钻石图标从 Resources 读取(放 Assets/Resources 根目录),静态缓存避免重复加载
|
|
private const string GoldResName = "gild";
|
|
private const string DiamondResName = "diamond";
|
|
private static Sprite _goldSprite;
|
|
private static Sprite _diamondSprite;
|
|
|
|
private static Sprite GetGoldSprite()
|
|
{
|
|
if (_goldSprite == null) _goldSprite = Resources.Load<Sprite>(GoldResName);
|
|
return _goldSprite;
|
|
}
|
|
private static Sprite GetDiamondSprite()
|
|
{
|
|
if (_diamondSprite == null) _diamondSprite = Resources.Load<Sprite>(DiamondResName);
|
|
return _diamondSprite;
|
|
}
|
|
|
|
public void Init(BackPackPropItemData data)
|
|
{
|
|
if (data == null) return;
|
|
|
|
// 道具子类型背景框(仅道具类才显示)
|
|
if (data.giftType == 3)
|
|
{
|
|
PropEnum propEnum = data.propEnum;
|
|
switch (propEnum)
|
|
{
|
|
case PropEnum.炮塔碎片:
|
|
case PropEnum.翅膀碎片:
|
|
item_Bg[1].gameObject.SetActive(true);
|
|
break;
|
|
case PropEnum.战神碎片:
|
|
item_Bg[0].gameObject.SetActive(true);
|
|
break;
|
|
case PropEnum.刮刮卡:
|
|
case PropEnum.召唤卡:
|
|
case PropEnum.技能:
|
|
item_Bg[2].gameObject.SetActive(true);
|
|
break;
|
|
case PropEnum.改名卡:
|
|
case PropEnum.大喇叭:
|
|
item_Bg[3].gameObject.SetActive(true);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 图标来源:金币/钻石用 Resources 本地图,道具下载图片
|
|
if (data.giftType == 1)
|
|
{
|
|
var gold = GetGoldSprite();
|
|
if (gold != null) img_Prop.sprite = gold;
|
|
}
|
|
else if (data.giftType == 2)
|
|
{
|
|
var diamond = GetDiamondSprite();
|
|
if (diamond != null) img_Prop.sprite = diamond;
|
|
}
|
|
else if (!string.IsNullOrEmpty(data.url))
|
|
{
|
|
WebTool.DownImage(data.url, (texture) =>
|
|
{
|
|
// 回调触发时本物体可能已被销毁(切换等级/关闭面板清空父节点),需判空
|
|
if (this == null || img_Prop == null) return;
|
|
if (texture != null)
|
|
{
|
|
img_Prop.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
|
|
}
|
|
});
|
|
}
|
|
|
|
this.propCount.text = data.count.ToString();
|
|
}
|
|
}
|