UserGameAnalyse.cs 3.0 KB

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