NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/ShowAllCharacter/ItemObj.cs

196 lines
5.7 KiB
C#
Raw Normal View History

2023-12-13 16:51:07 +08:00
using cfg.ActorCfg;
2023-12-22 14:12:25 +08:00
using cfg.item;
2023-12-13 16:51:07 +08:00
using Cysharp.Threading.Tasks;
2023-10-19 15:00:19 +08:00
using Framework;
using Gameplay;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2023-12-13 16:51:07 +08:00
using System;
2023-12-22 14:12:25 +08:00
using TMPro;
2023-12-13 16:51:07 +08:00
using UnityEngine;
2023-10-19 15:00:19 +08:00
using UnityEngine.UI;
public class ItemObj : UIGameObjectWrapper
2023-10-19 15:00:19 +08:00
{
GameObject _parentGo;
2023-12-13 16:51:07 +08:00
Action _action;//用来做点击事件
2023-10-19 15:00:19 +08:00
int _Id;
GameObject _itemGo;
2023-12-13 16:51:07 +08:00
DataItem Cfg;
Button clickBtn;
UIType _type;
int count = 0;
2023-12-13 16:51:07 +08:00
public enum interactiveType
{
normail,
selected,
gray,
}
2024-01-18 19:02:27 +08:00
public enum UIType//测试,正式美术图出来后弃用
2023-12-13 16:51:07 +08:00
{
cultivate,
mail,
2024-01-08 13:13:35 +08:00
shop,
2023-12-13 16:51:07 +08:00
}
2024-01-23 15:07:04 +08:00
//美术没出图前都用这
public ItemObj(int itemId, GameObject parentObject, UIType uIType, GameObject go, int itemCount = 0, bool isScale = false) : base(go, isScale)
2023-12-13 16:51:07 +08:00
{
InitData(itemId, parentObject, itemCount);
2024-01-23 15:07:04 +08:00
_type = uIType;
_itemGo = go;
_itemGo.transform.SetParent(_parentGo.transform);
_itemGo.transform.localPosition = Vector3.zero;
RefreshDetail();
2023-12-13 16:51:07 +08:00
2024-09-24 17:33:07 +08:00
CommonUtils.BindButton(FindObj("click").GetComponent<Button>(), OnClickBtnClick);
2023-12-13 16:51:07 +08:00
}
2024-01-23 15:07:04 +08:00
public static async UniTask<ItemObj> InitItemObjFromPrefab(int itemId, GameObject parentObject, UIType uIType, int itemCount = 0)
2023-12-13 16:51:07 +08:00
{
GameObject newGo = await LoadPrefabTest();
ItemObj itemObj = new ItemObj(itemId, parentObject, uIType, newGo, itemCount);
itemObj.IsScaleShow = true;
return itemObj;
2023-12-13 16:51:07 +08:00
}
2024-01-19 19:13:27 +08:00
//现在仅giftcode使用,后面改
public ItemObj(int itemId, GameObject parentObject, GameObject go, bool isScale = false) : base(go, isScale)
2024-01-18 15:45:51 +08:00
{
InitData(itemId, parentObject);
_itemGo = go;
2023-12-13 16:51:07 +08:00
_itemGo.transform.SetParent(_parentGo.transform);
_itemGo.transform.localPosition = Vector3.zero;
2024-01-19 19:13:27 +08:00
RefreshDetail();
2024-09-24 17:33:07 +08:00
CommonUtils.BindButton(FindObj("click").GetComponent<Button>(), OnClickBtnClick);
2024-01-19 19:13:27 +08:00
}
public static async UniTask<ItemObj> InitItemObjFromPrefab(int itemId, GameObject parentObject)
2024-01-19 19:13:27 +08:00
{
GameObject newGo = await LoadPrefabTest();
ItemObj itemObj = new ItemObj(itemId, parentObject, newGo);
itemObj.IsScaleShow = true;
return itemObj;
}
2024-01-08 13:13:35 +08:00
public void AddButtonListener(Action callback)
{
_action = callback;
2023-12-13 16:51:07 +08:00
}
//相关设置
public void SetScale(float scale)
2023-12-13 16:51:07 +08:00
{
if (_itemGo == null)
2023-12-13 16:51:07 +08:00
{
DebugUtil.LogError("itemobj害没生成,调整下顺序呢");
return;
2023-12-22 14:12:25 +08:00
}
_itemGo.transform.localScale = new Vector3(scale, scale, scale);
2023-12-22 14:12:25 +08:00
}
2023-12-13 16:51:07 +08:00
public void SetState(interactiveType interType)
2023-12-13 16:51:07 +08:00
{
switch (interType)
2023-12-13 16:51:07 +08:00
{
case interactiveType.normail:
CommonUtils.GetGameObject(_itemGo, "state/Gray").SetActive(false);
break;
case interactiveType.selected:
break;
case interactiveType.gray:
CommonUtils.GetGameObject(_itemGo, "state/Gray").SetActive(true);
break;
2023-12-13 16:51:07 +08:00
}
2023-10-19 15:00:19 +08:00
}
public void RefreshGoods(GameObject itemGo, int itemId)
2023-10-19 15:00:19 +08:00
{
2023-12-13 16:51:07 +08:00
switch (Cfg.Type)
2023-10-19 15:00:19 +08:00
{
2023-12-13 16:51:07 +08:00
case ItemType.Normal:
break;
case ItemType.Character:
break;
case ItemType.Useable:
break;
case ItemType.Vehicle:
break;
2023-10-19 15:00:19 +08:00
}
2023-12-13 16:51:07 +08:00
2023-10-19 15:00:19 +08:00
}
2024-01-23 15:07:04 +08:00
#region 私有方法
private static async UniTask<GameObject> LoadPrefabTest()
{
string prefabPath = "Assets/Art/UI/Prefab/MainScene/ShowCharacter/ItemObj.prefab";
GameObject go = await AssetManager.GetSingleton().LoadAssetAsync<GameObject>(prefabPath);
if (go == null)
2024-01-23 15:07:04 +08:00
{
DebugUtil.LogError("");
2024-01-23 15:07:04 +08:00
}
GameObject ret = UnityEngine.Object.Instantiate(go);
//_itemGo.transform.SetParent(_parentGo.transform);
//ret.transform.localPosition = Vector3.zero;
ret.transform.localScale = Vector3.one;
//RefreshDetail();
return ret;
2024-01-23 15:07:04 +08:00
}
2024-09-14 15:01:28 +08:00
private async void RefreshDetail()
2023-12-13 16:51:07 +08:00
{
CommonUtils.GetGameObject(_itemGo, "test2").SetActive(_type == UIType.mail);
CommonUtils.GetGameObject(_itemGo, "test1").SetActive(_type == UIType.cultivate);
CommonUtils.GetGameObject(_itemGo, "test3").SetActive(_type == UIType.shop);
//CommonUtils.GetComponent<TMP_Text>(_itemGo, "test2/icon/Text_StuffNum").text = count.ToString();
CommonUtils.GetComponent<TMP_Text>(_itemGo, "test2/icon/Text_StuffNum").text = count.ToString();
CommonUtils.GetComponent<TMP_Text>(_itemGo, "test3/Image_Bar/Text_ItemNum").text = count.ToString();
CommonUtils.GetGameObject(_itemGo, "DebugID").SetActive(DisplayIDForCeHua.IsDisplayID);
CommonUtils.GetComponent<TMP_Text>(_itemGo, "DebugID").text = _Id.ToString();
2024-09-14 15:01:28 +08:00
_itemGo.gameObject.SetActive(false);
await OnIconLoaded();
_itemGo.gameObject.SetActive(true);
}
private void InitData(int itemId, GameObject paObj, int itemCount = 0)
{
_parentGo = paObj;
_Id = itemId;
count = itemCount;
Cfg = CategoryManager.Instance.GetItemData(itemId);
if (Cfg == null)
2023-12-13 16:51:07 +08:00
{
DebugUtil.LogError("物品表没有配置id为{0}的物品,请检查配置", itemId);
}
}
2023-10-19 15:00:19 +08:00
private void OnClickBtnClick()
{
if (_action != null)
{
_action.Invoke();
return;
2023-12-13 16:51:07 +08:00
}
//下面写物品默认点击事件
2023-12-13 16:51:07 +08:00
}
2024-09-14 15:01:28 +08:00
private async UniTask OnIconLoaded()
2023-10-19 15:00:19 +08:00
{
var spritePath = CommonUtils.GetItemPath(_Id);
2024-09-14 15:01:28 +08:00
var img = CommonUtils.GetComponent<Image>(_itemGo, string.Format("test{0}/icon", (int)_type + 1));
img.sprite = await LoadAssetAsync<Sprite>(spritePath);
2023-10-19 15:00:19 +08:00
}
#endregion
2023-10-19 15:00:19 +08:00
}