UserGameAnalyse1.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. //打鸭子,塔防
  7. /* 用户游戏情况统计分析-主要向服务器是上传用户游戏数据 */
  8. public class UserGameAnalyse1 : MonoBehaviour
  9. {
  10. //重新玩操作,记录对应的数据信息。叠加时间和射箭次数
  11. public static float allTimeSecond = 0;
  12. public static int allShootingCount = 0;
  13. string targetScene;
  14. int gameType;
  15. long startTime; //毫秒
  16. float duration; //秒
  17. float sceneTimeOnStart;
  18. float uploadCountOnUpdate = 0;
  19. bool gameIsEnd = false;
  20. public int shootingCount = 0;
  21. private static UserGameAnalyse1 _Instance;
  22. public static UserGameAnalyse1 Instance
  23. {
  24. get
  25. {
  26. return _Instance;
  27. }
  28. }
  29. public static UserGameAnalyse1 CreateWhenGameStartAndReturn(int gameType)
  30. {
  31. //if (_Instance)
  32. //{
  33. // _Instance.shootingCount = 0;
  34. // _Instance.duration = 0;
  35. // _Instance.gameIsEnd = false;
  36. // return _Instance;
  37. //};
  38. UserGameAnalyse1 userGameAnalyse = new GameObject("UserGameAnaly1").AddComponent<UserGameAnalyse1>();
  39. userGameAnalyse.gameType = gameType;
  40. switch (gameType)
  41. {
  42. case 13:
  43. userGameAnalyse.targetScene = "DuckHunter";
  44. break;
  45. case 14:
  46. userGameAnalyse.targetScene = "WildAttack";
  47. break;
  48. case 15:
  49. userGameAnalyse.targetScene = "FruitMaster";
  50. break;
  51. case 16:
  52. userGameAnalyse.targetScene = "MovingTarget";
  53. break;
  54. }
  55. return userGameAnalyse;
  56. }
  57. void Awake()
  58. {
  59. _Instance = this;
  60. //DontDestroyOnLoad(gameObject);
  61. startTime = JCUnityLib.TimeUtils.GetTimestamp();
  62. sceneTimeOnStart = Time.realtimeSinceStartup;
  63. }
  64. void OnDestroy()
  65. {
  66. if (_Instance == this) _Instance = null;
  67. UploadData(true);
  68. }
  69. void Update()
  70. {
  71. //if (!IsTargetScene())
  72. //{
  73. // Destroy(gameObject);
  74. // return;
  75. //}
  76. if (gameIsEnd) return;
  77. duration = Time.realtimeSinceStartup - sceneTimeOnStart;
  78. if (duration / 60 >= uploadCountOnUpdate) //每60秒上传一次
  79. {
  80. uploadCountOnUpdate++;
  81. UploadData(false);
  82. }
  83. }
  84. public void UploadData(bool isEnd)
  85. {
  86. if (gameIsEnd) return;
  87. if (isEnd)
  88. {
  89. gameIsEnd = true;
  90. //本轮游戏结束时候,记录一次数据
  91. allTimeSecond += duration;
  92. }
  93. int durationTime = (int)duration;
  94. if (UserPlayer.ins != null)
  95. {
  96. UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, durationTime);
  97. //Debug.Log("上传" + targetScene + "(" + gameType + ")游戏时长统计数据:" + durationTime + ",allTimeSecond:" + allTimeSecond);
  98. }
  99. }
  100. bool IsTargetScene()
  101. {
  102. return SceneManager.GetActiveScene().name == targetScene;
  103. }
  104. public int changeShootingCount(int value = 1)
  105. {
  106. shootingCount += value;
  107. allShootingCount += value;
  108. return shootingCount;
  109. }
  110. public void showResultView(Action callback)
  111. {
  112. //不端直接返回,不处理弹出框
  113. if (CommonConfig.StandaloneModeOrPlatformB)
  114. {
  115. UploadData(true);
  116. onResetOverlayData();
  117. callback?.Invoke();
  118. return;
  119. }
  120. UploadData(true);
  121. //Debug.Log("ShowResultView" + targetScene + "(" + gameType + ") allTimeSecond:" + allTimeSecond + ",startTime:"+ startTime + ",duration:" + duration);
  122. StandaloneAPI.PauseGame();
  123. GameObject resultObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_GameResultView);
  124. GameInfo gameInfo = TextureMgr.ins.GetGameInfos(gameType);
  125. if (gameInfo == null) Debug.LogError("缺少GameInfo信息");
  126. GameResultView gameResultView = resultObj.GetComponent<GameResultView>();
  127. gameResultView.OnBackClicked += callback;
  128. gameResultView.setGameResultInfo(gameInfo.textId, (int)allTimeSecond, allShootingCount);
  129. onResetOverlayData();
  130. }
  131. public void onResetOverlayData()
  132. {
  133. allTimeSecond = 0;
  134. allShootingCount = 0;
  135. }
  136. }