GameMgr.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. using System.Collections.Generic;
  5. using System;
  6. public class GameMgr : SingletonMono<GameMgr>
  7. {
  8. public const string VersionUpdateState = "VersionUpdateState";
  9. public const string LoginState = "LoginState";
  10. public const string LoadingState = "LoadingState";
  11. public const string BattleState = "BattleState";
  12. public static int RandSeed = 100000;
  13. public float mGameSpeed = 1.0f;
  14. private bool mIsSingle = false;
  15. private string mGameVersion;
  16. public string GameVersion
  17. {
  18. get { return mGameVersion; }
  19. }
  20. private string mResVersion;
  21. public string ResVersion
  22. {
  23. get { return mResVersion; }
  24. }
  25. public bool IsSingle
  26. {
  27. get { return mIsSingle; }
  28. }
  29. public float GameSpeed
  30. {
  31. get { return mGameSpeed; }
  32. }
  33. private LuaFunction OnSceneLoaded2Lua;
  34. private LuaFunction mPlayStoryLuaFun;
  35. private static GameMgr instance;
  36. public static new GameMgr Instance
  37. {
  38. get { return instance; }
  39. }
  40. private bool mIsStartDungeon = false;
  41. public bool InStartDungeon
  42. {
  43. get { return mIsStartDungeon; }
  44. }
  45. private bool mIsEditorMode = false;
  46. public bool IsEditorMode
  47. {
  48. get { return mIsEditorMode; }
  49. }
  50. public GameObject CreateMaleRoot
  51. {
  52. get { return CreateRoleMgr.Instance.MaleRoot; }
  53. }
  54. public GameObject CreateFemaleRoot
  55. {
  56. get { return CreateRoleMgr.Instance.FemaleRoot; }
  57. }
  58. public GameObject CreateShowRoot
  59. {
  60. get { return CreateRoleMgr.Instance.ShowRoot; }
  61. }
  62. public eNetType NetStatus
  63. {
  64. get { return DeviceInfo.GetNetType(); }
  65. }
  66. private MainCharacter mCharacterInfo;
  67. public MainCharacter CharacterInfo
  68. {
  69. get { return mCharacterInfo; }
  70. }
  71. public string DeviceId
  72. {
  73. get { return DeviceInfo.m_deviceId; }
  74. }
  75. public string CurLangKey
  76. {
  77. get { return ConfigMgr.CurLangKey; }
  78. set { ConfigMgr.CurLangKey = value; }
  79. }
  80. public bool IsMobileDevice
  81. {
  82. get
  83. {
  84. #if UNITY_EDITOR
  85. return false;
  86. #elif UNITY_ANDROID || UNITY_IPHONE
  87. return true;
  88. #else
  89. return false;
  90. #endif
  91. }
  92. }
  93. public bool IsBadDevice
  94. {
  95. get { return DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.BAD_DEVICE; }
  96. }
  97. public bool IsNormalDevice
  98. {
  99. get { return DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE; }
  100. }
  101. public bool IsGoodDevice
  102. {
  103. get { return DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.GOOD_DEVICE; }
  104. }
  105. NPack.MersenneTwister rand = null;
  106. private SceneType mEnterSceneType = 0;
  107. private BattleSubMode mbossMode = BattleSubMode.None;
  108. private bool bInited = false;
  109. private void Awake()
  110. {
  111. #if !UNITY_EDITOR
  112. DebugHelper.LogLevel = LogLevel.Error;
  113. #endif
  114. instance = this;
  115. DontDestroyOnLoad(this.gameObject);
  116. rand = new NPack.MersenneTwister(int.MaxValue);
  117. //Application.systemLanguage == SystemLanguage.
  118. System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("en-US");//en-US//ru-RU
  119. }
  120. bool bPaused = false;
  121. float mPausedTime = 0;
  122. private void OnApplicationFocus(bool focus)
  123. {
  124. if (bPaused && focus)
  125. {
  126. float pausePassedTime = Time.realtimeSinceStartup - mPausedTime;
  127. // if (pausePassedTime >= 300) //切后台5分钟网络重新连接
  128. // {
  129. // NetworkMgr.Instance.Resume();
  130. // }
  131. }
  132. }
  133. private void OnApplicationPause(bool pause)
  134. {
  135. bPaused = pause;
  136. if (bPaused)
  137. {
  138. mPausedTime = Time.realtimeSinceStartup;
  139. GameSettings.Instance.Save();
  140. }
  141. }
  142. protected override void OnApplicationQuit()
  143. {
  144. GameSettings.Instance.Save();
  145. base.OnApplicationQuit();
  146. }
  147. private void InitSDK_E()
  148. {
  149. StartCoroutine(initSdk_E());
  150. }
  151. private IEnumerator initSdk_E()
  152. {
  153. SDKMgr.Instance.Init();
  154. yield return null;
  155. }
  156. private void Start()
  157. {
  158. if (bInited) return;
  159. SDKMgr.Instance.ReportActivation(SDKMgr.Instance.GetInt64TimeStamp());
  160. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  161. QualitySettings.vSyncCount = 0;
  162. RegisterEvents();
  163. #if UNITY_IOS && !UNITY_EDITOR
  164. InitSDK_E();
  165. #endif
  166. //InitSDK_E();
  167. StartDetector();
  168. DeviceInfo.GetDeviceState();
  169. if (DeviceInfo.m_DeviceState <= DeviceInfo.eDeviceState.NORMAL_DEVICE)
  170. {
  171. Application.targetFrameRate = 60;
  172. BattleMgr.c_updateFPS = 15;
  173. }
  174. else
  175. {
  176. Application.targetFrameRate = 60;
  177. BattleMgr.c_updateFPS = 30;
  178. }
  179. Input.multiTouchEnabled = false;
  180. //启用日志
  181. StartLog();
  182. //读取Apk设置文件
  183. //yield return StartCoroutine(ReadVersionFile());
  184. //InitData();
  185. //InitBuildConfig();
  186. //InitData();
  187. LaunchLoadMgr lanuchLoadMgr = new LaunchLoadMgr();
  188. lanuchLoadMgr.StartLaunch(OnLanuchLoadCompleted, OnAssetMapInitComplete);
  189. //检测非法修改
  190. AntiCheatMgr.Instance.GetOrCreateAnti(EnAntiCheatType.enSystemTime).Init(AntiCheatCfg.c_fTimeSysteCheckTime);
  191. AntiCheatMgr.Instance.GetOrCreateAnti(EnAntiCheatType.enScaleTime).Init(AntiCheatCfg.c_fTimeScaleCheckTime);
  192. bInited = true;
  193. }
  194. public void DoTaskByCorutine(Func<IEnumerator> func)
  195. {
  196. StartCoroutine(func.Invoke());
  197. }
  198. private void OnLanuchLoadCompleted(bool success)
  199. {
  200. EnterLuaLogin(false);
  201. }
  202. private void OnAssetMapInitComplete()
  203. {
  204. CheckVersion();
  205. InitBugly();
  206. }
  207. public void CheckVersion()
  208. {
  209. mGameVersion = Application.version;
  210. mResVersion = "0.0.0.0";
  211. string resversion = DownloadMgr.Instance.ResVersion;
  212. if (!String.IsNullOrEmpty(resversion))
  213. {
  214. mResVersion = resversion;
  215. }
  216. //int versionCodeInt = 0;// Wenting.Lebian.LeBianSDK.instance.GetResVerCode();
  217. //VersionCode versionCode1;
  218. //if (versionCodeInt <= 0)
  219. //{
  220. // versionCode1 = mGameVersion;
  221. //}
  222. //else
  223. //{
  224. // versionCode1 = (VersionCode)(uint)versionCodeInt;
  225. //}
  226. //VersionCode versionCode2 = AssetsMgr.Instance.resVersionCode;
  227. //if (versionCode1 < versionCode2)
  228. //{
  229. // mResVersion = versionCode2.ToString();
  230. //}
  231. //else
  232. //{
  233. // mResVersion = versionCode1.ToString();
  234. //}
  235. }
  236. void InitBugly()
  237. {
  238. #if BUGLY
  239. #if UNITY_IPHONE || UNITY_IOS
  240. string channel = ""; //Wenting.Lebian.LeBianSDK.instance.GetClientChId();
  241. BuglyAgent.ConfigDefault(channel, mResVersion, string.Empty, 0);
  242. BuglyAgent.InitWithAppId("3e7f97a53b");
  243. BuglyAgent.EnableExceptionHandler();
  244. #elif UNITY_ANDROID
  245. string channel = "";//Wenting.Lebian.LeBianSDK.instance.GetClientChId();
  246. BuglyAgent.ConfigDefault(channel, mResVersion, string.Empty, 0);
  247. #if LEBIAN_YUN_CLIENT
  248. BuglyAgent.InitWithAppId("96a729256a");
  249. #else
  250. BuglyAgent.InitWithAppId("c7177b1ae7");
  251. #endif
  252. BuglyAgent.EnableExceptionHandler();
  253. #endif
  254. #endif
  255. }
  256. public void StartDetector()
  257. {
  258. DataCheatingDetector.StartDetection(OnDataCheatingDetected);
  259. SpeedHackDetector.StartDetection(OnSpeedHackDetected);
  260. }
  261. bool speedHackDetected = false;
  262. bool dataCheatingDetected = false;
  263. void OnDataCheatingDetected()
  264. {
  265. dataCheatingDetected = true;
  266. //LocalPlayerInfo.SendCheat(ECheatType.CHEAT_DATA);
  267. DebugHelper.LogError("Data Cheating Detected!");
  268. }
  269. void OnSpeedHackDetected()
  270. {
  271. speedHackDetected = true;
  272. //LocalPlayerInfo.SendCheat(ECheatType.CHEAT_SPEED);
  273. DebugHelper.LogError("Speed hack Detected!");
  274. }
  275. private void Update()
  276. {
  277. AntiCheatMgr.Instance.Update();
  278. ResourceMgr.Instance.Update();
  279. TimerManager.Instance.Update();
  280. NetworkMgr.Instance.Update();
  281. UpdateFPS();
  282. #if UNITY_EDITOR || OPENGM
  283. if (Input.GetMouseButtonDown(0))
  284. {
  285. Rect validRect = SafeRectCheck.Instance.safeArea;
  286. validRect.width = validRect.height * 0.025f;
  287. validRect.y = validRect.height + validRect.y - validRect.width;
  288. validRect.height = validRect.width;
  289. if (validRect.Contains(Input.mousePosition))
  290. {
  291. mouseDown = true;
  292. dragged = false;
  293. mouseDownTime = Time.time;
  294. mouseDownPos = Input.mousePosition;
  295. }
  296. else
  297. {
  298. clickCount = 0;
  299. }
  300. }
  301. if (mouseDown && !dragged && Vector3.Distance(Input.mousePosition, mouseDownPos) >= 5.0f)
  302. {
  303. dragged = true;
  304. }
  305. if (Input.GetMouseButtonUp(0))
  306. {
  307. if (mouseDown)
  308. {
  309. mouseDown = false;
  310. if (dragged)
  311. {
  312. clickCount = 0;
  313. }
  314. else
  315. {
  316. dragged = false;
  317. Rect validRect = SafeRectCheck.Instance.safeArea;
  318. validRect.width = validRect.height * 0.025f;
  319. validRect.y = validRect.height + validRect.y - validRect.width;
  320. validRect.height = validRect.width;
  321. if (Time.time - mouseDownTime < 2 && validRect.Contains(Input.mousePosition))
  322. {
  323. clickCount = clickCount + 1;
  324. if (clickCount >= 2)
  325. {
  326. clickCount = 0;
  327. var pLuaState = LuaMgr.GetMainState();
  328. if (null != pLuaState)
  329. {
  330. LuaMgr.GetMainState().DoString("local curUIId = ManagerContainer.LuaUIMgr:GetCurUIId()\nManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIGM, nil, curUIId)");
  331. }
  332. }
  333. }
  334. else
  335. {
  336. clickCount = 0;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. // private void OnGUI() {
  343. // Rect validRect = SafeRectCheck.Instance.safeArea;
  344. // validRect.width = validRect.height * 0.025f;
  345. // validRect.y = SafeRectCheck.Instance.screenHeight - validRect.height - validRect.y;
  346. // validRect.height = validRect.width;
  347. // GUI.DrawTexture(validRect, Texture2D.whiteTexture);
  348. // }
  349. private int clickCount = 0;
  350. private bool mouseDown = false;
  351. private bool dragged = false;
  352. private float mouseDownTime = 0f;
  353. private Vector2 mouseDownPos = Vector2.zero;
  354. #else
  355. }
  356. #endif
  357. protected override void Dispose()
  358. {
  359. base.Dispose();
  360. }
  361. private const float m_FrameUpdateInterval = 0.5f;
  362. private float m_FrameUpdateTime = 0f;
  363. private float m_MaxTimeAccumulator = 0;
  364. private int m_FrameCounter = 0;
  365. private int m_MaxFrameTime = 0;
  366. public int maxFrameTime
  367. {
  368. get { return m_MaxFrameTime; }
  369. }
  370. private void UpdateFPS()
  371. {
  372. float deltaTime = Time.deltaTime;
  373. if (deltaTime > 0)
  374. {
  375. m_FrameUpdateTime -= deltaTime;
  376. m_MaxTimeAccumulator += Time.timeScale / deltaTime;
  377. m_FrameCounter++;
  378. }
  379. if (m_FrameUpdateTime <= 0)
  380. {
  381. m_MaxFrameTime = Mathf.RoundToInt(m_MaxTimeAccumulator / m_FrameCounter);
  382. m_FrameUpdateTime = m_FrameUpdateInterval;
  383. m_MaxTimeAccumulator = 0;
  384. m_FrameCounter = 0;
  385. }
  386. }
  387. void EnterLuaLogin(bool relogin)
  388. {
  389. if (mCharacterInfo == null)
  390. {
  391. mCharacterInfo = new MainCharacter();
  392. }
  393. LuaMgr.Instance.EnterLogin(relogin);
  394. }
  395. void DisposeCharactorInfo()
  396. {
  397. mCharacterInfo.Dispose();
  398. mCharacterInfo = null;
  399. }
  400. void OnLogout(CoreEvent<int> ce)
  401. {
  402. }
  403. public void SetOnSceneLoadedLuaFunc(LuaFunction func)
  404. {
  405. OnSceneLoaded2Lua = func;
  406. }
  407. public void ReLogin()
  408. {
  409. ShutDownBattle(false);//清空战斗数据 逻辑 (等待loading界面完成 清空战斗实例)
  410. DisposeCharactorInfo();
  411. EnterLuaLogin(true);
  412. }
  413. public void ReLoginClearData()
  414. {
  415. ClearMainCharacter();
  416. ActorDataMgr.Instance.Clear();
  417. DisposeBattle();//销毁战斗实例
  418. BattleMgr.Instance.ClearSkillDirty();//销毁待更新技能
  419. SceneMgr.Instance.LoadMainScene("relogin");
  420. }
  421. void ClearMainCharacter()
  422. {
  423. if (mCharacterInfo != null)
  424. {
  425. mCharacterInfo.Clear();
  426. }
  427. }
  428. public void SetLuaPlayStoryFunc(LuaFunction func)
  429. {
  430. mPlayStoryLuaFun = func;
  431. }
  432. public void LoadCurrentBattle()
  433. {
  434. GameStateCtrl.Instance.GotoState(LoadingState);
  435. }
  436. public void CloseLoading()
  437. {
  438. if (BattleMgr.Instance.Battle == null) return;
  439. if (BattleMgr.Instance.Battle.IsBossBattle && BattleMgr.Instance.Battle.SubBattleMode == BattleSubMode.NewbieBoss)
  440. {
  441. BattleMgr.Instance.StartStoryScript();
  442. }
  443. }
  444. public void SetTeamData(LuaTable teamParam, LuaTable tbIsForce)
  445. {
  446. if (mCharacterInfo != null)
  447. {
  448. mCharacterInfo.SetTeamActors(teamParam);
  449. bool bIsForce = false;//是否强制同步
  450. if (tbIsForce[1] != null)
  451. {
  452. bool.TryParse(tbIsForce[1].ToString(), out bIsForce);
  453. }
  454. BattleMgr.Instance.SyncTeams(bIsForce);
  455. }
  456. }
  457. public void RefreshTeamData()
  458. {
  459. for (int idx = 0; idx < mCharacterInfo.TeamActors.Count; idx++)
  460. {
  461. ActorDataMgr.Instance.RefreshFellowActorData(mCharacterInfo.TeamActors[idx].ID, mCharacterInfo.TeamActors[idx].BaseId);
  462. }
  463. }
  464. public void UpdateTeamSkills(LuaTable skillsParam)
  465. {
  466. if (mCharacterInfo != null)
  467. {
  468. BattleMgr.Instance.UpdateTeamSkills(skillsParam);
  469. }
  470. }
  471. /// <summary>
  472. /// 完成角色创建
  473. /// </summary>
  474. /// <param name="modelGo"></param>
  475. public void CreateRoleViewComplete(GameObject modelGo, int roleId = 1)
  476. {
  477. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, false, false, roleId);
  478. }
  479. /// <summary>
  480. /// 角色形象更新完成
  481. /// </summary>
  482. /// <param name="modelGo"></param>
  483. public void RefreshRoleViewComplete(GameObject modelGo, bool isNew, int roleId = 1)
  484. {
  485. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, true, isNew, roleId);
  486. }
  487. /// <summary>
  488. /// 时装或者其它功能更新了角色形象,战斗内需要自行调用形象更新 参数 是否为转职后的新角色
  489. /// </summary>
  490. public void NotifyRefreshRoleView()
  491. {
  492. BattleMgr.Instance.NotifyRefreshRoleView();
  493. }
  494. public void SetMapLevelId(int mapId, int levelId)
  495. {
  496. if (mCharacterInfo == null) return;
  497. if (mCharacterInfo.CurMapId != mapId)
  498. {
  499. mCharacterInfo.CurMapId = mapId;
  500. mCharacterInfo.CurLevelId = levelId;
  501. }
  502. else
  503. {
  504. if (mCharacterInfo.CurLevelId != levelId)
  505. {
  506. mCharacterInfo.CurLevelId = levelId;
  507. BattleMgr.Instance.EnterNextBattle();
  508. }
  509. }
  510. //BattleMgr.Instance?.SetMapLevelId(mapId,levelId);
  511. Debug.Log($"=========SetMapLevelId============={mCharacterInfo.CurMapId} --- {mCharacterInfo.CurLevelId}");
  512. }
  513. public void PreloadCreateRoleScene()
  514. {
  515. mEnterSceneType = SceneType.CreateRoleScene;
  516. CreateRoleMgr.Instance.LoadScene();
  517. }
  518. public void GotoLogin()
  519. {
  520. BattleMgr.Instance.ShutDownCurrentBattle();
  521. }
  522. //进入公会大厅
  523. public void PreloadGuildLobby()
  524. {
  525. mEnterSceneType = SceneType.GuildLobbyScene;
  526. BattleMgr.Instance.ShutDownCurrentBattle();
  527. GuildLobbyMgr.Instance.EnterGuildLobby();
  528. }
  529. public void PreloadAllFellows(ActorData[] actors)
  530. {
  531. if (actors == null || actors.Length == 0) return;
  532. for (int idx = 0; idx < actors.Length; idx++)
  533. {
  534. var actor = actors[idx];
  535. if (actor != null)
  536. {
  537. BattlePrepareManager.Instance.PrecacheModel(actor.AvatarData.prefab, actor.AType);
  538. BattlePrepareManager.Instance.PrecacheAnimatorCtrl(actor.ShowAnimCtrlName);
  539. }
  540. }
  541. }
  542. public void PreloadBattle(LuaTable teamParam)
  543. {
  544. mEnterSceneType = SceneType.NormalBattleScene;
  545. GuildLobbyMgr.Instance.Clear();
  546. mCharacterInfo.SetTeamActors(teamParam);
  547. LogicBattleStartInfo startInfo = new LogicBattleStartInfo(mCharacterInfo.CurMapId, mCharacterInfo.CurLevelId);
  548. Debug.Log($"============PreloadBattle=========={mCharacterInfo.CurMapId} --- {mCharacterInfo.CurLevelId}");
  549. BattleMgr.Instance.ShutDownCurrentBattle();
  550. BattleMgr.Instance.StartBattle(startInfo);
  551. }
  552. public List<ActorData> StartNewbieBossBattle(int sceneId, int roleSex)
  553. {
  554. mEnterSceneType = SceneType.BossBattleScene;
  555. BattleMgr.Instance.ShutDownCurrentBattle();
  556. return BattleMgr.Instance.StartNewbieBossBattle(sceneId, roleSex);
  557. }
  558. public void PreloadViewTeam(ActorData[] teamActors, string sceneName, LuaTable tbl)
  559. {
  560. mEnterSceneType = SceneType.TowerBattleScene;
  561. BattleMgr.Instance.ShutDownCurrentBattle();
  562. PreviewTeamMgr.Instance.LoadPreviewActorsAndScene(teamActors, sceneName, tbl);
  563. }
  564. public void PreloadVersusBattle(BattleSubMode mode,
  565. ActorData[] teamActors,
  566. ActorData[] enemyActors,
  567. string sceneName,
  568. float maxFightingTime,
  569. BattleEndCondition[] endConds,
  570. GvGMark[] OurMarks,
  571. GvGMark[] EnemyMarks,
  572. bool IsPresspoint,
  573. int nPresspoint)
  574. {
  575. mEnterSceneType = SceneType.TowerBattleScene;
  576. BattleMgr.Instance.ShutDownCurrentBattle();
  577. BattleMgr.Instance.StartVersusBattle(mode, teamActors, enemyActors, sceneName, maxFightingTime, endConds, OurMarks, EnemyMarks, IsPresspoint, nPresspoint);
  578. }
  579. public void PreloadBossBattle(ActorData[] actors,
  580. int bossId,
  581. int sceneId,
  582. BattleEndCondition[] endConds)
  583. {
  584. mEnterSceneType = SceneType.BossBattleScene;
  585. BattleMgr.Instance.ShutDownCurrentBattle();
  586. BattleMgr.Instance.StartBossBattle(actors, bossId, sceneId, endConds);
  587. }
  588. public bool PreloadTimeBattle(LuaTable luaTbl,
  589. BattleSubMode mode,
  590. float maxFightingTime,
  591. string sceneName,
  592. string bgmMusic,
  593. ActorData[] ourActors,
  594. ActorData[] enemyActors,
  595. ServerFighterParam[] fighterParams,
  596. BattleEndCondition[] endConds,
  597. ValType[] factors,
  598. int nRestoreSp,
  599. GvGMark[] OurMarks,
  600. GvGMark[] EnemyMarks
  601. )
  602. {
  603. mEnterSceneType = SceneType.TimeBattleScene;
  604. return BattleMgr.Instance.StartTimeBattle(luaTbl, mode, maxFightingTime, sceneName, bgmMusic, ourActors, enemyActors, fighterParams, endConds, factors, nRestoreSp, OurMarks, EnemyMarks);
  605. }
  606. public bool ReplayTimeBattle(LuaTable luaTbl, BattleSubMode mode, float maxFightingTime, string sceneName, string bgmMusic, string battleRecordStr)
  607. {
  608. mEnterSceneType = SceneType.TimeBattleScene;
  609. return BattleMgr.Instance.ReplayTimeBattle(luaTbl, mode, maxFightingTime, sceneName, bgmMusic, battleRecordStr);
  610. }
  611. public void ForceStopBattle()
  612. {
  613. BattleMgr.Instance.ForceStopBattle();
  614. }
  615. //关闭清空战斗
  616. public void ShutDownBattle(bool bIsDispose = true)
  617. {
  618. BattleMgr.Instance.ShutDownCurrentBattle(bIsDispose);
  619. }
  620. //销毁战斗
  621. public void DisposeBattle()
  622. {
  623. BattleMgr.Instance.DisposeCurrentBattle();
  624. }
  625. public void SetGameSpeed(float speed)
  626. {
  627. BattleMgr.Instance.SetSpeedUp(speed);
  628. }
  629. public void SaveGameSpeed(float speed)
  630. {
  631. mGameSpeed = speed;
  632. BattleMgr.Instance.SetSpeedUp(speed);
  633. }
  634. public float GetGameSpeed()
  635. {
  636. return BattleMgr.speed_up_rate;
  637. }
  638. private void OnLoadComplete(CoreEvent<bool> ce)
  639. {
  640. #if USE_LUA
  641. if (OnSceneLoaded2Lua != null)
  642. {
  643. OnSceneLoaded2Lua.Call(this, mEnterSceneType, BattleMgr.Instance.Battle != null ? BattleMgr.Instance.Battle.SubBattleMode : BattleSubMode.None);
  644. }
  645. #else
  646. EnterBattleState();
  647. #endif
  648. }
  649. public int Random(int min, int max)
  650. {
  651. return max > min ? rand.Next(min, max) : min;
  652. }
  653. public uint CalcPassedTime(long timeStr)
  654. {
  655. ulong time = (ulong)timeStr;
  656. time = TimerManager.Instance.serverTime - time;
  657. if (time > uint.MaxValue)
  658. {
  659. return uint.MaxValue;
  660. }
  661. else if (time < 0)
  662. {
  663. return 0;
  664. }
  665. else
  666. {
  667. return (uint)time;
  668. }
  669. }
  670. public void QuitGame(bool isShowView = false)
  671. {
  672. //需要打开退出页面
  673. if (isShowView)
  674. {
  675. //如果sdk有就打开
  676. if (SDKMgr.Instance.CheckHasModul(SDKModulType.EXIT_VIEW))
  677. {
  678. SDKMgr.Instance.OpenModul(SDKModulType.EXIT_VIEW);
  679. }
  680. else
  681. {
  682. if (!SDKMgr.Instance.Exit())
  683. {
  684. KillApplication();
  685. }
  686. }
  687. }
  688. else
  689. {
  690. if (!SDKMgr.Instance.Exit())
  691. {
  692. KillApplication();
  693. }
  694. }
  695. }
  696. private void KillApplication()
  697. {
  698. // 游戏需要保存数据的在这里保存
  699. GameSettings.Instance.Save();
  700. if (SDKMgr.Instance.Quit()) return;
  701. #if UNITY_EDITOR
  702. UnityEditor.EditorApplication.isPlaying = false;
  703. #else
  704. Application.Quit();
  705. #endif
  706. }
  707. public void PlayDialog(int dialogueType, int dialogueId = 0)
  708. {
  709. if (mPlayStoryLuaFun != null)
  710. mPlayStoryLuaFun.Call(this, dialogueType, dialogueId);
  711. }
  712. public void RequestHttpServer(string url, LuaFunction luaFunc_)
  713. {
  714. StartCoroutine(DoRequestHttpServer(url, luaFunc_));
  715. }
  716. IEnumerator DoRequestHttpServer(string url, LuaFunction luaFunc_)
  717. {
  718. if (string.IsNullOrEmpty(url))
  719. {
  720. yield break;
  721. }
  722. WWW httpReq = new WWW(url);
  723. yield return httpReq;
  724. if (!string.IsNullOrEmpty(httpReq.error))
  725. {
  726. DebugHelper.LogError("DoRequestHttpServer failed:{0}[url = {1}]", httpReq.error,url);
  727. httpReq.Dispose();
  728. httpReq = null;
  729. yield break;
  730. }
  731. string content = httpReq.text;
  732. httpReq.Dispose();
  733. httpReq = null;
  734. if (luaFunc_ != null)
  735. {
  736. luaFunc_.Call(content);
  737. }
  738. }
  739. public void CleanUnusedAssets()
  740. {
  741. StartCoroutine(UnloadAssets_Coroutine());
  742. }
  743. public void OpenUrl(string url)
  744. {
  745. if (string.IsNullOrEmpty(url)) return;
  746. SDKMgr.Instance.OpenWebview(url);
  747. //Application.OpenURL(url);
  748. }
  749. public void EnableAntiAliasing()
  750. {
  751. if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE)
  752. {
  753. QualitySettings.antiAliasing = 2;
  754. }
  755. else if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.GOOD_DEVICE)
  756. {
  757. QualitySettings.antiAliasing = 4;
  758. }
  759. //DebugHelper.LogError("antiAliasing" + QualitySettings.antiAliasing);
  760. }
  761. public void DisableAntiAliasing()
  762. {
  763. QualitySettings.antiAliasing = 0;
  764. //DebugHelper.LogError("antiAliasing" + QualitySettings.antiAliasing);
  765. }
  766. IEnumerator UnloadAssets_Coroutine()
  767. {
  768. yield return 0;
  769. yield return Resources.UnloadUnusedAssets();
  770. System.GC.Collect();
  771. }
  772. #region inner_methods
  773. private void RegisterEvents()
  774. {
  775. EventMgr.AddEventListener<int>(ECoreEventType.EID_Logout, OnLogout);
  776. EventMgr.AddEventListener<bool>(ECoreEventType.EID_LOAD_COMPLETE, OnLoadComplete);
  777. //EventMgr.AddEventListener<bool,string>(ECoreEventType.EID_SDK_INIT_RESULT, OnSdkInitRet);
  778. //EventMgr.AddEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_LOGIN_RESULT_NEW, OnSdkLoginRet);
  779. //EventMgr.AddEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_SWITCH_ACCOUNT_NEW, OnSdkSwitchAccount);
  780. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_LOGOUT, OnSdkLogout);
  781. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_EXIT, OnSdkExit);
  782. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_SUCCESS, OnSdkPaySuccess);
  783. EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_CANCEL, OnSdkPayCancel);
  784. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_FAILED, OnSdkPayFailed);
  785. EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_QUESTION_REWARD_RESULT, OnSdkQuestionRewardResult);
  786. SDKEventUtil.AddListener(SDKCBEnum.EXIT_SUCCESS_CB, OnSdkExit);
  787. SDKEventUtil.AddListener(SDKCBEnum.INIT_FAILED_CB, OnSdkInitFail);
  788. SDKEventUtil.AddListener(SDKCBEnum.INIT_SUCCESS_CB, OnSdkInitSuccess);
  789. SDKEventUtil.AddListener(SDKCBEnum.LOGIN_FAILED_CB, OnSdkLoginFail);
  790. SDKEventUtil.AddListener(SDKCBEnum.LOGIN_SUCCESS_CB, OnSdkLoginSuccess);
  791. SDKEventUtil.AddListener(SDKCBEnum.LOGOUT_SUCCESS_CB, OnSdkLogoutSuccess);
  792. SDKEventUtil.AddListener(SDKCBEnum.PAY_FAILED_CB, OnSdkPayFailed);
  793. SDKEventUtil.AddListener(SDKCBEnum.PAY_SUCCESS_CB, OnSdkPaySuccess);
  794. SDKEventUtil.AddListener(SDKCBEnum.SWITCH_FAILED_CB, OnSdkSwitchAccountFailed);
  795. SDKEventUtil.AddListener(SDKCBEnum.SWITCH_SUCCESS_CB, OnSdkSwitchAccountSuccess);
  796. SDKEventUtil.AddListener(SDKCBEnum.CAN_ENTER_SERVER_JUDGE_CB, OnSdkCanEnterServerJudge);
  797. }
  798. private void UnRegisterEvents()
  799. {
  800. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_Logout, OnLogout);
  801. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_LOAD_COMPLETE, OnLoadComplete);
  802. //EventMgr.RemoveEventListener<bool,string>(ECoreEventType.EID_SDK_INIT_RESULT, OnSdkInitRet);
  803. //EventMgr.RemoveEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_LOGIN_RESULT_NEW, OnSdkLoginRet);
  804. //EventMgr.RemoveEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_SWITCH_ACCOUNT_NEW, OnSdkSwitchAccount);
  805. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_LOGOUT, OnSdkLogout);
  806. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_EXIT, OnSdkExit);
  807. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_SUCCESS, OnSdkPaySuccess);
  808. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_CANCEL, OnSdkPayCancel);
  809. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_FAILED, OnSdkPayFailed);
  810. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_QUESTION_REWARD_RESULT, OnSdkQuestionRewardResult);
  811. SDKEventUtil.RemoveListener(SDKCBEnum.EXIT_SUCCESS_CB, OnSdkExit);
  812. SDKEventUtil.RemoveListener(SDKCBEnum.INIT_FAILED_CB, OnSdkInitFail);
  813. SDKEventUtil.RemoveListener(SDKCBEnum.INIT_SUCCESS_CB, OnSdkInitSuccess);
  814. SDKEventUtil.RemoveListener(SDKCBEnum.LOGIN_FAILED_CB, OnSdkLoginFail);
  815. SDKEventUtil.RemoveListener(SDKCBEnum.LOGIN_SUCCESS_CB, OnSdkLoginSuccess);
  816. SDKEventUtil.RemoveListener(SDKCBEnum.LOGOUT_SUCCESS_CB, OnSdkLogoutSuccess);
  817. SDKEventUtil.RemoveListener(SDKCBEnum.PAY_FAILED_CB, OnSdkPayFailed);
  818. SDKEventUtil.RemoveListener(SDKCBEnum.PAY_SUCCESS_CB, OnSdkPaySuccess);
  819. SDKEventUtil.RemoveListener(SDKCBEnum.SWITCH_FAILED_CB, OnSdkSwitchAccountFailed);
  820. SDKEventUtil.RemoveListener(SDKCBEnum.SWITCH_SUCCESS_CB, OnSdkSwitchAccountSuccess);
  821. SDKEventUtil.RemoveListener(SDKCBEnum.CAN_ENTER_SERVER_JUDGE_CB, OnSdkCanEnterServerJudge);
  822. }
  823. private void StartLog()
  824. {
  825. DebugHelper debugHelper = this.gameObject.GetComponent<DebugHelper>();
  826. if (debugHelper == null)
  827. {
  828. this.gameObject.AddComponent<DebugHelper>();
  829. }
  830. DebugHelper.enableLog = true;
  831. DebugHelper.BeginLogs();
  832. }
  833. private IEnumerator ReadVersionFile()
  834. {
  835. string path = FileSystem.LocalPackagePath;
  836. #if !UNITY_STANDALONE_WIN
  837. #if UNITY_EDITOR //editor
  838. path = string.Format("{0}/", Application.dataPath);
  839. #elif UNITY_ANDROID && !UNITY_EDITOR//android release;
  840. path = FileSystem.LocalPackagePath;
  841. #elif UNITY_IPHONE && !UNITY_EDITOR //ios release;
  842. path = FileSystem.LocalPackagePath;
  843. #else //pc release;
  844. path = Application.dataPath;
  845. #endif
  846. #else
  847. path = FileSystem.LocalPackagePath;
  848. #endif
  849. //热更地址从新整合的配置中提取
  850. string UpdateFilePath = string.Format("{0}appbuildconfig.xml", path);
  851. WWW www = new WWW(UpdateFilePath);
  852. DebugHelper.Log("ReadUpdateFile : LocalPackagePath {0}", UpdateFilePath);
  853. yield return www;
  854. if (www.error == null)
  855. {
  856. DebugHelper.Log("ReadUpdateFile : {0}", www.text);
  857. www.Dispose();
  858. www = null;
  859. }
  860. else
  861. {
  862. DebugHelper.LogError("ReadUpdateFile {0} , {1}", www.error, UpdateFilePath);
  863. www.Dispose();
  864. www = null;
  865. }
  866. }
  867. #endregion
  868. #region ACCOUNT_SDK
  869. //private SDKMgr SdkManager = SDKMgr.Instance;
  870. private LuaFunction mSdkInitedLuaCB = null;
  871. private LuaFunction mSdkLoginedLuaCB = null;
  872. private LuaFunction mSdkLogoutLuaCB = null;
  873. private LuaFunction mSdkPayLuaCB = null;
  874. private LuaFunction msdkQuestionLuaCB = null;
  875. private LuaFunction mSdkCanEnterServerJudgeCB = null;
  876. public void SdkInitFunc(LuaFunction func)
  877. {
  878. mSdkInitedLuaCB = func;
  879. //mSdkInitedLuaCB.Call(this, true, "");
  880. }
  881. public void SdkLoginFunc(LuaFunction func)
  882. {
  883. mSdkLoginedLuaCB = func;
  884. }
  885. public void SdkLogoutFunc(LuaFunction func)
  886. {
  887. mSdkLogoutLuaCB = func;
  888. }
  889. public void SdkPayFunc(LuaFunction func)
  890. {
  891. mSdkPayLuaCB = func;
  892. }
  893. public void SdkQuestionFunc(LuaFunction func)
  894. {
  895. msdkQuestionLuaCB = func;
  896. }
  897. public void SdkCanEnterServerJudgeFunc(LuaFunction func)
  898. {
  899. mSdkCanEnterServerJudgeCB = func;
  900. }
  901. public void InitSdkCfg()
  902. {
  903. string cname = "";
  904. #if UNITY_ANDROID
  905. cname = "AndroidShopProductIDCfg";
  906. #elif UNITY_IOS
  907. cname = "IOSShopProductIDCfg";
  908. #endif
  909. Dictionary<string, Dictionary<string, string>> datas = ConfigMgr.Instance.getTable(cname);
  910. if (datas != null)
  911. SDKMgr.Instance.InitProductCfg(datas);
  912. }
  913. public void SdkInit()
  914. {
  915. SDKMgr.Instance.Init();
  916. InitSdkCfg();
  917. }
  918. public void SdkLogin()
  919. {
  920. SDKMgr.Instance.Login();
  921. }
  922. public void SdkPay(int goodsId, string goodsName, string goodsDesc,
  923. int count, float amount,
  924. string cpOrderId, string extrasParams)
  925. {
  926. int decimalVal = 1000;
  927. int tempVal = Mathf.FloorToInt((amount + 0.0005f) * decimalVal);
  928. amount = (float)tempVal / decimalVal;
  929. if (string.IsNullOrEmpty(goodsName))
  930. {
  931. goodsName = "初心者";
  932. }
  933. SDKMgr.Instance.Pay(goodsId, goodsName, goodsDesc,
  934. count, amount,
  935. cpOrderId, extrasParams);
  936. //Debug.Log($"goodsId = {goodsId} ==== goodsName = {goodsName} ===== goodsDesc = {goodsDesc} ===== count = {count} ===== amount = {amount} ====== cpOrderId = {cpOrderId} ==== extrasParams = {extrasParams}");
  937. }
  938. public void SdkLogout()
  939. {
  940. SDKMgr.Instance.Logout();
  941. }
  942. public void SdkExit()
  943. {
  944. }
  945. public void SdkShowToolbar()
  946. {
  947. //int ret = quicksdk.QuickSDK.getInstance().showToolBar(quicksdk.ToolbarPlace.QUICK_SDK_TOOLBAR_MID_LEFT);
  948. //DebugHelper.LogError("SdkShowToolbar:" + ret);
  949. }
  950. public void SdkHideToolBar()
  951. {
  952. //SdkManager.SdkHideToolBar();
  953. }
  954. private void OnSdkInitFail(object obj)
  955. {
  956. if (mSdkInitedLuaCB != null)
  957. {
  958. mSdkInitedLuaCB.Call(this, false, SDKMgr.Instance.GetSDKName());
  959. }
  960. }
  961. private void OnSdkInitSuccess(object obj)
  962. {
  963. if (mSdkInitedLuaCB != null)
  964. {
  965. mSdkInitedLuaCB.Call(this, true, SDKMgr.Instance.GetSDKName());
  966. }
  967. }
  968. private void OnSdkLoginFail(object obj)
  969. {
  970. if (mSdkLoginedLuaCB != null)
  971. {
  972. mSdkLoginedLuaCB.Call(this, false, "", "", false);
  973. SDKMgr.Instance.ReportRoleEnterFail();
  974. }
  975. }
  976. private void OnSdkLoginSuccess(object obj)
  977. {
  978. if (mSdkLoginedLuaCB != null)
  979. {
  980. if (obj != null)
  981. {
  982. UserInfo userInfo = (UserInfo)obj;
  983. mSdkLoginedLuaCB.Call(this, true, userInfo.uid, userInfo.token, false);
  984. SDKMgr.Instance.ReportIdentification(SDKMgr.Instance.GetInt64TimeStamp());
  985. }
  986. else
  987. {
  988. mSdkLoginedLuaCB.Call(this, true, "", "", false);
  989. }
  990. }
  991. }
  992. private void OnSdkSwitchAccountFailed(object obj)
  993. {
  994. if (mSdkLoginedLuaCB != null)
  995. {
  996. UserInfo userInfo = (UserInfo)obj;
  997. mSdkLoginedLuaCB.Call(this, false, userInfo.uid, userInfo.token, true);
  998. }
  999. }
  1000. private void OnSdkSwitchAccountSuccess(object obj)
  1001. {
  1002. if (mSdkLoginedLuaCB != null)
  1003. {
  1004. UserInfo userInfo = (UserInfo)obj;
  1005. mSdkLoginedLuaCB.Call(this, true, userInfo.uid, userInfo.token, true);
  1006. }
  1007. }
  1008. private void OnSdkLogoutSuccess(object obj)
  1009. {
  1010. if (mSdkLogoutLuaCB != null)
  1011. {
  1012. mSdkLogoutLuaCB.Call(this);
  1013. }
  1014. }
  1015. private void OnSdkExit(object obj)
  1016. {
  1017. KillApplication();
  1018. }
  1019. private void OnSdkPaySuccess(object obj)
  1020. {
  1021. if (mSdkPayLuaCB != null)
  1022. {
  1023. mSdkPayLuaCB.Call(this, true);
  1024. }
  1025. }
  1026. private void OnSdkPayCancel(CoreEvent<bool> ce)
  1027. {
  1028. if (mSdkPayLuaCB != null)
  1029. {
  1030. mSdkPayLuaCB.Call(this, false);
  1031. }
  1032. }
  1033. private void OnSdkPayFailed(object obj)
  1034. {
  1035. if (mSdkPayLuaCB != null)
  1036. {
  1037. mSdkPayLuaCB.Call(this, false);
  1038. }
  1039. }
  1040. private void OnSdkQuestionRewardResult(CoreEvent<bool> ce)
  1041. {
  1042. if (msdkQuestionLuaCB != null)
  1043. {
  1044. msdkQuestionLuaCB.Call(this, true);
  1045. }
  1046. }
  1047. private void OnSdkCanEnterServerJudge(object obj)
  1048. {
  1049. if (mSdkCanEnterServerJudgeCB != null)
  1050. {
  1051. mSdkCanEnterServerJudgeCB.Call(this, "" + obj);
  1052. }
  1053. }
  1054. #endregion
  1055. #region 新加
  1056. private Dictionary<int, int> mDifAttrs = new Dictionary<int, int>();
  1057. public void ClearDifAttr()
  1058. {
  1059. mDifAttrs.Clear();
  1060. }
  1061. public void AddDifAttr(int type,int attr)
  1062. {
  1063. mDifAttrs.Add(type, attr);
  1064. }
  1065. public void AddDifAttrs(Dictionary<object, object> difAttrs)
  1066. {
  1067. foreach (var item in difAttrs)
  1068. {
  1069. mDifAttrs.Add((int)item.Key,(int)item.Value);
  1070. }
  1071. }
  1072. public Dictionary<int, int> GetDifAttrs()
  1073. {
  1074. return mDifAttrs;
  1075. }
  1076. #endregion
  1077. }