| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- //打鸭子,塔防
- /* 用户游戏情况统计分析-主要向服务器是上传用户游戏数据 */
- public class UserGameAnalyse1 : MonoBehaviour
- {
- string targetScene;
- int gameType;
- long startTime; //毫秒
- float duration; //秒
- float sceneTimeOnStart;
- float uploadCountOnUpdate = 0;
- private static UserGameAnalyse1 _Instance;
- public static void CreateWhenGameStart(int gameType) {
- if (_Instance) return;
- UserGameAnalyse1 userGameAnalyse = new GameObject("UserGameAnaly1").AddComponent<UserGameAnalyse1>();
- userGameAnalyse.gameType = gameType;
- switch (gameType)
- {
- case 13:
- userGameAnalyse.targetScene = "DuckHunter";
- break;
- case 14:
- userGameAnalyse.targetScene = "WildAttack";
- break;
- }
- }
- void Awake()
- {
- _Instance = this;
- DontDestroyOnLoad(gameObject);
- startTime = JCUnityLib.TimeUtils.GetTimestamp();
- sceneTimeOnStart = Time.realtimeSinceStartup;
- }
- void OnDestroy() {
- if (_Instance == this) _Instance = null;
- UploadData();
- }
- void Update()
- {
- if (!IsTargetScene())
- {
- Destroy(gameObject);
- return;
- }
- duration = Time.realtimeSinceStartup - sceneTimeOnStart;
- if (duration / 60 >= uploadCountOnUpdate) //每60秒上传一次
- {
- uploadCountOnUpdate++;
- UploadData();
- }
- }
- public void UploadData()
- {
- int durationTime = (int) duration;
- if (UserPlayer.ins != null)
- {
- UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, durationTime);
- Debug.Log("上传" + targetScene + "(" + gameType + ")游戏时长统计数据:" + durationTime);
- }
- }
- bool IsTargetScene()
- {
- return SceneManager.GetActiveScene().name == targetScene;
- }
- }
|