I have a few ideas how to capture play time but the main question is how do you capture if people switch between scenes? I need a object that will carry over between scenes. How do I make a object that carries over across scenes?
ULTRA-simple static solution to a GameManager: https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068 https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b OR for a more-complex "lives as a MonoBehaviour" solution. This is the pattern I use in almost all my games: Simple Singleton (UnitySingleton): Some super-simple Singleton examples to take and modify: Simple Unity3D Singleton (no predefined data): https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30 Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data: https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625 These are pure-code solutions, do not put anything into any scene, just access it via .Instance! If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like: Code (csharp): public void DestroyThyself() { Destroy(gameObject); Instance = null; // because destroy doesn't happen until end of frame }
While @Kurt-Dekker gives a more thorough answer for what you're trying to achieve, the most direct answer to this question is to call DontDestroyOnLoad on a script attached to a GameObject at the root of the scene. The DontDestroyOnLoad object you will see is then moved to its own fake DontDestroyOnLoad scene in the scene hierarchy, where it survives any scene load or unload without being destroyed automatically. https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
You can but it's not really defined how much you can do after receiving a quit message... some platforms you can do a lot, others throw you out of the pool of running processes fairly rapidly, so it is best to keep it brief and to the point, perhaps write some values to disk, that's about it.
I think I got it. Here is my code in case its needed: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Core_Analytics_Manager : MonoBehaviour { public static Core_Analytics_Manager _Analytics_Manager; public static bool _Playing_Product; private float PlayTime = 0f; private decimal HoursPlayed = 0; void Awake() { if(_Analytics_Manager == null) { DontDestroyOnLoad(gameObject); _Analytics_Manager = this; } else if(_Analytics_Manager != this) { Destroy(gameObject); } } private void Update() { //3600 seconds in 1 hour if (_Playing_Product) { PlayTime += Time.deltaTime; HoursPlayed = (Mathf.RoundToInt(PlayTime)/3600); } } static void Quit() { _Playing_Product = false; //Save Play Time } }
I bet you're trying it in the editor. There is no Quit in the editor. https://docs.unity3d.com/ScriptReference/Application.Quit.html If you want to test in the editor, you need to use the https://docs.unity3d.com/ScriptReference/EditorApplication-playModeStateChanged.html Also read the quitting's documentation page carefully, you don't have quitting on some platforms or you only have limited functionality.
I built the game and tested it. that's how I knew it was not working. It never hit my API. So I tried the void OnApplicationQuit() and that worked.