UserGameAnalyse.cs 1.3 KB

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