예제 #1
0
        public PlayerPrefData(string key, T value, Action <T> OnValueChanged = null)
        {
            _key = key;

            //if : The following key is not used, we initialized the data type and their respective value
            if (!PlayerPrefs.HasKey(key))
            {
                //if : Valid DataType
                SetData(value);
#if UNITY_EDITOR
                if (PlayerPrefDataSettings.IsPlayerPrefEditorDataAlreadyInContainer(key) != -1)
                {
                    PlayerPrefDataSettings.EnlistPlayerPrefEditorDataInContainer(key, Convert.ChangeType(value, typeof(T)).ToString(), ref OnValueChanged);
                }
                else
                {
                    PlayerPrefDataSettings.EnlistPlayerPrefEditorDataInContainer(key, GetData().ToString(), ref OnValueChanged);
                }
#endif
            }
            else
            {
                if (AssigningDataType(value))
                {
                    InvokeOnValueChangedEvent(GetData());
                }
#if UNITY_EDITOR
                PlayerPrefDataSettings.EnlistPlayerPrefEditorDataInContainer(key, GetData().ToString(), ref OnValueChanged);
#endif
            }

            RegisterOnValueChangedEvent(OnValueChanged);
        }
예제 #2
0
        public void RegisterOnValueChangedEvent(Action <T> OnValueChanged)
        {
            if (OnValueChanged != null)
            {
                OnValueChangedEvent += OnValueChanged;
                InvokeOnValueChangedEvent(GetData());
#if UNITY_EDITOR
                PlayerPrefDataSettings.RegisterOnValueChangedEvent(_key, ref OnValueChanged);
#endif
            }
        }
예제 #3
0
        public static void ClearPlayerPref()
        {
            bool result = EditorUtility.DisplayDialog(
                "Clear 'PlayerPrefs' (Partial)",
                "Clear 'PlayerPrefs' that are only created by 'PlayerPrefsData/SavedData' using com.faith.core package.",
                "Clear", "Cancel");

            if (result)
            {
                PlayerPrefDataSettings.ResetAllPlayerPrefData();
            }
        }
