CTT/Unity/Assets/HotfixView/UI/CharacterUI/CharacterUI.cs

264 lines
11 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00

using Cal.DataTable;
using ET.EventType;
using ET;
using FairyGUI;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ET
{
public class CharacterUIAwakeSyatem : AwakeSystem<CharacterUI>
{
public override void Awake(CharacterUI self)
{
self.Awake();
}
}
public class CharacterUIDestroySyatem : DestroySystem<CharacterUI>
{
public override void Destroy(CharacterUI self)
{
self.Destroy();
}
}
public class CharacterUI : Entity
{
public static Dictionary<string, GTextField> AttributeDic { get; private set; }
public FUI_CharacterUI ui;
private NTexture nTexture;
private Camera rtCamera;
private Transform skinTran;
private RenderTexture rt;
public void Awake()
{
AwakeAsync().Coroutine();
}
private async ETVoid AwakeAsync()
{
ui = GetParent<FUI_CharacterUI>();
if (AttributeDic == null)
{
AttributeDic = new Dictionary<string, GTextField>();
foreach (GLabel item in ui.m_infoList.GetChildrenList())
{
AttributeDic.Add(item.name, item.GetChild("txt").asTextField);
}
foreach (GLabel item in ui.m_basicAttributeList.GetChildrenList())
{
AttributeDic.Add(item.name, item.GetChild("txt").asTextField);
}
foreach (GLabel item in ui.m_battleAttributeList.GetChildrenList())
{
AttributeDic.Add(item.name, item.GetChild("txt").asTextField);
}
}
//!临时变量
bool isMine = GlobalVariable.IsMineCharacter;
//!+获取数据并更新人物信息
long id = isMine ? GlobalVariable.MyId : GlobalVariable.SelectUnitId;
var m2cGetCharacter = await SessionComponent.Instance.Call<M2C_GetCharacter>(new C2M_GetCharacter() { Id = id });
if (!m2cGetCharacter.Message.IsNullOrEmpty())
{
TipHelper.OpenUI(m2cGetCharacter.Message);
return;
}
var unitCharacter = m2cGetCharacter.UnitCharacter;
ClientUnitCharacterComponent.Instance.Update(unitCharacter);
await Game.EventSystem.Publish(new UpdateCharacterUI
{
Id = id
});
//!显示形象
ShowSkin(unitCharacter.SkinId).Coroutine();
//!更新装备栏
Game.EventSystem.Publish(new ET.EventType.UpdateWornEquipUI
{
list = m2cGetCharacter.WornBagMapList
}).Coroutine();
//!+添加按钮事件
{
//!武器等装备栏
int atkListCount = this.ui.m_atkEquipList.numChildren;
for (int i = 0; i < atkListCount; i++)
{
int index = i;
GButton btn = this.ui.m_atkEquipList.GetChildAt(i).asButton;
if (isMine)
{
btn.onClick.Clear();
btn.onClick.Add1(async context =>
{
//!卸下装备
if (context.inputEvent.isDoubleClick)
{
if (UnitComponent.MyUnit.IsFight)
{
TipHelper.OpenUI("战斗中,不能卸下装备!");
return;
}
var ret = await SessionComponent.Instance.Call<M2C_Takeoff>(new C2M_Takeoff() { Index = index });
if (!ret.Message.IsNullOrEmpty())
{
TipHelper.OpenUI(ret.Message);
return;
}
Game.EventSystem.Publish(new ET.EventType.UpdateWornEquipUI
{
list = ret.WornBagMapList
}).Coroutine();
Game.EventSystem.Publish_Sync(new ET.EventType.UpdateBagUI
{
list = ret.BagMapList
});
}
});
}
//!Roll事件
AddRollEvent(btn, index);
}
//!普通装备栏
for (int i = 0; i < this.ui.m_commonEquipList.numChildren; i++)
{
int index = i;
index += atkListCount;
GButton btn = this.ui.m_commonEquipList.GetChildAt(i).asButton;
if (isMine)
{
btn.onClick.Clear();
btn.onClick.Add1(async context =>
{
//!卸下装备
if (context.inputEvent.isDoubleClick)
{
if (UnitComponent.MyUnit.IsFight)
{
TipHelper.OpenUI("战斗中,不能卸下装备!");
return;
}
var ret = await SessionComponent.Instance.Call<M2C_Takeoff>(new C2M_Takeoff() { Index = index });
if (!ret.Message.IsNullOrEmpty())
{
TipHelper.OpenUI(ret.Message);
return;
}
Game.EventSystem.Publish(new ET.EventType.UpdateWornEquipUI
{
list = ret.WornBagMapList
}).Coroutine();
Game.EventSystem.Publish_Sync(new ET.EventType.UpdateBagUI
{
list = ret.BagMapList
});
}
});
}
//!Roll事件
AddRollEvent(btn, index);
}
//!加点按钮监听
{
this.ui.m_btnAddPoint.self.onClick.Clear();
if (isMine)
this.ui.m_btnAddPoint.self.onClick.Set(async () =>
{
int count = this.ui.m_addPointAttributeList.numChildren;
C2M_AddPoint c2M_AddPoint = new C2M_AddPoint();
for (int i = 0; i < count; i++)
{
GLabel label = this.ui.m_addPointAttributeList.GetChildAt(i).asLabel;
FUI_LabelAddPoint item = FUI_LabelAddPoint.GetFormPool(this.ui, label);
if (!int.TryParse(item.m_iptPoint.text, out int pointCount))
{
pointCount = 0;
}
if (pointCount < 0)
pointCount = 0;
c2M_AddPoint.PointList.Add(pointCount);
item.m_iptPoint.text = "0";
}
var ret = await SessionComponent.Instance.Call<M2C_AddPoint>(c2M_AddPoint);
if (!ret.Message.IsNullOrEmpty())
{
TipHelper.OpenUI(ret.Message);
return;
}
ClientUnitCharacterComponent.Instance.Update(ret.Character);
await Game.EventSystem.Publish(new UpdateCharacterUI
{
Id = id
});
});
this.ui.m_btnResetPoint.self.onClick.Clear();
if (isMine)
this.ui.m_btnResetPoint.self.onClick.Set(async () =>
{
var ret = await SessionComponent.Instance.Call<M2C_ResetPoint>(new C2M_ResetPoint());
if (!ret.Message.IsNullOrEmpty())
{
TipHelper.OpenUI(ret.Message);
return;
}
ClientUnitCharacterComponent.Instance.Update(ret.Character);
await Game.EventSystem.Publish(new UpdateCharacterUI
{
Id = id
});
});
}
void AddRollEvent(GButton btn, int index)
{
//!显示页签
TabHelper.SetTab(btn, () =>
{
if (ClientItemDataComponent.Instance.WornEquipDic.TryGetValue(index, out var data))
{
TabHelper.OpenUI(data, false);
}
});
}
}
await ETTask.CompletedTask;
}
private async ETVoid ShowSkin(int skinId)
{
SkinBase skinBase = DataTableHelper.Get<SkinBase>(skinId);
skinTran = await ResourceViewHelper.LoadPrefabAsync(skinBase.PrfabId);
if (nTexture == null)
{
var characterTran = await ResourceViewHelper.LoadPrefabAsync(Sys_PrefabId.Character);
characterTran.position = new Vector3(-2000, 0, 0);
rt = CreateTexture((int)this.ui.m_icon.width, (int)this.ui.m_icon.height);
rtCamera = characterTran.GetComponentInChildren<Camera>();
rtCamera.targetTexture = rt;
nTexture = new NTexture(rt);
this.ui.m_icon.texture = nTexture;
}
rtCamera.enabled = true;
skinTran.SetParent(rtCamera.transform.parent);
skinTran.localPosition = Vector3.forward * 5;
}
private static RenderTexture CreateTexture(int width, int height)
{
return new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32)
{
antiAliasing = 1,
filterMode = FilterMode.Bilinear,
anisoLevel = 0,
useMipMap = false
};
}
public void Destroy()
{
rtCamera.enabled = false;
ResourceViewHelper.DestoryPrefabAsync(skinTran);
}
}
}