UserGameAnalyse1.cs 4.4 KB

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