/
Game.cs
671 lines (594 loc) · 18.7 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#region using
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using ScriptManager;
using ClientDataModel;
using ClientService;
using DataContract;
using EventSystem;
using ScorpionNetLib;
using Shared;
using UnityEngine;
#endregion
public class Game : MonoBehaviour
{
public static Game Instance;
public GameObject DelayShowObj;
private List<IManager> mMgrList;
public RechargeActivityData RechargeActivityData;
public bool ServerInfoCached;
public DateTime LoginTime { set; private get; }
public List<IManager> MgrList
{
get { return mMgrList; }
private set { }
}
public int OnLineSeconds
{
get { return (int) (ServerTime - LoginTime).TotalSeconds; }
}
public DateTime ServerTime
{
get { return DateTime.Now - ServerTimeDiff; }
}
public TimeSpan ServerTimeDiff { set; get; }
public TimeSpan ServerZoneDiff { set; get; }
private void Awake()
{
#if !UNITY_EDITOR
try
{
#endif
#if UNITY_EDITOR
GameObject rm = null;
rm = GameObject.Find("ResourceManager");
while (rm)
{
DestroyImmediate(rm);
rm = GameObject.Find("ResourceManager");
}
#endif
#if !UNITY_EDITOR
StartCoroutine(DelayShowScreen());
PlatformHelper.PlayLogoMovie();
#endif
TargetBindingProperty.Register();
// var channel = PlatformHelper.GetChannelString();
// Logger.Debug("PlatformHelper.GetChannelString = " + channel);
GameSetting.Channel = GameUtils.GetChannelString();
PlatformHelper.Initialize();
if (null != Instance)
{
Logger.Fatal("ERROR!!!!!!!!!!!!!!!!!!! Game has been created");
return;
}
Instance = this;
var Fps = PlayerPrefs.GetInt(GameSetting.LowFpsKey, 60);
Application.targetFrameRate = Fps;
#if UNITY_EDITOR
Application.targetFrameRate = 60;
#endif
//注册所有解析表中表达式中的函数
ExpressionHelper.RegisterAllFunction();
PlayCG.Instance.Init();
mMgrList = new List<IManager>
{
ObjManager.Instance,
SceneManager.Instance,
PlayerDataManager.Instance,
ResourceManager.Instance,
EffectManager.Instance,
UIManager.Instance,
SoundManager.Instance
};
DontDestroyOnLoad(gameObject);
#if !UNITY_EDITOR
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
#endif
}
/// <summary>
/// 更新完成后,重新加载资源前调用
/// </summary>
public void BeforeStartLoading()
{
CleanupAllGameData();
}
public void ChangeSceneToLogin()
{
EventDispatcher.Instance.DispatchEvent(new Close_UI_Event(UIConfig.ServerListUI));
Action act = () =>
{
CleanupAllGameData(false);
const string sceneName = "Login";
ResourceManager.PrepareScene(Resource.GetScenePath(sceneName), www =>
{
LoginLogic.State = LoginLogic.LoginState.BeforeLogin;
ResourceManager.Instance.StartCoroutine(ResourceManager.LoadSceneImpl(sceneName, www));
});
};
GameUtils.ExitLogin(act);
// if (LoginLogic.instance )
// {
// const string sceneName = "Login";
// ResourceManager.PrepareScene(Resource.GetScenePath(sceneName), (www) =>
// {
// LoginLogic.State = LoginLogic.LoginState.BeforeLogin;
// ResourceManager.Instance.StartCoroutine(ResourceManager.LoadSceneImpl(sceneName, www));
// });
// }
// else
// {bi
// LoginLogic.State = LoginLogic.LoginState.BeforeLogin;
// LoginLogic.instance.Init();
// }
}
public void ChangeSceneToLoginAndAutoLogin(Action afterChange)
{
CleanupAllGameData();
const string sceneName = "Login";
ResourceManager.PrepareScene(Resource.GetScenePath(sceneName), www =>
{
LoginLogic.State = LoginLogic.LoginState.ThirdLogin;
LoginLogic.ThirdLoginAction = afterChange;
ResourceManager.Instance.StartCoroutine(ResourceManager.LoadSceneImpl(sceneName, www));
});
}
private void CleanupAllGameData(bool stopNet = true)
{
try
{
EventDispatcher.Instance.DispatchEvent(new UIEvent_VisibleEyeCanBeStart(false));
ServerInfoCached = false;
PlatformHelper.CloseToolBar();
//正在loading过程中,如果被踢下线,删除掉全局的uiroot
if (null != LoadingLogic.Instance)
{
var loadingRoot = GameObject.Find("LoadingObject");
if (null != loadingRoot)
{
Destroy(loadingRoot);
}
}
if (null != ObjManager.Instance.MyPlayer)
{
Destroy(ObjManager.Instance.MyPlayer.gameObject);
}
var bc = Instance.GetComponent<BrightnessController>();
if (bc)
{
bc.OnTouchOrMouseRelease();
}
NetManager.Instance.SyncCenter.Clear();
EventDispatcher.Instance.RemoveAllEventListeners();
CleanUpManagers();
NetManager.Instance.StopAllCoroutines();
ConditionTrigger.Instance.Init();
TimeManager.Instance.CleanUp();
PlayerAttr.Instance.CleanUp();
UIManager.Instance.ClearCacheUI();
if (stopNet)
{
NetManager.Instance.Stop();
}
}
catch (Exception e)
{
Logger.Log2Bugly("----CleanupAllGameData throw exception:{0}", e);
throw;
}
}
public void CleanUpManagers()
{
{
var __list3 = Instance.MgrList;
var __listCount3 = __list3.Count;
for (var __i3 = 0; __i3 < __listCount3; ++__i3)
{
var manager = __list3[__i3];
{
manager.Reset();
}
}
}
}
//异步播放片头logo会显示一帧游戏画面,所以先隐藏一秒之后再显示
private IEnumerator DelayShowScreen()
{
if (null == DelayShowObj)
{
yield break;
}
DelayShowObj.SetActive(false);
yield return new WaitForSeconds(1);
DelayShowObj.SetActive(true);
}
public void EnterStartup()
{
//PlatformHelper.UserLogout();
CleanupAllGameData();
ResourceManager.Instance.UnloadCommonBundle();
ResourceManager.Instance.ClearCache(true);
var game = GameObject.Find("Game");
DestroyImmediate(game);
Application.LoadLevel("Startup");
}
private IEnumerator ExitSelectCharacterCoroutine(Action callback = null)
{
var characterId = PlayerDataManager.Instance.CharacterGuid;
if (characterId == 0ul)
{
if (null != callback)
{
callback();
}
yield break;
}
var msg = NetManager.Instance.ExitSelectCharacter(characterId);
UIManager.Instance.ShowBlockLayer();
yield return msg.SendAndWaitUntilDone();
if (msg.State == MessageState.Reply)
{
if (msg.ErrorCode == (int) ErrorCodes.OK)
{
ObjManager.Instance.Reset();
PlayerDataManager.Instance.CharacterLists = msg.Response.Info;
PlayerDataManager.Instance.SelectedRoleIndex = msg.Response.SelectId;
PlayerDataManager.Instance.CharacterGuid = 0ul;
//umeng登出
PlatformHelper.ProfileSignOff();
if (null != callback)
{
callback();
}
}
else
{
UIManager.Instance.RemoveBlockLayer();
Logger.Error(".....ExitSelectCharacter.......{0}.", msg.ErrorCode);
//UIManager.Instance.ShowMessage(MessageBoxType.Ok, "ExitSelectCharacter error:"+ msg.ErrorCode);
ExitToLogin();
}
}
else
{
UIManager.Instance.RemoveBlockLayer();
Logger.Error(".....ExitSelectCharacter.......time out!");
ExitToLogin();
//UIManager.Instance.ShowMessage(MessageBoxType.Ok, "ExitSelectCharacter time out!");
}
}
//所有退回登陆界面总入口
public void ExitToLogin()
{
if (GameUtils.IsOurChannel())
{
ChangeSceneToLogin();
}
else
{
PlatformHelper.UserLogout();
}
GVoiceManager.Instance.QuitRoom(true);
EraManager.Instance.Clear();
PlayerDataManager.Instance.IsCheckSailingTip = false;
}
public void ExitToSelectRole()
{
Action action = () =>
{
CleanupAllGameData(false);
ResourceManager.PrepareScene(Resource.GetScenePath("SelectCharacter"), www =>
{
ResourceManager.Instance.StartCoroutine(ResourceManager.LoadSceneImpl("SelectCharacter", www, () =>
{
LoginLogic.State = LoginLogic.LoginState.LoginSuccess;
var serverName = string.Empty;
var controller = UIManager.Instance.GetController(UIConfig.ServerListUI);
if (null != controller)
{
var datamodel = controller.GetDataModel("") as ServerListDataModel;
if (null != datamodel)
{
serverName = datamodel.SelectedServer.ServerName;
}
}
}));
});
};
NetManager.Instance.StartCoroutine(ExitSelectCharacterCoroutine(action));
GVoiceManager.Instance.QuitRoom(true);
EraManager.Instance.Clear();
PlayerDataManager.Instance.IsCheckSailingTip = false;
}
public void ExitToServerList(bool bQuick = false)
{
Action action = () =>
{
const string sceneName = "Login";
ResourceManager.PrepareScene(Resource.GetScenePath(sceneName), www =>
{
LoginLogic.State = LoginLogic.LoginState.LoginSuccess;
ResourceManager.Instance.StartCoroutine(ResourceManager.LoadSceneImpl(sceneName, www, () =>
{
if (ServerInfoCached && bQuick)
{
EventDispatcher.Instance.DispatchEvent(new Show_UI_Event(UIConfig.ServerListUI));
}
else
{
CleanupAllGameData(false);
NetManager.Instance.StartCoroutine(LoginLogic.LoginSuccess());
}
}));
});
};
NetManager.Instance.StartCoroutine(ExitSelectCharacterCoroutine(action));
GVoiceManager.Instance.QuitRoom(true);
EraManager.Instance.Clear();
PlayerDataManager.Instance.IsCheckSailingTip = false;
}
private void LateUpdate()
{
#if !UNITY_EDITOR
try
{
#endif
// if (ChangeSceneList.Count > 0 && ChangeScenestate == eChangeSceneState.Finished)
// {
// Action act = ChangeSceneList.Dequeue();
// act();
// }
Profiler.BeginSample("UIRect.Update()");
UIRect.Update();
Profiler.EndSample();
#if !UNITY_EDITOR
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
#endif
}
private void LocalNotificationTest()
{
// PlatformHelper.ClearAllLocalNotification();
// PlatformHelper.SetLocalNotification("key1","key1 测试本地通知,这条消息应该在启动游戏一分钟后弹出!",60);
// PlatformHelper.SetLocalNotification("key2", "key2 测试本地通知,这条消息你看不到!", 120);
// PlatformHelper.DeleteLocalNotificationWithKey("key2");
}
private void OnApplicationPause(bool pauseStatus)
{
#if !UNITY_EDITOR
if (LoginLogic.State != LoginLogic.LoginState.InGaming)
{
return;
}
if (!pauseStatus)
{
NetManager.Instance.ResumeStop();
if (!NetManager.Instance.Connected)
{
//正在重新连接...
//Debug.LogError("正在重新连接...");
// this.StartCoroutine(NetManager.Instance.OnServerLost());
NetManager.Instance.IsReconnecting = false;
Game.Instance.ExitToLogin();
}
}
else
{
if (NetManager.Instance.Connected && ObjManager.Instance.MyPlayer != null)
{
ObjManager.Instance.MyPlayer.StopMove();
}
NetManager.Instance.NeedReconnet = true;
NetManager.Instance.StopAfter(1000*60*4);
}
GVoiceManager.Instance.OnApplicationPause(pauseStatus);
#endif
}
private void OnDestroy()
{
#if !UNITY_EDITOR
try
{
#endif
{
var __list2 = mMgrList;
var __listCount2 = __list2.Count;
for (var __i2 = 0; __i2 < __listCount2; ++__i2)
{
var mgr = __list2[__i2];
{
if (mgr != null)
{
try
{
mgr.Destroy();
}
catch (Exception e)
{
//
}
}
}
}
}
mMgrList.Clear();
#if !UNITY_EDITOR
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
#endif
}
public static void SetGameQuality()
{
var defaultQualityLevel = 3;
#if UNITY_IOS
{
iPhoneGeneration iOSGen = iPhone.generation;
if (iOSGen == iPhoneGeneration.iPhone3GS)
{
defaultQualityLevel = 1;
}
else if (iOSGen == iPhoneGeneration.iPhone4)
{
defaultQualityLevel = 1;
}
else if (iOSGen == iPhoneGeneration.iPhone4S)
{
defaultQualityLevel = 1;
}
else if (iOSGen == iPhoneGeneration.iPhone5 || iOSGen == iPhoneGeneration.iPhone5C)
{
defaultQualityLevel = 2;
}
else if (iOSGen == iPhoneGeneration.iPhone5S || iOSGen == iPhoneGeneration.iPhone6 ||
iOSGen == iPhoneGeneration.iPhone6Plus)
{
defaultQualityLevel = 3;
}
else if (iOSGen == iPhoneGeneration.iPad1Gen)
{
defaultQualityLevel = 1;
}
else if (iOSGen == iPhoneGeneration.iPad2Gen)
{
defaultQualityLevel = 1;
}
else if (iOSGen == iPhoneGeneration.iPad3Gen)
{
defaultQualityLevel = 2;
}
else if (iOSGen == iPhoneGeneration.iPodTouch3Gen)
{
defaultQualityLevel = 2;
}
else if (iOSGen == iPhoneGeneration.iPodTouch4Gen)
{
defaultQualityLevel = 2;
}
else
{
string device = SystemInfo.deviceModel;
Logger.Debug("Current device is :" + device);
if (device == "iPhone")
{
defaultQualityLevel = 3;
}
else if (device == "iPad")
{
defaultQualityLevel = 3;
}
else if (device == "iPod")
{
defaultQualityLevel = 1;
}
}
}
#endif
#if UNITY_ANDROID
{
var ram = SystemInfo.systemMemorySize;
// var vram = SystemInfo.graphicsMemorySize;
var cpus = SystemInfo.processorCount;
if (cpus > 4 && ram >= 2800)
{
defaultQualityLevel = 3;
}
else if (ram >= 1900)
{
defaultQualityLevel = 2;
}
else
{
defaultQualityLevel = 1;
}
}
#endif
if (null == GameSetting.Instance)
{
Logger.Error("GameSetting.Instance = null , call SetGameQuality too early!");
return;
}
var level = PlayerPrefs.GetInt(GameSetting.GameQuilatyKey, defaultQualityLevel);
if (level < 1)
{
level = defaultQualityLevel;
}
GameSetting.Instance.GameQualityLevel = level;
}
private void Start()
{
#if !UNITY_EDITOR
try
{
#endif
Logger.LogLevel = GameSetting.Instance.LogLevel;
//进入游戏后在设置画质
//SetGameQuality();
#if !UNITY_EDITOR
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
#endif
}
private void Update()
{
#if !UNITY_EDITOR
try
{
#endif
if (Input.GetKeyDown(KeyCode.Escape))
{
PlatformHelper.Exit();
}
{
var __list1 = mMgrList;
var __listCount1 = __list1.Count;
for (var __i1 = 0; __i1 < __listCount1; ++__i1)
{
var mgr = __list1[__i1];
{
try
{
// Profiler.BeginSample(mgr.GetType().ToString());
mgr.Tick(Time.deltaTime);
// Profiler.EndSample();
}
catch
{
// some mgr failed.
}
}
}
}
Profiler.BeginSample("TimeManager.Updata()");
TimeManager.Instance.Updata();
Profiler.EndSample();
Profiler.BeginSample("GVoiceManager.Updata()");
GVoiceManager.Instance.Update();
Profiler.EndSample();
Profiler.BeginSample("AnimationUpdateFrequencyController.Tick()");
AnimationUpdateFrequencyController.Tick();
Profiler.EndSample();
ObjBase.UpdateVisiblity();
#if !UNITY_EDITOR
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
#endif
}
}