UserGameAnalyse.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /* 用户游戏情况统计分析-主要向服务器是上传用户游戏数据 */
  5. public class UserGameAnalyse : MonoBehaviour
  6. {
  7. int gameType;
  8. long startTime; //毫秒
  9. float duration; //秒
  10. float sceneTimeOnStart;
  11. float uploadCountOnUpdate = 0;
  12. bool gameIsEnd = false;
  13. public static void CreateWhenGameStart(int gameType) {
  14. new GameObject("UserGameAnaly").AddComponent<UserGameAnalyse>().gameType = gameType;
  15. }
  16. void Start()
  17. {
  18. startTime = JC.CS.Utility.GetTimestamp();
  19. sceneTimeOnStart = Time.realtimeSinceStartup;
  20. }
  21. void OnDestroy() {
  22. UploadData(true);
  23. }
  24. void Update()
  25. {
  26. if (!gameIsEnd) {
  27. duration = Time.realtimeSinceStartup - sceneTimeOnStart;
  28. if (duration / 60 >= uploadCountOnUpdate) { //每60秒上传一次
  29. uploadCountOnUpdate++;
  30. UploadData(false);
  31. }
  32. if (GameMgr.ins.gameOver) {
  33. UploadData(true);
  34. }
  35. }
  36. }
  37. public void UploadData(bool isEnd) {
  38. if (gameIsEnd) return;
  39. if (isEnd) gameIsEnd = true;
  40. try {
  41. UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, (int) duration);
  42. }
  43. catch (System.Exception) {}
  44. }
  45. }