Beispiel #1
0
        /// <summary>
        /// Creates a new preset and adds it to a new state in the list.
        /// </summary>
        public static Opsive.Shared.StateSystem.State[] CreatePreset(object target, Opsive.Shared.StateSystem.State[] states, ReorderableList reorderableList, string selectedIndexKey)
        {
            var preset = PersistablePreset.CreatePreset(target);

            if (preset != null)
            {
                var startName = target.GetType().Name + "Preset.asset";
                var path      = EditorPrefs.GetString(c_EditorPrefsLastPresetPathKey, Utility.InspectorUtility.GetSaveFilePath());
                path = EditorUtility.SaveFilePanel("Save Preset", path, startName, "asset");
                if (path.Length != 0 && Application.dataPath.Length < path.Length)
                {
                    EditorPrefs.SetString(c_EditorPrefsLastPresetPathKey, System.IO.Path.GetDirectoryName(path));
                    // The path is relative to the project.
                    path = string.Format("Assets/{0}", path.Substring(Application.dataPath.Length + 1));
                    // Do not delete/add if an existing preset already exists to prevent the references from being destroyed.
                    var existingPreset = AssetDatabase.LoadAssetAtPath <Preset>(path);
                    if (existingPreset != null)
                    {
                        EditorUtility.DisplayDialog("Unable to Save Preset", "The preset must reference a unique file name.", "Okay");
                        return(states);
                    }

                    var name = System.IO.Path.GetFileNameWithoutExtension(path);
                    if (!string.IsNullOrEmpty(name.Replace(target.GetType().Name + "Preset", "")))
                    {
                        name = name.Replace(target.GetType().Name + "Preset", "");
                    }

                    AssetDatabase.CreateAsset(preset, path);
                    AssetDatabase.ImportAsset(path);
                    EditorGUIUtility.PingObject(preset);
                    if (!Application.isPlaying)
                    {
                        states = InsertStateElement(states, reorderableList, selectedIndexKey, name, preset);
                    }
                }
            }
            return(states);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the states added to the specified object.
        /// </summary>
        /// <param name="obj">The object to get the states from/</param>
        /// <returns>The states added to the specified object.</returns>
        private void AddStatesFromObject(object obj, List <Opsive.Shared.StateSystem.State> stateList)
        {
            Opsive.Shared.StateSystem.State[] states = null;
            if (obj is StateBehavior)
            {
                states = (obj as StateBehavior).States;
            }
            else if (obj is StateObject)
            {
                states = (obj as StateObject).States;
            }
            if (states == null)
            {
                return;
            }
            if (m_CopyType == CopyType.All || m_CopyType == CopyType.NonDefault)
            {
                for (int i = 0; i < states.Length - 1; ++i)
                {
                    // Ignore any states that don't have a preset.
                    if (states[i].Preset == null)
                    {
                        continue;
                    }
                    stateList.Add(states[i]);
                }
            }

            if (m_CopyType == CopyType.All || m_CopyType == CopyType.Default)
            {
                // The default state has to be persisted.
                var preset = PersistablePreset.CreatePreset(obj, MemberVisibility.AllPublic);
                preset.name = obj.GetType().Name;
                AssetDatabase.AddObjectToAsset(preset, m_StateConfiguration);
                stateList.Add(new Opsive.Shared.StateSystem.State("Default" + preset.name, preset, null));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Inserts a new state element in the state array.
        /// </summary>
        private static Opsive.Shared.StateSystem.State[] InsertStateElement(Opsive.Shared.StateSystem.State[] states, ReorderableList reorderableList, string selectedIndexKey, string name, PersistablePreset preset)
        {
            // The name has to be unique to prevent it from interferring with other state names.
            if (!IsUniqueName(states, name))
            {
                var postfixIndex = 1;
                while (!IsUniqueName(states, name + " " + postfixIndex))
                {
                    postfixIndex++;
                }
                name += " " + postfixIndex;
            }

            // Create the element.
            var state = new Opsive.Shared.StateSystem.State(name, false);

            state.Preset = preset;
            var stateList = new List <Opsive.Shared.StateSystem.State>(states);

            stateList.Insert(0, state);
            reorderableList.displayRemove = stateList.Count > 1;

            // Select the new element.
            reorderableList.index = stateList.Count - 1;
            EditorPrefs.SetInt(selectedIndexKey, reorderableList.index);
            return(stateList.ToArray());
        }