UserGameAnalyse.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using LocalRank;
  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 GameResultView showResultView(Action callback)
  73. {
  74. //上传一次数据
  75. UploadData(true);
  76. //显示面板
  77. GameMgr.ins.StopGame();
  78. GameObject resultObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_GameResultView);
  79. GameInfo gameInfo = TextureMgr.ins.GetGameInfos(gameType);
  80. GameResultView gameResultView = resultObj.GetComponent<GameResultView>();
  81. gameResultView.OnBackClicked += callback;
  82. gameResultView.setGameResultInfo(gameInfo.textId, (int)allTimeSecond, allShootingCount);
  83. //如果弹出结算面板,则清空一次数据
  84. onResetOverlayData();
  85. //b端直接返回,不处理弹出框
  86. if (CommonConfig.StandaloneModeOrPlatformB)
  87. {
  88. resultObj.SetActive(false);
  89. callback?.Invoke();
  90. }
  91. return gameResultView;
  92. }
  93. public void onResetOverlayData() {
  94. allTimeSecond = 0;
  95. allShootingCount = 0;
  96. }
  97. }