UserGameAnalyse1.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  52. return userGameAnalyse;
  53. }
  54. void Awake()
  55. {
  56. _Instance = this;
  57. //DontDestroyOnLoad(gameObject);
  58. startTime = JCUnityLib.TimeUtils.GetTimestamp();
  59. sceneTimeOnStart = Time.realtimeSinceStartup;
  60. }
  61. void OnDestroy()
  62. {
  63. if (_Instance == this) _Instance = null;
  64. UploadData(true);
  65. }
  66. void Update()
  67. {
  68. //if (!IsTargetScene())
  69. //{
  70. // Destroy(gameObject);
  71. // return;
  72. //}
  73. if (gameIsEnd) return;
  74. duration = Time.realtimeSinceStartup - sceneTimeOnStart;
  75. if (duration / 60 >= uploadCountOnUpdate) //每60秒上传一次
  76. {
  77. uploadCountOnUpdate++;
  78. UploadData(false);
  79. }
  80. }
  81. public void UploadData(bool isEnd)
  82. {
  83. if (gameIsEnd) return;
  84. if (isEnd)
  85. {
  86. gameIsEnd = true;
  87. //本轮游戏结束时候,记录一次数据
  88. allTimeSecond += duration;
  89. }
  90. int durationTime = (int)duration;
  91. if (UserPlayer.ins != null)
  92. {
  93. UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, durationTime);
  94. //Debug.Log("上传" + targetScene + "(" + gameType + ")游戏时长统计数据:" + durationTime + ",allTimeSecond:" + allTimeSecond);
  95. }
  96. }
  97. bool IsTargetScene()
  98. {
  99. return SceneManager.GetActiveScene().name == targetScene;
  100. }
  101. public int changeShootingCount(int value = 1)
  102. {
  103. shootingCount += value;
  104. allShootingCount += value;
  105. return shootingCount;
  106. }
  107. public void showResultView(Action callback)
  108. {
  109. UploadData(true);
  110. //Debug.Log("ShowResultView" + targetScene + "(" + gameType + ") allTimeSecond:" + allTimeSecond + ",startTime:"+ startTime + ",duration:" + duration);
  111. StandaloneAPI.PauseGame();
  112. GameObject resultObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_GameResultView);
  113. GameInfo gameInfo = TextureMgr.ins.GetGameInfos(gameType);
  114. GameResultView gameResultView = resultObj.GetComponent<GameResultView>();
  115. gameResultView.OnBackClicked += callback;
  116. gameResultView.setGameResultInfo(gameInfo.textId, (int)allTimeSecond, allShootingCount);
  117. onResetOverlayData();
  118. }
  119. public void onResetOverlayData()
  120. {
  121. allTimeSecond = 0;
  122. allShootingCount = 0;
  123. }
  124. }