using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; //打鸭子,塔防 /* 用户游戏情况统计分析-主要向服务器是上传用户游戏数据 */ public class UserGameAnalyse1 : MonoBehaviour { //重新玩操作,记录对应的数据信息。叠加时间和射箭次数 public static float allTimeSecond = 0; public static int allShootingCount = 0; string targetScene; int gameType; long startTime; //毫秒 float duration; //秒 float sceneTimeOnStart; float uploadCountOnUpdate = 0; bool gameIsEnd = false; public int shootingCount = 0; private static UserGameAnalyse1 _Instance; public static UserGameAnalyse1 Instance { get { return _Instance; } } public static UserGameAnalyse1 CreateWhenGameStartAndReturn(int gameType) { //if (_Instance) //{ // _Instance.shootingCount = 0; // _Instance.duration = 0; // _Instance.gameIsEnd = false; // return _Instance; //}; UserGameAnalyse1 userGameAnalyse = new GameObject("UserGameAnaly1").AddComponent(); userGameAnalyse.gameType = gameType; switch (gameType) { case 13: userGameAnalyse.targetScene = "DuckHunter"; break; case 14: userGameAnalyse.targetScene = "WildAttack"; break; case 15: userGameAnalyse.targetScene = "FruitMaster"; break; case 16: userGameAnalyse.targetScene = "MovingTarget"; break; } return userGameAnalyse; } void Awake() { _Instance = this; //DontDestroyOnLoad(gameObject); startTime = JCUnityLib.TimeUtils.GetTimestamp(); sceneTimeOnStart = Time.realtimeSinceStartup; } void OnDestroy() { if (_Instance == this) _Instance = null; UploadData(true); } void Update() { //if (!IsTargetScene()) //{ // Destroy(gameObject); // return; //} if (gameIsEnd) return; duration = Time.realtimeSinceStartup - sceneTimeOnStart; if (duration / 60 >= uploadCountOnUpdate) //每60秒上传一次 { uploadCountOnUpdate++; UploadData(false); } } public void UploadData(bool isEnd) { if (gameIsEnd) return; if (isEnd) { gameIsEnd = true; //本轮游戏结束时候,记录一次数据 allTimeSecond += duration; } int durationTime = (int)duration; if (UserPlayer.ins != null) { UserPlayer.ins.call("UserGameAnalyseComp.uploadUserGameRecord", gameType, startTime, durationTime); //Debug.Log("上传" + targetScene + "(" + gameType + ")游戏时长统计数据:" + durationTime + ",allTimeSecond:" + allTimeSecond); } } bool IsTargetScene() { return SceneManager.GetActiveScene().name == targetScene; } public int changeShootingCount(int value = 1) { shootingCount += value; allShootingCount += value; return shootingCount; } public void showResultView(Action callback) { //不端直接返回,不处理弹出框 if (CommonConfig.StandaloneModeOrPlatformB) { UploadData(true); onResetOverlayData(); callback?.Invoke(); return; } UploadData(true); //Debug.Log("ShowResultView" + targetScene + "(" + gameType + ") allTimeSecond:" + allTimeSecond + ",startTime:"+ startTime + ",duration:" + duration); StandaloneAPI.PauseGame(); GameObject resultObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_GameResultView); GameInfo gameInfo = TextureMgr.ins.GetGameInfos(gameType); if (gameInfo == null) Debug.LogError("缺少GameInfo信息"); GameResultView gameResultView = resultObj.GetComponent(); gameResultView.OnBackClicked += callback; gameResultView.setGameResultInfo(gameInfo.textId, (int)allTimeSecond, allShootingCount); onResetOverlayData(); } public void onResetOverlayData() { allTimeSecond = 0; allShootingCount = 0; } }