/// <summary>
        /// Gets the existing world variable value. This should only be used in startup code. Otherwise grab the variable from GetWorldVariable
        /// </summary>
        /// <returns>
        /// The existing World Variable value in PlayerPrefs.
        /// </returns>
        /// <param name='variableName'>World Variable name.</param>
        /// <param name="startingValue">The value to use if not present.</param>
        public static float?GetExistingWorldVariableFloatValue(string variableName, float startingValue)
        {
            var tokenKey = InGameWorldVariable.GetTokenPrefsKey(variableName);

            // save this if we need it later.
            if (!VariableExistsInScene(variableName))
            {
                return(null);
            }

            if (!PlayerPrefs.HasKey(tokenKey))
            {
                // if this is the first time, set it!
                PlayerPrefs.SetFloat(tokenKey, startingValue);
            }

            return(PlayerPrefs.GetFloat(tokenKey));
        }
        private static void Init()
        {
            if (_inGamePlayerStats != null)
            {
                return;
            }

            _doneInitializing = false;

            _inGamePlayerStats = new Dictionary <string, InGameWorldVariable>(StringComparer.OrdinalIgnoreCase);

            if (TrackerTransform == null)
            {
                return;
            }

            // set up variables for use
            for (var i = 0; i < TrackerTransform.childCount; i++)
            {
                var oTrans = TrackerTransform.GetChild(i);
                var oStat  = oTrans.GetComponent <WorldVariable>();

                if (oStat == null)
                {
                    LevelSettings.LogIfNew("Transform '" + oTrans.name +
                                           "' under WorldVariables does not contain a WorldVariable script. Please fix this.");
                    continue;
                }

                if (_inGamePlayerStats.ContainsKey(oStat.name))
                {
                    LevelSettings.LogIfNew("You have more than one World Variable named '" + oStat.name +
                                           "' in this Scene. Please make sure the names are unique.");
                    continue;
                }

                var newStatTracker = new InGameWorldVariable(oStat, oStat.name, oStat.varType);

                if (Application.isPlaying)
                {
                    // do not update values when we're in edit mode!
                    switch (oStat.persistanceMode)
                    {
                    case WorldVariable.StatPersistanceMode.ResetToStartingValue:
                        switch (oStat.varType)
                        {
                        case VariableType._integer:
                            newStatTracker.CurrentIntValue = oStat.startingValue;
                            break;

                        case VariableType._float:
                            newStatTracker.CurrentFloatValue = oStat.startingValueFloat;
                            break;
                        }
                        break;

                    case WorldVariable.StatPersistanceMode.KeepFromPrevious:
                        // set to value in player prefs
                        break;
                    }

                    if (oStat.listenerPrefab != null)
                    {
                        var variable = GetExistingWorldVariableIntValue(oStat.name, oStat.startingValue);
                        if (variable != null)
                        {
                            oStat.listenerPrefab.UpdateValue(variable.Value, variable.Value);
                        }
                    }
                }

                _inGamePlayerStats.Add(oStat.name, newStatTracker);
            }

            _doneInitializing = true;
        }
Exemplo n.º 3
0
        private static void Init()
        {
            if (_inGamePlayerStats != null) {
                return;
            }

            _doneInitializing = false;

            _inGamePlayerStats = new Dictionary<string, InGameWorldVariable>();

            if (TrackerTransform == null) {
                return;
            }

            // set up variables for use
            for (var i = 0; i < TrackerTransform.childCount; i++) {
                var oTrans = TrackerTransform.GetChild(i);
                var oStat = oTrans.GetComponent<WorldVariable>();

                if (oStat == null) {
                    LevelSettings.LogIfNew("Transform '" + oTrans.name +
                                           "' under WorldVariables does not contain a WorldVariable script. Please fix this.");
                    continue;
                }

                if (_inGamePlayerStats.ContainsKey(oStat.name)) {
                    LevelSettings.LogIfNew("You have more than one World Variable named '" + oStat.name +
                                           "' in this Scene. Please make sure the names are unique.");
                    continue;
                }

                var newStatTracker = new InGameWorldVariable(oStat, oStat.name, oStat.varType);

                if (Application.isPlaying) {
                    // do not update values when we're in edit mode!
                    switch (oStat.persistanceMode) {
                        case WorldVariable.StatPersistanceMode.ResetToStartingValue:
                            switch (oStat.varType) {
                                case VariableType._integer:
                                    newStatTracker.CurrentIntValue = oStat.startingValue;
                                    break;
                                case VariableType._float:
                                    newStatTracker.CurrentFloatValue = oStat.startingValueFloat;
                                    break;
                            }
                            break;
                        case WorldVariable.StatPersistanceMode.KeepFromPrevious:
                            // set to value in player prefs
                            break;
                    }

                    if (oStat.listenerPrefab != null) {
                        var variable = GetExistingWorldVariableIntValue(oStat.name, oStat.startingValue);
                        if (variable != null) {
                            oStat.listenerPrefab.UpdateValue(variable.Value, variable.Value, true);
                        }
                    }
                }

                _inGamePlayerStats.Add(oStat.name, newStatTracker);
            }

            _doneInitializing = true;
        }