NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Level/LevelFightEnd/FightWinComponent.cs

191 lines
5.6 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 System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Gameplay;
using PhxhSDK;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.SceneManagement;
using UnityEngine.Timeline;
using Framework;
using Constants = Framework.Constants;
public class FightWinComponent: MonoBehaviour
{
private List<uint> _characterIDs;
private uint _vehicleID;
private GameObject _objRoot;
private List<Transform> _posTransList;
private Camera _camera;
private PlayableDirector _timelinePlayable;
//test
private List<GameObject> _testObjs = new List<GameObject>();
private readonly int Vehicle_Model_Index = 5;
#region Unity EventFunction
private void Awake()
{
//test
for (int i = 1; i <= 5; i++)
{
_testObjs.Add(GameObject.Find("Content/P_S_A0000" + i));
}
_camera = GameObject.Find("CameraRoot").GetComponent<CameraRoot>().Camera;
_objRoot = GameObject.Find("Content/ObjRoot");
var posRootTrans = GameObject.Find("Content/PosRoot").transform;
_posTransList = new List<Transform>();
for (int i = 0; i < posRootTrans.childCount; i++)
{
var child = posRootTrans.GetChild(i);
_posTransList.Add(child);
}
_Load();
_timelinePlayable.Play();
}
#endregion
private void _Load()
{
_LoadTimeline();
}
private async void _LoadModels()
{
if (_characterIDs != null)
{
for (int i = 0; i < _characterIDs.Count; i++)
{
await _AddCharacterModel(_characterIDs[i], i);
}
}
if (_vehicleID != Constants.INVALID_UINT_ID)
{
await _AddVehicleModel(_vehicleID);
}
}
private async UniTask _AddCharacterModel(uint uid, int idx)
{
var modelPath = CommonUtils.GetCharacterShowModelPath(uid);
if (modelPath != null)
{
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(modelPath);
var model = Instantiate(prefab, _objRoot.transform);
var posTrans = _posTransList[idx];
model.transform.SetPositionAndRotation(posTrans.position, posTrans.rotation);
model.transform.localScale = posTrans.localScale;
}
}
private async UniTask _AddVehicleModel(uint uid)
{
var modelPath = CommonUtils.GetVehicleShowModelPath(uid);
if (modelPath != null)
{
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(modelPath);
var model = Instantiate(prefab, _objRoot.transform);
var posTrans = _posTransList[Vehicle_Model_Index];
model.transform.SetPositionAndRotation(posTrans.position, posTrans.rotation);
model.transform.localScale = posTrans.localScale;
}
}
private async void _LoadTimeline()
{
var timeLineObj = new GameObject("GameTimeLine");
var timeline = timeLineObj.AddComponent<PlayableDirector>();
// var playableAsset =
// await AssetManager.Instance.LoadAssetAsync<PlayableAsset>("Assets/_Test/Prefab/VictoryTimeLine.playable");
//test
#if UNITY_EDITOR
var playableAsset =
UnityEditor.AssetDatabase.LoadAssetAtPath<PlayableAsset>("Assets/_Test/Prefab/VictoryTimeLine.playable");
var timeLineAsset = playableAsset as TimelineAsset;
if (!timeLineAsset)
{
DebugUtil.LogError("加载的TimeLine资源为空");
return;
}
timeline.playableAsset = timeLineAsset;
timeline.timeUpdateMode = DirectorUpdateMode.GameTime;
timeline.playOnAwake = false;
_timelinePlayable = timeline;
var trackIEnumerable = timeLineAsset.GetOutputTracks();
int index = 0;
foreach (var trackAsset in trackIEnumerable)
{
//test
if (index < _testObjs.Count)
{
var animObj = _testObjs[index].transform.GetChild(0);
var anim = animObj.GetComponent<Animator>();
if (!anim)
{
DebugUtil.LogError("TimeLine使用的模型未绑定Animator组件");
}
timeline.SetGenericBinding(trackAsset, anim);
}
else
{
var anim = _camera.GetComponent<Animator>();
if (anim)
{
timeline.SetGenericBinding(trackAsset, anim);
}
}
index++;
// if (index < _characterIDs.Count)
// {
// var animObj = _objRoot.transform.GetChild(index).GetChild(0).gameObject;
//
// var anim = animObj.GetComponent<Animator>();
// if (!anim)
// {
// DebugUtil.LogError("TimeLine使用的模型未绑定Animator组件");
// }
// timeline.SetGenericBinding(trackAsset, anim);
// }
//index++;
}
#endif
}
#region API
public void SetData(List<uint> charIDList)
{
_characterIDs = charIDList;
}
public void SetData(uint vehicleID)
{
_vehicleID = vehicleID;
}
public void SetData(List<uint> charIDList, uint vehicleID)
{
_characterIDs = charIDList;
_vehicleID = vehicleID;
}
public void StartPlay()
{
_timelinePlayable.Play();
}
#endregion
}