Files
Fishing2/Assets/Scripts/Startup/Init.cs
2025-07-26 15:19:14 +08:00

272 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using Assets.Scripts.Entity;
using Assets.Scripts.Hotfix;
using FairyGUI;
using NBC;
using NBC.Network;
using NBC.Platform.Unity;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Video;
namespace NBF
{
public class Init : MonoBehaviour
{
[SerializeField] private VideoPlayer videoPlayer;
[SerializeField] private GameObject startCanvas;
[SerializeField] private bool playVideo;
private void CheckOver(VideoPlayer vp)
{
Destroy(startCanvas);
StartGame();
}
private void Awake()
{
DontDestroyOnLoad(gameObject);
App.Init(InitCallback);
}
private void OnDestroy()
{
PermanentCommon.Dispose();
}
void InitCallback()
{
ES3.Init();
ES3.Save("NBF", 1);
if (!playVideo)
{
videoPlayer.Stop();
}
else
{
videoPlayer.loopPointReached += CheckOver;
}
QualitySettings.vSyncCount = 0;
InitLanguage();
InitService();
InitUI();
if (!playVideo)
{
CheckOver(videoPlayer);
}
}
#region
private void InitLanguage()
{
// var map = LanguageConst.languageMap;
// Lan.Inst.AddLanguageModule((int)LanguageModuleType.Text, new LanguageText());
// Lan.Inst.AddLanguageModule((int)LanguageModuleType.Image, new LanguageImage());
// Lan.Inst.AddLanguageModule((int)LanguageModuleType.Font, new LanguageFont());
//
// foreach (var key in LanguageConst.languageMap.Keys)
// {
// Lan.Inst.AddLanguage(key);
// }
//
// UI.Inst.SetUILanguage<UILangeageConfig>();
// Lan.Inst.AutoUseLanguage();
}
#endregion
#region UI
private void InitUI()
{
GRoot.inst.SetContentScaleFactor(UIDef.DefaultScreen.Width, UIDef.DefaultScreen.Height,
UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);
Binder.BindAll();
UIObjectFactory.SetLoaderExtension(typeof(XGLoader));
UIConst.UIPackRootUrl = UIDef.UIRoot;
UIConfig.defaultFont = "AlibabaPuHuiTi-3-Medium";
App.UI.SetUILanguage<UILangeageConfig>();
UIConfig.modalLayerColor = new Color(0, 0, 0, 0.9f);
AddUIPackages();
}
private void AddUIPackages()
{
App.UI.AddPackage("Common/Common");
}
#endregion
#region Service
// private static readonly List<MonoService> Services = new List<MonoService>();
private static readonly Dictionary<Type, MonoService> Services = new Dictionary<Type, MonoService>();
private void AddService<T>() where T : MonoService, new()
{
var service = this.GetComponent<T>();
if (!service)
{
service = gameObject.AddComponent<T>();
}
Services[typeof(T)] = service;
}
private void InitService()
{
AddService<InputManager>();
AddService<Settings>();
}
#endregion
public void StartGame()
{
PermanentCommon.Init();
InputDef.LoadIcon();
// UI.Inst.OpenUI<FishingShopPanel>();
LoadData();
CommonTopPanel.Show();
// FishingPanel.Show();
PreviewPanel.Show();
// Fishing.Inst.Go(1);
// SettingPanel.Show();
// HomePanel.Show();
//测试登录
OnLoginButtonClick().Coroutine();
}
#region
private Session _session;
private async FTask OnLoginButtonClick()
{
// NBC.Platform.Unity.Entry.Initialize(GetType().Assembly);
// 根据用户名来选择目标的鉴权服务器
// 根据鉴权服务器地址来创建一个新的网络会话
_session = SessionHelper.CreateSession(App.Main, "127.0.0.1:20001", OnConnectComplete,
OnConnectFail,
OnConnectDisconnect);
var acc = "test001";
// // 发送一个注册的请求消息到目标服务器
// var responseReg = (A2C_RegisterResponse)await _session.Call(new C2A_RegisterRequest()
// {
// Username = acc,
// Password = acc
// });
// 发送登录的请求给服务器
var response = (A2C_LoginResponse)await _session.Call(new C2A_LoginRequest()
{
Username = acc,
Password = acc,
LoginType = 1
});
if (response.ErrorCode != 0)
{
Log.Error($"登录发生错误{response.ErrorCode}");
return;
}
if (!App.Main.GetComponent<JWTParseComponent>().Parse(response.ToKen, out var payload))
{
return;
}
// 根据ToKen返回的Address登录到Gate服务器
_session = SessionHelper.CreateSession(App.Main, payload.Address, OnConnectComplete, OnConnectFail,
OnConnectDisconnect);
// 发送登录请求到Gate服务器
var loginResponse = (G2C_LoginResponse)await _session.Call(new C2G_LoginRequest()
{
ToKen = response.ToKen
});
if (loginResponse.ErrorCode != 0)
{
Log.Error($"登录发生错误{loginResponse.ErrorCode}");
return;
}
Log.Debug(
$"登录到Gate服务器成功LoginTime:{loginResponse.GameAccountInfo.LoginTime} CreateTime:{loginResponse.GameAccountInfo.CreateTime}");
}
private void OnConnectComplete()
{
Log.Debug("连接成功");
// 添加心跳组件给Session。
// Start(2000)就是2000毫秒。
_session.AddComponent<SessionHeartbeatComponent>().Start(2000);
}
private void OnConnectFail()
{
Log.Debug("连接失败");
}
#endregion
private void OnConnectDisconnect()
{
Log.Debug("连接断开");
}
private void LoadData()
{
ConfigAssets.Init();
GameModel.Inst.Init();
// var inventoryManager = GetComponent<InventoryManager>();
// if (inventoryManager == null)
// {
// inventoryManager = gameObject.AddComponent<InventoryManager>();
// }
//InventoryManager
}
#region New
public ControllerType controllerType = ControllerType.GamePad;
public void SetMouseCursor(bool val)
{
if (val)
{
if (controllerType == ControllerType.KeyboardMouse)
{
Cursor.visible = true;
}
Cursor.lockState = CursorLockMode.None;
}
else if (controllerType == ControllerType.KeyboardMouse)
{
Cursor.visible = false;
}
Cursor.visible = val;
if (!val)
{
Cursor.lockState = CursorLockMode.Confined;
}
}
#endregion
}
}