UserGameAnalyse1.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. string targetScene;
  10. int gameType;
  11. long startTime; //毫秒
  12. float duration; //秒
  13. float sceneTimeOnStart;
  14. float uploadCountOnUpdate = 0;
  15. private static UserGameAnalyse1 _Instance;
  16. public static void CreateWhenGameStart(int gameType) {
  17. if (_Instance) return;
  18. UserGameAnalyse1 userGameAnalyse = new GameObject("UserGameAnaly1").AddComponent<UserGameAnalyse1>();
  19. userGameAnalyse.gameType = gameType;
  20. switch (gameType)
  21. {
  22. case 13:
  23. userGameAnalyse.targetScene = "DuckHunter";
  24. break;
  25. case 14:
  26. userGameAnalyse.targetScene = "WildAttack";
  27. break;
  28. }
  29. }
  30. void Awake()
  31. {
  32. _Instance = this;
  33. DontDestroyOnLoad(gameObject);
  34. startTime = JCUnityLib.TimeUtils.GetTimestamp();
  35. sceneTimeOnStart = Time.realtimeSinceStartup;
  36. }
  37. void OnDestroy() {
  38. if (_Instance == this) _Instance = null;
  39. UploadData();
  40. }
  41. void Update()
  42. {
  43. if (!IsTargetScene())
  44. {
  45. Destroy(gameObject);
  46. return;
  47. }
  48. duration = Time.realtimeSinceStartup - sceneTimeOnStart;
  49. if (duration / 60 >= uploadCountOnUpdate) //每60秒上传一次
  50. {
  51. uploadCountOnUpdate++;
  52. UploadData();
  53. }
  54. }
  55. public void UploadData()
  56. {
  57. int durationTime = (int) duration;
  58. if (UserPlayer.ins != null)
  59. {
  60. UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, durationTime);
  61. Debug.Log("上传" + targetScene + "(" + gameType + ")游戏时长统计数据:" + durationTime);
  62. }
  63. }
  64. bool IsTargetScene()
  65. {
  66. return SceneManager.GetActiveScene().name == targetScene;
  67. }
  68. }