示例#1
0
 private void UpdateEntryValue(string key, BlackboardVariable value)
 {
     Undo.RecordObject(bb, "Update blackboard entry");
     bb.UpdateValue(key, value);
     EditorUtility.SetDirty(bb);
     EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
 }
        private void CreateVariableAndResetInput()
        {
            // Validate field. Key "None" is not allowed.
            if (string.IsNullOrEmpty(newVariableKey) || newVariableKey.Equals("None"))
            {
                return;
            }
            string k = new string( newVariableKey.ToCharArray().Where(c => !Char.IsWhiteSpace(c)).ToArray());

            // Check for key duplicates
            for (int i = 0; i < blackboard.variables.Count; i++)
            {
                if (blackboard.variables[i].key == k)
                {
                    Debug.LogWarning("Variable '" + k + "' already exists.");
                    return;
                }
            }
            // Add variable
            Undo.RecordObject(blackboard, "Create Blackboard Variable");
            BlackboardVariable var = Undo.AddComponent(blackboard.gameObject, variableTypes[selectedVariableType]) as BlackboardVariable;

            var.hideFlags = HideFlags.HideInInspector;
            var.key       = k;
            blackboard.variables.Add(var);
            // Reset field
            newVariableKey = "";
            GUI.FocusControl("Clear");
        }
    private void RenameRow(string oldKey, string newKey)
    {
        BlackboardVariable value = GetValue(oldKey);

        RemoveRow(oldKey);
        AddRow(newKey, value);
    }
    private void LoadBlackboardState()
    {
        if (File.Exists(Application.persistentDataPath + "/SaveFile"))
        {
            BlackboardStateSave save;
            try
            {
                BinaryFormatter bf   = new BinaryFormatter();
                FileStream      file = File.Open(Application.persistentDataPath + "/SaveFile", FileMode.Open);
                save = (BlackboardStateSave)bf.Deserialize(file);
                file.Close();
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                Debug.LogError("You most likely need to delete the SaveFile file at '" +
                               Application.persistentDataPath + "', start/stop the game and look at the exception " +
                               "thrown on playmode exit");
                throw;
            }

            foreach (KeyValuePair <string, BVarSave> entry in save.savedEntries)
            {
                if (!blackboard.KeyExists(entry.Key))
                {
                    Debug.LogWarning($"Key {entry.Key} does not exist for the current blackboard. Skipping.");
                    continue;
                }

                BlackboardVariable finalValue = blackboard.GetValue(entry.Key);
                if (entry.Value.GetType() != finalValue.GetSaveType())
                {
                    Debug.LogWarning($"Value {entry.Value.GetType()} does not match the value type of current " +
                                     $"blackboard's {entry.Key} key, which is {finalValue.GetType()}. Skipping.");
                    continue;
                }

                if (finalValue.persistenceType == PersistenceType.SavedToFile)
                {
                    finalValue.LoadFrom(entry.Value);
                }
            }

            foreach (KeyValuePair <string, BlackboardVariable> entry in blackboard.AsList())
            {
                if (entry.Value != null && entry.Value.persistenceType != PersistenceType.SavedToFile)
                {
                    entry.Value.SnapshotState();
                }
            }
        }
        else
        {
            Debug.Log("No saved blackboard data found.");
        }
    }
    private void DrawListItems(Rect rect, int index, bool isActive, bool isFocused)
    {
        float spacing         = 2;
        float persistTogWidth = 20;
        float delBtnWidth     = 20;

        string             entryKey   = currentGroup[index];
        BlackboardVariable entryValue = bb.EditorGetDict()[entryKey];

        string textInKeyField = EditorGUI.DelayedTextField(
            new Rect(rect.x, rect.y, (rect.width - spacing - persistTogWidth - spacing - delBtnWidth) / 2,
                     EditorGUIUtility.singleLineHeight), entryKey);

        BlackboardVariable objectInValueField = (BlackboardVariable)EditorGUI.ObjectField(
            new Rect(rect.x + (rect.width - spacing - persistTogWidth - spacing - delBtnWidth) / 2 + spacing,
                     rect.y, (rect.width - spacing - persistTogWidth - spacing - delBtnWidth) / 2,
                     EditorGUIUtility.singleLineHeight), entryValue, typeof(BlackboardVariable), false);

        PersistenceType persistenceType = PersistenceType.NeverPersist;

        if (entryValue != null)
        {
            persistenceType = (PersistenceType)EditorGUI.EnumPopup(
                new Rect(rect.x + rect.width - persistTogWidth - delBtnWidth, rect.y, persistTogWidth,
                         EditorGUIUtility.singleLineHeight), entryValue.persistenceType);
        }

        bool deletebutton = GUI.Button(new Rect(rect.x + rect.width - delBtnWidth, rect.y, delBtnWidth,
                                                EditorGUIUtility.singleLineHeight), "X", deleteButtonStyle);

        if (entryKey != textInKeyField)
        {
            RenameEntry(entryKey, textInKeyField);
        }

        if (entryValue != objectInValueField)
        {
            UpdateEntryValue(entryKey, objectInValueField);
        }

        if (entryValue != null && entryValue.persistenceType != persistenceType)
        {
            Undo.RecordObject(entryValue, "Set blackboard variable's persistence type");
            entryValue.persistenceType = persistenceType;
            EditorUtility.SetDirty(entryValue);
        }

        if (deletebutton)
        {
            RemoveEntry(entryKey);
        }
    }
 private void UpdateEntryValue(string key, BlackboardVariable value)
 {
     Undo.RecordObject(bb, "Update blackboard entry");
     bb.UpdateValue(key, value);
     EditorUtility.SetDirty(bb);
 }
 private void DeleteVariabe(BlackboardVariable blackboardVariable)
 {
     Undo.RecordObject(blackboard, "Delete Blackboard Variable");
     blackboard.variables.Remove(blackboardVariable);
     Undo.DestroyObjectImmediate(blackboardVariable);
 }
 private void AddRow(string key, BlackboardVariable value)
 {
     blackboardEntries.Add(key, value);
 }
 public void UpdateValue(string key, BlackboardVariable value) => blackboardEntries[key] = value;