예제 #4
0
        public void SetData(T value)
        {
            if (AssigningDataType(value))
            {
#if UNITY_EDITOR
                int index = PlayerPrefDataSettings.IsPlayerPrefEditorDataAlreadyInContainer(_key);
#endif
                switch (_dataType)
                {
                case CoreEnums.DataTypeForSavingData.DATA_TYPE_BOOL:

                    bool t_ParsedBoolValue = (bool)Convert.ChangeType(value, typeof(bool));
                    PlayerPrefs.SetInt(_key, t_ParsedBoolValue ? 1 : 0);
                    InvokeOnValueChangedEvent((T)Convert.ChangeType(value, typeof(bool)));

#if UNITY_EDITOR
                    if (index != -1)
                    {
                        PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData[index].InvokeEvent(t_ParsedBoolValue.ToString());
                    }
#endif

                    break;

                case CoreEnums.DataTypeForSavingData.DATA_TYPE_INT:

                    int t_ParsedIntValue = (int)Convert.ChangeType(value, typeof(int));
                    PlayerPrefs.SetInt(_key, t_ParsedIntValue);
                    InvokeOnValueChangedEvent((T)Convert.ChangeType(value, typeof(int)));

#if UNITY_EDITOR
                    if (index != -1)
                    {
                        PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData[index].InvokeEvent(t_ParsedIntValue.ToString());
                    }
#endif

                    break;

                case CoreEnums.DataTypeForSavingData.DATA_TYPE_FLOAT:

                    float t_ParsedFloatValue = (float)Convert.ChangeType(value, typeof(float));
                    PlayerPrefs.SetFloat(_key, t_ParsedFloatValue);
                    InvokeOnValueChangedEvent((T)Convert.ChangeType(value, typeof(float)));

#if UNITY_EDITOR
                    if (index != -1)
                    {
                        PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData[index].InvokeEvent(t_ParsedFloatValue.ToString());
                    }
#endif

                    break;

                case CoreEnums.DataTypeForSavingData.DATA_TYPE_DOUBLE:

                    double t_ParsedDoubleValue = (double)Convert.ChangeType(value, typeof(double));
                    PlayerPrefs.SetString(_key, t_ParsedDoubleValue.ToString());
                    InvokeOnValueChangedEvent((T)Convert.ChangeType(value, typeof(double)));

#if UNITY_EDITOR
                    if (index != -1)
                    {
                        PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData[index].InvokeEvent(t_ParsedDoubleValue.ToString());
                    }
#endif

                    break;

                case CoreEnums.DataTypeForSavingData.DATA_TYPE_STRING:

                    string t_ParsedStringValue = (string)Convert.ChangeType(value, typeof(string));
                    PlayerPrefs.SetString(_key, t_ParsedStringValue);
                    InvokeOnValueChangedEvent((T)Convert.ChangeType(value, typeof(string)));

#if UNITY_EDITOR
                    if (index != -1)
                    {
                        PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData[index].InvokeEvent(t_ParsedStringValue.ToString());
                    }
#endif

                    break;

                case CoreEnums.DataTypeForSavingData.DATA_TYPE_DATETIME:

                    DateTime t_ParsedDateTime = (DateTime)Convert.ChangeType(value, typeof(DateTime));
                    PlayerPrefs.SetString(_key, t_ParsedDateTime.ToString());
                    InvokeOnValueChangedEvent((T)Convert.ChangeType(value, typeof(DateTime)));

#if UNITY_EDITOR
                    if (index != -1)
                    {
                        PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData[index].InvokeEvent(t_ParsedDateTime.ToString());
                    }
#endif

                    break;
                }
            }
        }
        private void OnGUI()
        {
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            {
                m_SearchText = EditorGUILayout.TextField(
                    "Search",
                    m_SearchText
                    );
            }
            EditorGUILayout.EndHorizontal();


            m_ScrollPosition = EditorGUILayout.BeginScrollView(m_ScrollPosition, false, false);
            {
                //SettingMenu
                CoreEditorModule.DrawHorizontalLine();
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("PrefKeys (" + PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData.Count.ToString() + ")", EditorStyles.boldLabel);
                    EditorGUILayout.LabelField("DataType", EditorStyles.boldLabel, GUILayout.Width(widthForDataType));
                    EditorGUILayout.LabelField("Value", EditorStyles.boldLabel, GUILayout.Width(widthForValue));
                    EditorGUILayout.LabelField("SetValue", EditorStyles.boldLabel, GUILayout.Width(widthForValueChange));
                }
                EditorGUILayout.EndHorizontal();
                CoreEditorModule.DrawHorizontalLine();

                //Traversing List
                int t_Index = 1;
                foreach (PlayerPrefDataSettings.PlayerPrefEditorData t_PlayerPrefDataKey in PlayerPrefDataSettings.listOfUsedPlayerPrefEditorData)
                {
                    if (m_SearchText == null || (m_SearchText != null && (m_SearchText.Length == 0 || (m_SearchText.Length > 0 && t_PlayerPrefDataKey.key.Contains(m_SearchText)))))
                    {
                        EditorGUILayout.BeginHorizontal();
                        {
                            EditorGUILayout.LabelField(t_Index + " : " + t_PlayerPrefDataKey.key);

                            if (t_PlayerPrefDataKey.type == typeof(bool))
                            {
                                EditorGUILayout.LabelField("BOOLEAN", GUILayout.Width(widthForDataType));
                                EditorGUILayout.LabelField(t_PlayerPrefDataKey.value, GUILayout.Width(widthForValue));

                                bool t_BoolValue = (bool)System.Convert.ChangeType(t_PlayerPrefDataKey.value, typeof(bool));
                                EditorGUI.BeginChangeCheck();
                                {
                                    t_BoolValue = EditorGUILayout.Toggle(
                                        "",
                                        t_BoolValue,
                                        GUILayout.Width(widthForValueChange)
                                        );
                                }
                                if (EditorGUI.EndChangeCheck())
                                {
                                    PlayerPrefDataSettings.SetData <bool>(t_PlayerPrefDataKey.key, t_BoolValue.ToString());
                                }
                            }
                            else if (t_PlayerPrefDataKey.type == typeof(int))
                            {
                                EditorGUILayout.LabelField("INTEGER", GUILayout.Width(widthForDataType));
                                EditorGUILayout.LabelField(t_PlayerPrefDataKey.value, GUILayout.Width(widthForValue));

                                int t_IntValue = (int)System.Convert.ChangeType(t_PlayerPrefDataKey.value, typeof(int));

                                EditorGUI.BeginChangeCheck();
                                {
                                    t_IntValue = EditorGUILayout.IntField(
                                        "",
                                        t_IntValue,
                                        GUILayout.Width(widthForValueChange)
                                        );
                                }
                                if (EditorGUI.EndChangeCheck())
                                {
                                    PlayerPrefDataSettings.SetData <int>(t_PlayerPrefDataKey.key, t_IntValue.ToString());
                                }
                            }
                            else if (t_PlayerPrefDataKey.type == typeof(float))
                            {
                                EditorGUILayout.LabelField("FLOAT", GUILayout.Width(widthForDataType));
                                EditorGUILayout.LabelField(t_PlayerPrefDataKey.value, GUILayout.Width(widthForValue));

                                float t_FloatValue = (float)System.Convert.ChangeType(t_PlayerPrefDataKey.value, typeof(float));

                                EditorGUI.BeginChangeCheck();
                                {
                                    t_FloatValue = EditorGUILayout.FloatField(
                                        "",
                                        t_FloatValue,
                                        GUILayout.Width(widthForValueChange)
                                        );
                                }
                                if (EditorGUI.EndChangeCheck())
                                {
                                    PlayerPrefDataSettings.SetData <float>(t_PlayerPrefDataKey.key, t_FloatValue.ToString());
                                }
                            }
                            else if (t_PlayerPrefDataKey.type == typeof(double))
                            {
                                EditorGUILayout.LabelField("DOUBLE", GUILayout.Width(widthForDataType));
                                EditorGUILayout.LabelField(t_PlayerPrefDataKey.value, GUILayout.Width(widthForValue));

                                double t_DoubleValue = (double)System.Convert.ChangeType(t_PlayerPrefDataKey.value, typeof(double));

                                EditorGUI.BeginChangeCheck();
                                {
                                    t_DoubleValue = EditorGUILayout.DoubleField(
                                        "",
                                        t_DoubleValue,
                                        GUILayout.Width(widthForValueChange)
                                        );
                                }
                                if (EditorGUI.EndChangeCheck())
                                {
                                    PlayerPrefDataSettings.SetData <double>(t_PlayerPrefDataKey.key, t_DoubleValue.ToString());
                                }
                            }
                            else if (t_PlayerPrefDataKey.type == typeof(string))
                            {
                                EditorGUILayout.LabelField("STRING", GUILayout.Width(widthForDataType));
                                EditorGUILayout.LabelField(t_PlayerPrefDataKey.value, GUILayout.Width(widthForValue));

                                string t_StringValue = (string)System.Convert.ChangeType(t_PlayerPrefDataKey.value, typeof(string));

                                EditorGUI.BeginChangeCheck();
                                {
                                    t_StringValue = EditorGUILayout.TextField(
                                        "",
                                        t_StringValue,
                                        GUILayout.Width(widthForValueChange)
                                        );
                                }
                                if (EditorGUI.EndChangeCheck())
                                {
                                    PlayerPrefDataSettings.SetData <string>(t_PlayerPrefDataKey.key, t_StringValue.ToString());
                                }
                            }
                            else if (t_PlayerPrefDataKey.type == typeof(System.DateTime))
                            {
                                EditorGUILayout.LabelField("DATE_TIME", GUILayout.Width(widthForDataType));
                                EditorGUILayout.LabelField(t_PlayerPrefDataKey.value, GUILayout.Width(widthForValue));

                                System.DateTime dateTimeValue = (System.DateTime)System.Convert.ChangeType(t_PlayerPrefDataKey.value, typeof(System.DateTime));

                                EditorGUI.BeginChangeCheck();
                                {
                                    GUI.enabled = false;
                                    EditorGUILayout.TextField(
                                        "",
                                        dateTimeValue.ToString(),
                                        GUILayout.Width(widthForValueChange)
                                        );
                                    GUI.enabled = true;
                                }
                                if (EditorGUI.EndChangeCheck())
                                {
                                    PlayerPrefDataSettings.SetData <System.DateTime>(t_PlayerPrefDataKey.key, dateTimeValue.ToString());
                                }
                            }
                        }
                        EditorGUILayout.EndHorizontal();
                    }

                    t_Index++;
                }
            }
            EditorGUILayout.EndScrollView();
        }