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

275 lines
12 KiB
C#

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;
private Scene zoneScene;
public void Awake()
{
zoneScene = this.ZoneScene();
AwakeAsync().Coroutine();
}
private async ETVoid AwakeAsync()
{
ui = GetParent<FUI_CharacterUI>();
if (AttributeDic == null)
{
AttributeDic = new Dictionary<string, GTextField>();
foreach (var o in ui.m_infoList.GetChildrenList())
{
var item = (GLabel) o;
AttributeDic.Add(item.name, item.GetChild("txt").asTextField);
}
foreach (var o in ui.m_basicAttributeList.GetChildrenList())
{
var item = (GLabel) o;
AttributeDic.Add(item.name, item.GetChild("txt").asTextField);
}
foreach (var o in ui.m_battleAttributeList.GetChildrenList())
{
var item = (GLabel) o;
AttributeDic.Add(item.name, item.GetChild("txt").asTextField);
}
}
//!临时变量
bool isMine = zoneScene.GetComponent<GlobalVariable>().IsMineCharacter;
//!+获取数据并更新人物信息
long id = isMine ? zoneScene.GetComponent<GlobalVariable>().MyId : zoneScene.GetComponent<GlobalVariable>().SelectUnitId;
M2C_GetCharacter m2cGetCharacter = await zoneScene.GetComponent<SessionComponent>().Call<M2C_GetCharacter>(new C2M_GetCharacter() { Id = id });
if (!m2cGetCharacter.Message.IsNullOrEmpty())
{
TipHelper.OpenUI(m2cGetCharacter.Message);
return;
}
UnitCharacter unitCharacter = m2cGetCharacter.UnitCharacter;
ClientUnitCharacterComponent.Instance.Update(unitCharacter);
await Game.EventSystem.Publish(new UpdateCharacterUI
{
zoneScene = zoneScene,
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 (zoneScene.GetComponent<UnitComponent>().MyUnit.IsFight)
{
TipHelper.OpenUI("战斗中,不能卸下装备!");
return;
}
M2C_Takeoff ret = await zoneScene.GetComponent<SessionComponent>().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 (zoneScene.GetComponent<UnitComponent>().MyUnit.IsFight)
{
TipHelper.OpenUI("战斗中,不能卸下装备!");
return;
}
M2C_Takeoff ret = await zoneScene.GetComponent<SessionComponent>().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";
}
M2C_AddPoint ret = await zoneScene.GetComponent<SessionComponent>().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
{
zoneScene = zoneScene,
Id = id
});
});
this.ui.m_btnResetPoint.self.onClick.Clear();
if (isMine)
this.ui.m_btnResetPoint.self.onClick.Set(async () =>
{
M2C_ResetPoint ret = await zoneScene.GetComponent<SessionComponent>().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
{
zoneScene = zoneScene,
Id = id
});
});
}
void AddRollEvent(GButton btn, int index)
{
//!显示页签
TabHelper.SetTab(btn, () =>
{
if (ClientItemDataComponent.Instance.WornEquipDic.TryGetValue(index, out ClientItemData data))
{
var gv = zoneScene.GetComponent<GlobalVariable>();
long selectUnitId=gv.IsMineCharacter?0:gv.SelectUnitId;
TabHelper.OpenUI(zoneScene,data, false,selectUnitId);
}
});
}
}
await ETTask.CompletedTask;
}
private async ETVoid ShowSkin(int skinId)
{
SkinBase skinBase = DataTableHelper.Get<SkinBase>(skinId);
skinTran = await ResourceViewHelper.LoadPrefabAsync(skinBase.PrfabId);
if (nTexture == null)
{
Transform characterTran = await ResourceViewHelper.LoadPrefabAsync(Sys_PrefabId.Character);
characterTran.localPosition = 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);
}
}
}