示例#10
0
    private void OnDoorToggle(BlackboardVariable data)
    {
        isOpen = ((BoolVariable)data).value;

        UpdateGoal();
    }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            EditorGUI.BeginChangeCheck();

            position.height = 18f;
            SerializedProperty keyProperty        = property.FindPropertyRelative("key");
            SerializedProperty blackboardProperty = property.FindPropertyRelative("blackboard");
            SerializedProperty useConstProperty   = property.FindPropertyRelative("useConstant");

            MonoBehaviour inspectedComponent = property.serializedObject.targetObject as MonoBehaviour;

            // search only in the same game object
            if (inspectedComponent != null)
            {
                // Blackboard blackboard = inspectedComponent.GetComponent<Blackboard>();
                Blackboard blackboard = GetBlackboardInParent(inspectedComponent);
                if (blackboard != null)
                {
                    // Draw mode toggle if not disabled
                    if (property.FindPropertyRelative("mode").enumValueIndex == 0)
                    {
                        Rect togglePosition = position;
                        togglePosition.width       = 8;
                        togglePosition.height      = 16;
                        useConstProperty.boolValue = EditorGUI.Toggle(togglePosition, useConstProperty.boolValue, constVarGUIStyle);
                        position.xMin += 10;
                    }

                    // Draw constant or dropdown
                    if (useConstProperty.boolValue)
                    {
                        // Use constant variable
                        EditorGUI.PropertyField(position, property.FindPropertyRelative("constantValue"), label);
                    }
                    else
                    {
                        System.Type          desiredVariableType = fieldInfo.FieldType.BaseType.GetGenericArguments()[0];
                        BlackboardVariable[] variables           = blackboard.GetAllVariables();
                        List <string>        keys = new List <string>();
                        keys.Add("None");
                        for (int i = 0; i < variables.Length; i++)
                        {
                            BlackboardVariable bv = variables[i];
                            if (bv.GetType() == desiredVariableType)
                            {
                                keys.Add(bv.key);
                            }
                        }
                        // Setup dropdown
                        // INFO: "None" can not be used as key
                        int selected = keys.IndexOf(keyProperty.stringValue);
                        if (selected < 0)
                        {
                            selected = 0;
                            // If key is not empty it means variable was deleted and missing
                            if (!System.String.IsNullOrEmpty(keyProperty.stringValue))
                            {
                                keys[0] = "Missing";
                            }
                        }
                        int result = EditorGUI.Popup(position, label.text, selected, keys.ToArray());
                        if (result > 0)
                        {
                            keyProperty.stringValue = keys[result];
                            blackboardProperty.objectReferenceValue = blackboard;
                        }
                        else
                        {
                            keyProperty.stringValue = "";
                            blackboardProperty.objectReferenceValue = null;
                        }
                    }
                }
                else
                {
                    EditorGUI.LabelField(position, property.displayName);
                    int indent = EditorGUI.indentLevel;
                    EditorGUI.indentLevel = 1;
                    position.y           += EditorGUI.GetPropertyHeight(keyProperty); // + EditorGUIUtility.standardVerticalSpacing;
                    EditorGUI.PropertyField(position, keyProperty);
                    position.y += EditorGUI.GetPropertyHeight(blackboardProperty);    // + EditorGUIUtility.standardVerticalSpacing;
                    EditorGUI.PropertyField(position, blackboardProperty);
                    EditorGUI.indentLevel = indent;
                }
            }


            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
            }

            EditorGUI.EndProperty();
        }