196 lines
5.4 KiB
C#
196 lines
5.4 KiB
C#
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
|
||
{
|
||
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,Action click=null)
|
||
{
|
||
if(click!=null)
|
||
{
|
||
_action = click;
|
||
|
||
}
|
||
InitData(itemId, parentObject);
|
||
|
||
}
|
||
public async UniTask Init()
|
||
{
|
||
await LoadPrefab();
|
||
RefreshDetail();
|
||
}
|
||
|
||
|
||
public ItemObj(int itemId, GameObject parentObject, UIType uIType,int itemCount=0,bool IsInit=false)//美术出图前暂用,不用init的版本
|
||
{
|
||
InitData(itemId, itemCount,parentObject);
|
||
_type = uIType;
|
||
if(!IsInit)
|
||
{
|
||
LoadPrefabTest();
|
||
|
||
}
|
||
|
||
}
|
||
|
||
public void SetScale(float scale)
|
||
{
|
||
|
||
if(_itemGo==null)
|
||
{
|
||
DebugUtil.LogError("itemobj害没生成,调整下顺序呢");
|
||
return;
|
||
}
|
||
_itemGo.transform.localScale = new Vector3(scale, scale, scale);
|
||
}
|
||
|
||
private void LoadPrefabTest()
|
||
{
|
||
string prefabPath = "Assets/Art/UI/Prefab/MainScene/ShowCharacter/ItemObj.prefab";
|
||
AssetManager.GetSingleton().LoadAssetOnCallBack<GameObject>(prefabPath, OnPrefabLoadedTest);
|
||
}
|
||
|
||
private void OnPrefabLoadedTest(GameObject @object)
|
||
{
|
||
_itemGo = UnityEngine.Object.Instantiate(@object);
|
||
_itemGo.transform.SetParent(_parentGo.transform);
|
||
_itemGo.transform.localPosition = Vector3.zero;
|
||
_itemGo.transform.localScale = Vector3.one;
|
||
RefreshDetail();
|
||
|
||
}
|
||
private 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();
|
||
}
|
||
private void InitData(int itemId,GameObject paObj)
|
||
{
|
||
_parentGo = paObj;
|
||
_Id = itemId;
|
||
|
||
Cfg = TableManager.GetSingleton().Tables.Item.GetOrDefault(itemId);
|
||
if(Cfg==null)
|
||
{
|
||
DebugUtil.LogError("物品表没有配置id为{0}的物品,请检查配置", itemId);
|
||
}
|
||
}
|
||
private void InitData(int itemId, int itemCount,GameObject paObj)
|
||
{
|
||
_parentGo = paObj;
|
||
_Id = itemId;
|
||
count = itemCount;
|
||
Cfg = TableManager.GetSingleton().Tables.Item.GetOrDefault(itemId);
|
||
if (Cfg == null)
|
||
{
|
||
DebugUtil.LogError("物品表没有配置id为{0}的物品,请检查配置", itemId);
|
||
}
|
||
}
|
||
private async UniTask LoadPrefab()
|
||
{
|
||
string prefabPath = "Assets/Art/UI/Prefab/MainScene/ShowCharacter/ItemObj.prefab";
|
||
GameObject obj= await AssetManager.GetSingleton().LoadAssetAsync<GameObject>(prefabPath);
|
||
if(obj==null)
|
||
{
|
||
DebugUtil.LogError("");
|
||
}
|
||
_itemGo = UnityEngine.Object.Instantiate(obj);
|
||
_itemGo.transform.SetParent(_parentGo.transform);
|
||
_itemGo.transform.localPosition = Vector3.zero;
|
||
_itemGo.transform.localScale = Vector3.one;
|
||
clickBtn = CommonUtils.GetComponent<Button>(_itemGo, "click");
|
||
clickBtn.onClick.AddListener(OnClickBtnClick);
|
||
AssetManager.Instance.LoadAssetOnCallBack<Sprite>(TableManager.Instance.Tables.Item.Get(_Id).IconPath, OnIconLoaded);
|
||
}
|
||
|
||
|
||
|
||
private void OnClickBtnClick()
|
||
{
|
||
if(_action != null)
|
||
{
|
||
_action.Invoke();
|
||
return;
|
||
}
|
||
//下面写物品默认点击事件
|
||
|
||
}
|
||
|
||
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;
|
||
|
||
}
|
||
|
||
}
|
||
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;
|
||
}
|
||
|
||
}
|
||
private void OnIconLoaded(Sprite sprite)
|
||
{
|
||
CommonUtils.GetComponent<Image>(_itemGo, "test1/icon").sprite= sprite;
|
||
}
|
||
|
||
|
||
}
|