UserGameAnalyse1.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. case 15:
  29. userGameAnalyse.targetScene = "FruitMaster";
  30. break;
  31. }
  32. }
  33. void Awake()
  34. {
  35. _Instance = this;
  36. DontDestroyOnLoad(gameObject);
  37. startTime = JCUnityLib.TimeUtils.GetTimestamp();
  38. sceneTimeOnStart = Time.realtimeSinceStartup;
  39. }
  40. void OnDestroy() {
  41. if (_Instance == this) _Instance = null;
  42. UploadData();
  43. }
  44. void Update()
  45. {
  46. if (!IsTargetScene())
  47. {
  48. Destroy(gameObject);
  49. return;
  50. }
  51. duration = Time.realtimeSinceStartup - sceneTimeOnStart;
  52. if (duration / 60 >= uploadCountOnUpdate) //每60秒上传一次
  53. {
  54. uploadCountOnUpdate++;
  55. UploadData();
  56. }
  57. }
  58. public void UploadData()
  59. {
  60. int durationTime = (int) duration;
  61. if (UserPlayer.ins != null)
  62. {
  63. UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, durationTime);
  64. Debug.Log("上传" + targetScene + "(" + gameType + ")游戏时长统计数据:" + durationTime);
  65. }
  66. }
  67. bool IsTargetScene()
  68. {
  69. return SceneManager.GetActiveScene().name == targetScene;
  70. }
  71. }