UserGameAnalyse.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using AppUI.Manager.View;
  6. /* 用户游戏情况统计分析-主要向服务器是上传用户游戏数据 */
  7. public class UserGameAnalyse : MonoBehaviour
  8. {
  9. //跳转关卡时候,记录对应的数据信息。叠加时间和射箭次数
  10. public static float allTimeSecond = 0;
  11. public static int allShootingCount = 0;
  12. int gameType;
  13. long startTime; //毫秒
  14. float duration; //秒
  15. float sceneTimeOnStart;
  16. float uploadCountOnUpdate = 0;
  17. bool gameIsEnd = false;
  18. public int shootingCount = 0;
  19. public static UserGameAnalyse CreateWhenGameStartAndReturn(int gameType)
  20. {
  21. UserGameAnalyse userGameAnalyse = new GameObject("UserGameAnaly").AddComponent<UserGameAnalyse>();
  22. userGameAnalyse.gameType = gameType;
  23. return userGameAnalyse;
  24. }
  25. void Start()
  26. {
  27. startTime = JCUnityLib.TimeUtils.GetTimestamp();
  28. sceneTimeOnStart = Time.realtimeSinceStartup;
  29. }
  30. void OnDestroy() {
  31. UploadData(true);
  32. }
  33. void Update()
  34. {
  35. if (!gameIsEnd) {
  36. duration = Time.realtimeSinceStartup - sceneTimeOnStart;
  37. if (duration / 60 >= uploadCountOnUpdate) { //每60秒上传一次
  38. uploadCountOnUpdate++;
  39. UploadData(false);
  40. }
  41. if (GameMgr.ins.gameOver) {
  42. UploadData(true);
  43. }
  44. }
  45. }
  46. public void UploadData(bool isEnd) {
  47. if (gameIsEnd) return;
  48. if (isEnd)
  49. {
  50. gameIsEnd = true;
  51. //本轮游戏结束时候,记录一次数据
  52. allTimeSecond += duration;
  53. }
  54. try {
  55. UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, (int) duration);
  56. //Debug.Log("上传" + "(" + gameType + ")游戏时长统计数据:" + (int)duration + ",allTimeSecond:" + allTimeSecond);
  57. }
  58. catch (System.Exception) {}
  59. }
  60. /**
  61. * shootCheck 触发
  62. */
  63. public void onShootEvent() {
  64. changeShootingCount();
  65. }
  66. int changeShootingCount(int value = 1)
  67. {
  68. shootingCount += value;
  69. allShootingCount += value;
  70. return shootingCount;
  71. }
  72. public void showResultView(Action callback)
  73. {
  74. //不端直接返回,不处理弹出框
  75. if (CommonConfig.StandaloneModeOrPlatformB)
  76. {
  77. UploadData(true);
  78. onResetOverlayData();
  79. callback?.Invoke();
  80. return;
  81. }
  82. //上传一次数据
  83. UploadData(true);
  84. //显示面板
  85. GameMgr.ins.StopGame();
  86. GameObject resultObj = ViewManager.ShowView(UIViewType.GameResultView);
  87. GameInfo gameInfo = TextureMgr.ins.GetGameInfos(gameType);
  88. GameResultView gameResultView = resultObj.GetComponent<GameResultView>();
  89. gameResultView.OnBackClicked += callback;
  90. gameResultView.setGameResultInfo(gameInfo.textId, (int)allTimeSecond, allShootingCount);
  91. //如果弹出结算面板,则清空一次数据
  92. onResetOverlayData();
  93. }
  94. public void onResetOverlayData() {
  95. allTimeSecond = 0;
  96. allShootingCount = 0;
  97. }
  98. }