UserGameAnalyse1.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. //打鸭子专用
  6. /* 用户游戏情况统计分析-主要向服务器是上传用户游戏数据 */
  7. public class UserGameAnalyse1 : MonoBehaviour
  8. {
  9. int gameType;
  10. long startTime; //毫秒
  11. float duration; //秒
  12. float sceneTimeOnStart;
  13. float uploadCountOnUpdate = 0;
  14. private static UserGameAnalyse1 _Instance;
  15. public static void CreateWhenGameStart() {
  16. if (_Instance) return;
  17. new GameObject("UserGameAnaly1").AddComponent<UserGameAnalyse1>();
  18. }
  19. void Awake()
  20. {
  21. _Instance = this;
  22. DontDestroyOnLoad(gameObject);
  23. gameType = 13;
  24. startTime = JCUnityLib.TimeUtils.GetTimestamp();
  25. sceneTimeOnStart = Time.realtimeSinceStartup;
  26. }
  27. void OnDestroy() {
  28. if (_Instance == this) _Instance = null;
  29. UploadData();
  30. }
  31. void Update()
  32. {
  33. if (!IsTargetScene())
  34. {
  35. Destroy(gameObject);
  36. return;
  37. }
  38. duration = Time.realtimeSinceStartup - sceneTimeOnStart;
  39. if (duration / 60 >= uploadCountOnUpdate) //每60秒上传一次
  40. {
  41. uploadCountOnUpdate++;
  42. UploadData();
  43. }
  44. }
  45. public void UploadData()
  46. {
  47. int durationTime = (int) duration;
  48. UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, durationTime);
  49. Debug.Log("上传用户打鸭子游戏时长统计数据:" + durationTime);
  50. }
  51. bool IsTargetScene()
  52. {
  53. return SceneManager.GetActiveScene().name == "DuckHunter";
  54. }
  55. }