328 lines
8.9 KiB
C#
328 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using Framework;
|
|
using Gameplay.Common;
|
|
using PhxhSDK;
|
|
using UnityEngine;
|
|
using Constants = Framework.Constants;
|
|
|
|
public class TeamPreviewNode: MonoBehaviour
|
|
{
|
|
public Transform[] characterPos;
|
|
public Transform vehiclePos;
|
|
public Transform cRoot;
|
|
public Transform vRoot;
|
|
|
|
public struct CharAddData
|
|
{
|
|
public uint ID;
|
|
public int Idx;
|
|
}
|
|
|
|
public RenderTexture RTexture => _rTexture;
|
|
|
|
private Dictionary<int, GameObject> _characterDic;
|
|
private GameObject _vehicleGo;
|
|
|
|
private uint[] _charIDs;
|
|
private uint _vehicleID;
|
|
|
|
private bool _isLoading;
|
|
private int _teamIdx;
|
|
|
|
private bool _isReleasing;
|
|
|
|
private RenderTexture _rTexture;
|
|
|
|
private void Awake()
|
|
{
|
|
EventManager.Instance.Register(EventManager.EventName.TeamEditModelCreate, (Action<CharAddData>)OnCharModelCreate);
|
|
EventManager.Instance.Register(EventManager.EventName.TeamEditModelClear, (Action<int>)OnCharModelClear);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
EventManager.Instance.Unregister(EventManager.EventName.TeamEditModelCreate, (Action<CharAddData>)OnCharModelCreate);
|
|
EventManager.Instance.Unregister(EventManager.EventName.TeamEditModelClear, (Action<int>)OnCharModelClear);
|
|
}
|
|
|
|
public static async UniTask<TeamPreviewNode> LoadNode()
|
|
{
|
|
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(Constants.TEAM_EDIT_PREFAB_PATH);
|
|
var go = Instantiate(prefab, Vector3.up, Quaternion.identity);
|
|
var component = go.GetComponent<TeamPreviewNode>();
|
|
|
|
return component;
|
|
}
|
|
|
|
public static void ReleaseNode(TeamPreviewNode node)
|
|
{
|
|
node.ReleaseRt();
|
|
Destroy(node.gameObject);
|
|
}
|
|
|
|
public void RefreshNode(int teamIdx)
|
|
{
|
|
var team = TeamEditManager.Instance.GetTeamByIndex((uint)teamIdx);
|
|
var charIDs = team.GetCharacterIDs();
|
|
var vehicleID = team.GetVehicleID();
|
|
ClearObjs();
|
|
SetData(teamIdx, charIDs, vehicleID);
|
|
CreateObjs();
|
|
}
|
|
|
|
public void ClearObjs()
|
|
{
|
|
cRoot.transform.ClearChilds(true);
|
|
vRoot.transform.ClearChilds(true);
|
|
_characterDic.Clear();
|
|
}
|
|
|
|
public async void CreateObjs()
|
|
{
|
|
for (int i = 0; i < _charIDs.Length; i++)
|
|
{
|
|
var dataDic = CharacterDataInfoManager.Instance.DataDic;
|
|
uint id = _charIDs[i];
|
|
bool isValid = dataDic.ContainsKey(id);
|
|
if (isValid)
|
|
{
|
|
var trans = characterPos[i];
|
|
if (!_isLoading)
|
|
{
|
|
_isLoading = true;
|
|
await AddCharacterGo(trans, id, i);
|
|
}
|
|
}
|
|
}
|
|
|
|
_isLoading = true;
|
|
await AddVehicleGo(_vehicleID);
|
|
}
|
|
|
|
//初始化
|
|
public async UniTask Init()
|
|
{
|
|
cRoot.transform.ClearChilds(true);
|
|
vRoot.transform.ClearChilds(true);
|
|
|
|
// if (!_rTexture)
|
|
// {
|
|
// _rTexture = RenderTexturePool.Instance.Create("TeamPreviewNode", Screen.width,
|
|
// Screen.height, 32);
|
|
// _rTexture.name = "team_rt";
|
|
// // _rTexture.width = TeamEditManager.Instance.ImageSceneWidth;
|
|
// // _rTexture.height = TeamEditManager.Instance.ImageSceneHeight;
|
|
// }
|
|
|
|
var teamCamera = TeamEditCameraObj.Instance.teamEditCamera;
|
|
teamCamera.targetTexture = _rTexture;
|
|
|
|
_characterDic = new Dictionary<int, GameObject>();
|
|
for (int i = 0; i < _charIDs.Length; i++)
|
|
{
|
|
var dataDic = CharacterDataInfoManager.Instance.DataDic;
|
|
uint id = _charIDs[i];
|
|
bool isValid = dataDic.ContainsKey(id);
|
|
if (isValid)
|
|
{
|
|
var trans = characterPos[i];
|
|
if (!_isLoading)
|
|
{
|
|
_isLoading = true;
|
|
await AddCharacterGo(trans, id, i);
|
|
}
|
|
}
|
|
}
|
|
|
|
_isLoading = true;
|
|
await AddVehicleGo(_vehicleID);
|
|
|
|
EventManager.Instance.Send(EventManager.EventName.TeamEditPreviewInit, _rTexture);
|
|
}
|
|
|
|
public void CreateRt()
|
|
{
|
|
if (!_rTexture)
|
|
{
|
|
_rTexture = RenderTexturePool.Instance.Create("TeamPreviewNode", Screen.width,
|
|
Screen.height, RenderTexturePool.DEFAULT_DEPTH);
|
|
_rTexture.name = "team_rt";
|
|
// _rTexture.width = TeamEditManager.Instance.ImageSceneWidth;
|
|
// _rTexture.height = TeamEditManager.Instance.ImageSceneHeight;
|
|
}
|
|
}
|
|
|
|
public void ReleaseRt()
|
|
{
|
|
if (_rTexture)
|
|
{
|
|
RenderTexturePool.Instance.Destroy(_rTexture);
|
|
_rTexture = null;
|
|
}
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
for (int i = 0; i < characterPos.Length; i++)
|
|
{
|
|
ClearCharacterGo(i);
|
|
}
|
|
|
|
ClearVehicleGo();
|
|
_rTexture.Release();
|
|
}
|
|
|
|
//设置数据
|
|
public void SetData(int teamIdx, uint[] charIDs, uint vehicleID)
|
|
{
|
|
_teamIdx = teamIdx;
|
|
_charIDs = (uint[])charIDs.Clone();
|
|
_vehicleID = vehicleID;
|
|
}
|
|
|
|
public async UniTask LoadCharacterModel(uint id, int idx)
|
|
{
|
|
if (_charIDs[idx] != id)
|
|
{
|
|
ClearCharacterGo(idx);
|
|
_charIDs[idx] = id;
|
|
await AddCharacterGo(characterPos[idx], id, idx);
|
|
}
|
|
}
|
|
|
|
public async UniTask LoadVehicleModel(uint id)
|
|
{
|
|
if (_vehicleID != id)
|
|
{
|
|
ClearVehicleGo();
|
|
_vehicleID = id;
|
|
await AddVehicleGo(id);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//监听事件
|
|
private async void OnCharModelCreate(CharAddData charAddData)
|
|
{
|
|
await LoadCharacterModel(charAddData.ID, charAddData.Idx);
|
|
}
|
|
|
|
private void OnCharModelClear(int idx)
|
|
{
|
|
if (idx > 0)
|
|
{
|
|
ClearCharacterGo(idx);
|
|
return;
|
|
}
|
|
ClearVehicleGo();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加角色模型
|
|
/// </summary>
|
|
/// <param name="trans"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="idx"></param>
|
|
async UniTask AddCharacterGo(Transform trans, uint id, int idx)
|
|
{
|
|
var charData = CharacterDataInfoManager.Instance.DataDic[id].Cfg;
|
|
var data = TableManager.Instance.Tables.SkinCfg.Get(Constants.DEFULT_SKINID, charData.RoleID);
|
|
if (data != null)
|
|
{
|
|
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(data.DisplayPrefabPath);
|
|
if (!trans)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var go = Instantiate(prefab, trans.position, trans.rotation, cRoot.transform);
|
|
if (go)
|
|
{
|
|
_characterDic.Add(idx, go);
|
|
go.SetLayerEx(LayerDefine.TeamEdit);
|
|
var anim = Utils.CommonUtils.GetChildComponent<Animator>(go);
|
|
if (anim)
|
|
{
|
|
if (idx < 2)
|
|
{
|
|
anim.Play("squatready001");
|
|
}
|
|
else
|
|
{
|
|
anim.Play("standready001");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
_isLoading = false;
|
|
}
|
|
|
|
void ClearCharacterGo(int idx)
|
|
{
|
|
if (_isLoading)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_isLoading = true;
|
|
if (_characterDic.TryGetValue(idx, out var go))
|
|
{
|
|
if (go)
|
|
{
|
|
DestroyImmediate(go);
|
|
_charIDs[idx] = Team.NONE_ID;
|
|
_characterDic.Remove(idx);
|
|
}
|
|
}
|
|
_isLoading = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 载具模型加载
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
async UniTask AddVehicleGo(uint id)
|
|
{
|
|
var vehicleDataDic = VehicleCultivateDataManager.Instance.DataDic;
|
|
if (vehicleDataDic.TryGetValue(id, out var vehicleData))
|
|
{
|
|
if (vehicleData != null)
|
|
{
|
|
var trans = vehiclePos;
|
|
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(vehicleData.Cfg.PrefabPath);
|
|
if (!trans)
|
|
{
|
|
return;
|
|
}
|
|
var go = Instantiate(prefab, trans.position, trans.rotation, vRoot.transform);
|
|
if (go)
|
|
{
|
|
go.SetLayerEx(LayerDefine.TeamEdit);
|
|
go.transform.localScale = new Vector3(1.7f, 1.7f, 1.7f);
|
|
_vehicleGo = go;
|
|
}
|
|
}
|
|
}
|
|
|
|
_isLoading = false;
|
|
}
|
|
|
|
void ClearVehicleGo()
|
|
{
|
|
if (_isLoading)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_isLoading = true;
|
|
DestroyImmediate(_vehicleGo);
|
|
_vehicleID = Team.NONE_ID;
|
|
_vehicleGo = null;
|
|
|
|
_isLoading = false;
|
|
}
|
|
} |