public void ResetToDefaults()
        {
            UndoHandler.RegisterUndoableAction(this, "Reset To Defaults");

                        #if UNITY_EDITOR          // in UnityEditor use Presets or EditorJsonUtility
                        #if UNITY_2018_1_OR_NEWER // Presets were introduced in Unity 2018.1
                        #if UNITY_2019_3_OR_NEWER // GetDefaultForObject became obsolete in Unity 2019.3
            var presets = Preset.GetDefaultPresetsForObject(this);
            var preset  = presets.Length > 0 ? presets[0] : null;
                        #else
            var preset = Preset.GetDefaultForObject(this);
                        #endif

            // if no default preset has been assigned for preferences asset, then try finding a preset
            // in the same directory with the preferences asset
            if (preset == null)
            {
                var preferencesPath = AssetDatabase.GetAssetPath(this);
                var directoryPath   = FileUtility.GetParentDirectory(preferencesPath);
                var updateGuids     = AssetDatabase.FindAssets("t:Preset", ArrayExtensions.TempStringArray(directoryPath));
                for (int n = updateGuids.Length - 1; n >= 0; n--)
                {
                    var path = AssetDatabase.GUIDToAssetPath(updateGuids[n]);
                    preset = AssetDatabase.LoadAssetAtPath <Preset>(path);
                    if (!string.Equals(preset.GetTargetFullTypeName(), typeof(InspectorPreferences).FullName, StringComparison.OrdinalIgnoreCase))
                    {
                        preset = null;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (preset != null)
            {
                preset.ApplyTo(this);
            }
            else
                        #endif
            {
                var freshInstance = CreateInstance <InspectorPreferences>();
                var jsonString    = EditorJsonUtility.ToJson(freshInstance);
                Platform.Active.Destroy(freshInstance);
                EditorJsonUtility.FromJsonOverwrite(jsonString, this);
            }
                        #else
            // at runtime use JsonUtility to reset values to those of a freshly created instance
            var freshInstance = CreateInstance <InspectorPreferences>();
            var jsonString    = JsonUtility.ToJson(freshInstance);
            Platform.Active.Destroy(freshInstance);
            JsonUtility.FromJsonOverwrite(jsonString, this);
                        #endif

            setupDone         = false;
            isFirstOnValidate = true;

            if (Event.current != null)
            {
                Setup();
            }

            Platform.Active.SetDirty(this);

            if (onSettingsChanged != null)
            {
                onSettingsChanged(this);
            }
        }
Esempio n. 2
0
        public static void RemoveAt([NotNull] ref ICollection collection, int index, bool throwExceptionIfFails)
        {
            int count;

            try
            {
                count = collection.Count;
            }
            catch (Exception)
            {
                if (throwExceptionIfFails)
                {
                    throw;
                }
                return;
            }

            if (count <= index)
            {
                if (throwExceptionIfFails)
                {
                    throw new IndexOutOfRangeException("Index " + index + " was >= than collection.Count " + count);
                }
                return;
            }

            var array = collection as Array;

            if (array != null)
            {
                try
                {
                    ArrayExtensions.RemoveAt(ref array, index);
                }
                catch (Exception)
                {
                    if (throwExceptionIfFails)
                    {
                        throw;
                    }
                }
                collection = array;
                return;
            }

            var list = collection as IList;

            if (list != null)
            {
                try
                {
                    list.RemoveAt(index);
                }
                catch (Exception)
                {
                    if (throwExceptionIfFails)
                    {
                        throw;
                    }
                }
                return;
            }

            var dictionary = collection as IDictionary;

            if (dictionary != null)
            {
                dictionary.RemoveAt(index);
                return;
            }

            var removeAtMethod = collection.GetType().GetMethod("RemoveAt");

            if (removeAtMethod != null)
            {
                var parameters = removeAtMethod.GetParameters();
                if (parameters.Length == 1)
                {
                    var parameter = parameters[0];
                    if (parameter.ParameterType == Types.Int)
                    {
                        if (removeAtMethod.ReturnType == null || !typeof(ICollection).IsAssignableFrom(removeAtMethod.ReturnType))
                        //if(removeAtMethod.ReturnType == Types.Void || removeAtMethod.ReturnType == Types.Bool)
                        {
                            try
                            {
                                removeAtMethod.Invoke(collection, ArrayExtensions.TempObjectArray(index));
                            }
                            catch (Exception)
                            {
                                if (throwExceptionIfFails)
                                {
                                    throw;
                                }
                            }
                        }
                        else                         //if(typeof(ICollection).IsAssignableFrom(removeAtMethod.ReturnType))
                        {
                            try
                            {
                                collection = (ICollection)removeAtMethod.Invoke(collection, ArrayExtensions.TempObjectArray(index));
                            }
                            catch (Exception)
                            {
                                if (throwExceptionIfFails)
                                {
                                    throw;
                                }
                            }
                        }
                        return;
                    }
                }
            }

            if (throwExceptionIfFails)
            {
                throw new InvalidOperationException("Unable to remove item at index " + index + " from collection of type " + collection.GetType().Name + ".");
            }
            return;
        }
Esempio n. 3
0
 /// <inheritdoc />
 protected override void RemoveAt(ref Array collection, int index)
 {
     ArrayExtensions.RemoveAt(ref collection, index);
 }
        /// <summary>
        /// Gets all updates for InspectorPreferences.
        /// Update files should be located in the same directory as the PreferencesAsset inside
        /// a folder named "Updates"
        /// </summary>
        /// <param name="preferences"></param>
        /// <returns></returns>
        public static InspectorPreferencesUpdate[] GetAllUpdates([NotNull] InspectorPreferences preferences)
        {
            var preferencesPath = AssetDatabase.GetAssetPath(preferences);
            var directoryPath   = FileUtility.GetParentDirectory(preferencesPath);

            directoryPath = FileUtility.GetChildDirectory(directoryPath, "Updates");

            if (!AssetDatabase.IsValidFolder(directoryPath))
            {
                                #if DEV_MODE
                Debug.LogWarning(preferences.name + " had no Updates folder next to it.");
                                #endif
                return(ArrayPool <InspectorPreferencesUpdate> .ZeroSizeArray);
            }

            var updateGuids = AssetDatabase.FindAssets("t:InspectorPreferencesUpdate", ArrayExtensions.TempStringArray(directoryPath));
            int count       = updateGuids.Length;
            var results     = new InspectorPreferencesUpdate[count];

            for (int n = updateGuids.Length - 1; n >= 0; n--)
            {
                var updatePath = AssetDatabase.GUIDToAssetPath(updateGuids[n]);
                results[n] = AssetDatabase.LoadAssetAtPath <InspectorPreferencesUpdate>(updatePath);
            }

            return(results);
        }