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

196 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using cfg.ActorCfg;
using cfg.item;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay;
using PhxhSDK;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ItemObj : UIGameObjectWrapper
{
GameObject _parentGo;
Action _action;//用来做点击事件
int _Id;
GameObject _itemGo;
DataItem Cfg;
Button clickBtn;
UIType _type;
int count = 0;
public enum interactiveType
{
normail,
selected,
gray,
}
public enum UIType//测试,正式美术图出来后弃用
{
cultivate,
mail,
shop,
}
//美术没出图前都用这
public ItemObj(int itemId, GameObject parentObject, UIType uIType, GameObject go, int itemCount = 0, bool isScale = false) : base(go, isScale)
{
InitData(itemId, parentObject, itemCount);
_type = uIType;
_itemGo = go;
_itemGo.transform.SetParent(_parentGo.transform);
_itemGo.transform.localPosition = Vector3.zero;
RefreshDetail();
CommonUtils.BindButton(FindObj("click").GetComponent<Button>(), OnClickBtnClick);
}
public static async UniTask<ItemObj> InitItemObjFromPrefab(int itemId, GameObject parentObject, UIType uIType, int itemCount = 0)
{
GameObject newGo = await LoadPrefabTest();
ItemObj itemObj = new ItemObj(itemId, parentObject, uIType, newGo, itemCount);
itemObj.IsScaleShow = true;
return itemObj;
}
//现在仅giftcode使用,后面改
public ItemObj(int itemId, GameObject parentObject, GameObject go, bool isScale = false) : base(go, isScale)
{
InitData(itemId, parentObject);
_itemGo = go;
_itemGo.transform.SetParent(_parentGo.transform);
_itemGo.transform.localPosition = Vector3.zero;
RefreshDetail();
CommonUtils.BindButton(FindObj("click").GetComponent<Button>(), OnClickBtnClick);
}
public static async UniTask<ItemObj> InitItemObjFromPrefab(int itemId, GameObject parentObject)
{
GameObject newGo = await LoadPrefabTest();
ItemObj itemObj = new ItemObj(itemId, parentObject, newGo);
itemObj.IsScaleShow = true;
return itemObj;
}
public void AddButtonListener(Action callback)
{
_action = callback;
}
//相关设置
public void SetScale(float scale)
{
if (_itemGo == null)
{
DebugUtil.LogError("itemobj害没生成调整下顺序呢");
return;
}
_itemGo.transform.localScale = new Vector3(scale, scale, scale);
}
public void SetState(interactiveType interType)
{
switch (interType)
{
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;
}
}
public void RefreshGoods(GameObject itemGo, int itemId)
{
switch (Cfg.Type)
{
case ItemType.Normal:
break;
case ItemType.Character:
break;
case ItemType.Useable:
break;
case ItemType.Vehicle:
break;
}
}
#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)
{
DebugUtil.LogError("");
}
GameObject ret = UnityEngine.Object.Instantiate(go);
//_itemGo.transform.SetParent(_parentGo.transform);
//ret.transform.localPosition = Vector3.zero;
ret.transform.localScale = Vector3.one;
//RefreshDetail();
return ret;
}
private async void RefreshDetail()
{
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();
_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)
{
DebugUtil.LogError("物品表没有配置id为{0}的物品,请检查配置", itemId);
}
}
private void OnClickBtnClick()
{
if (_action != null)
{
_action.Invoke();
return;
}
//下面写物品默认点击事件
}
private async UniTask OnIconLoaded()
{
var spritePath = CommonUtils.GetItemPath(_Id);
var img = CommonUtils.GetComponent<Image>(_itemGo, string.Format("test{0}/icon", (int)_type + 1));
img.sprite = await LoadAssetAsync<Sprite>(spritePath);
}
#endregion
}