NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UI_ChoosePersonController.cs

199 lines
5.5 KiB
C#

using Code.Scripts.Gameplay.Game;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay;
using Gameplay.Net;
using Gameplay.Story;
using Gameplay.Utils;
using PhxhSDK;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;
/// <summary>
/// 选人UI
/// </summary>
public class UI_ChoosePersonController : UIWindow
{
/// <summary>
/// 打开选人界面
/// </summary>
/// <returns></returns>
public static async UniTask<UIWindow> Open()
{
CommonUtils.CaptureScreenshot(); // 截取当前屏幕截图
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_ChoosePerson);
}
#region UI
/// <summary>
/// 高斯模糊遮罩
/// </summary>
private RawImage rawImageGaussianBlurMask;
/// <summary>
/// 形象1按钮
/// </summary>
private Button buttonModel1;
/// <summary>
/// 形象2按钮
/// </summary>
private Button buttonModel2;
/// <summary>
/// 确定按钮
/// </summary>
private Button buttonConfirm;
/// <summary>
/// 选中状态1
/// </summary>
private GameObject goSelect1;
/// <summary>
/// 选中状态2
/// </summary>
private GameObject goSelect2;
#endregion
private const string ANIM_STATE = "IsSelect";
/// <summary>
/// 模型1
/// </summary>
private GameObject goModel1;
/// <summary>
/// 模型2
/// </summary>
private GameObject goModel2;
private Animator animator1;
private Animator animator2;
/// <summary>
/// 选择的模型id
/// </summary>
private int selectModelId;
public override void OnInit()
{
rawImageGaussianBlurMask = GetComponent<RawImage>("GaussianBlurMask");
buttonModel1 = GetComponent<Button>("Image_BG/ButtonModel1");
buttonModel2 = GetComponent<Button>("Image_BG/ButtonModel2");
buttonConfirm = GetComponent<Button>("Image_BG/Button_Confirm");
goSelect1 = FindObj("Image_BG/ButtonModel1/ImageSelect");
goSelect2 = FindObj("Image_BG/ButtonModel2/ImageSelect");
goModel1 = FindObj("Image_BG/ButtonModel1/P_S_N00004");
goModel2 = FindObj("Image_BG/ButtonModel2/P_S_N00005");
animator1 = GetComponent<Animator>("Image_BG/ButtonModel1/P_S_N00004/N00004");
animator2 = GetComponent<Animator>("Image_BG/ButtonModel2/P_S_N00005/N00005");
rawImageGaussianBlurMask.texture = CommonUtils.GetScreenTexture2D();
selectModelId = 0;
goModel1.SetLayerEx(5);
goModel2.SetLayerEx(5);
animator1.SetBool(ANIM_STATE, false);
animator2.SetBool(ANIM_STATE, false);
OnModel(0);
goSelect1.gameObject.IsScaleShow(false);
goSelect2.gameObject.IsScaleShow(false);
BindButton(buttonModel1, () =>
{
OnModel(1);
});
BindButton(buttonModel2, () =>
{
OnModel(2);
});
BindButton(buttonConfirm, OnConfirm);
EventManager.Instance.Register<string>(EventManager.EventName.UIOpen, OnUIOpen);
EventManager.Instance.Register<string>(EventManager.EventName.UIClose, OnUIClose);
EventManager.Instance.Register(EventManager.EventName.SetPlayerMode, _ContinueStory);
}
public override void OnRelease()
{
EventManager.Instance.Unregister(EventManager.EventName.SetPlayerMode, _ContinueStory);
EventManager.Instance.Unregister<string>(EventManager.EventName.UIClose, OnUIClose);
EventManager.Instance.Unregister<string>(EventManager.EventName.UIOpen, OnUIOpen);
base.OnRelease();
}
#region 回调方法
/// <summary>
/// 关闭
/// </summary>
private void OnClose()
{
CloseWindow();
}
/// <summary>
/// 选择模型
/// </summary>
/// <param name="i"></param>
private void OnModel(int id)
{
selectModelId = id;
buttonConfirm.interactable = 0 != selectModelId;
if (1 == id)
{
animator1.SetBool(ANIM_STATE, true);
animator2.SetBool(ANIM_STATE, false);
goSelect1.gameObject.IsScaleShow(true);
goSelect2.gameObject.IsScaleShow(false);
}
else if(2 == id)
{
animator1.SetBool(ANIM_STATE, false);
animator2.SetBool(ANIM_STATE, true);
goSelect1.gameObject.IsScaleShow(false);
goSelect2.gameObject.IsScaleShow(true);
}
}
/// <summary>
/// 确定选择
/// </summary>
private void OnConfirm()
{
NetHelper.SetPlayeHead((uint)selectModelId);
}
private async void _ContinueStory()
{
if (GameStateManager.Instance.GetCurState() is GameStateStory gameStateStory)
{
// 这里需要重新创建主角模型
await StoryManager.instance.AutoReCreatePlayerModel(gameStateStory.storyConfig);
gameStateStory.IsReady = true;
}
OnClose();
}
private void OnUIOpen(string uiName)
{
if (UINameConst.UI_ChoosePerson == uiName)
return;
if (UINameConst.UI_Guide == uiName)
return;
buttonModel1.gameObject.SetActive(false);
buttonModel2.gameObject.SetActive(false);
}
private void OnUIClose(string uiName)
{
if (UINameConst.UI_ChoosePerson == uiName)
return;
if (UINameConst.UI_Guide == uiName)
return;
buttonModel1.gameObject.SetActive(true);
buttonModel2.gameObject.SetActive(true);
OnModel(selectModelId);
}
#endregion
}