Conditions

Checked with version: 5.5

-

Difficulty: Intermediate

Phase 3 teaches you how to build an interaction system, focusing on conditions. We will create a system to check the current game state, discuss scripting patterns and use scriptable objects, generic classes, inheritance and extension methods. Download the asset package for Phase 3 here.

Conditions

Intermediate Adventure Game Tutorial

Download the asset package for Phase 3 at the Unity Asset Store here.

ConditionCollection

Code snippet

using UnityEngine;
public class ConditionCollection : ScriptableObject
{
    public string description;
    public Condition[] requiredConditions = new Condition[0];
    public ReactionCollection reactionCollection;
    public bool CheckAndReact()
    {
        for (int i = 0; i < requiredConditions.Length; i++)
        {
            if (!AllConditions.CheckCondition (requiredConditions[i]))
                return false;
        }
        if(reactionCollection)
            reactionCollection.React();
        return true;
    }
}

ConditionCollectionEditor

Code snippet

using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ConditionCollection))]
public class ConditionCollectionEditor : EditorWithSubEditors<ConditionEditor, Condition>
{
    public SerializedProperty collectionsProperty;
    private ConditionCollection conditionCollection;
    private SerializedProperty descriptionProperty;
    private SerializedProperty conditionsProperty;
    private SerializedProperty reactionCollectionProperty;
    private const float conditionButtonWidth = 30f;
    private const float collectionButtonWidth = 125f;
    private const string conditionCollectionPropDescriptionName = "description";
    private const string conditionCollectionPropRequiredConditionsName = "requiredConditions";
    private const string conditionCollectionPropReactionCollectionName = "reactionCollection";
    private void OnEnable ()
    {
        conditionCollection = (ConditionCollection)target;
        if (target == null)
        {
            DestroyImmediate (this);
            return;
        }
        descriptionProperty = serializedObject.FindProperty(conditionCollectionPropDescriptionName);
        conditionsProperty = serializedObject.FindProperty(conditionCollectionPropRequiredConditionsName);
        reactionCollectionProperty = serializedObject.FindProperty(conditionCollectionPropReactionCollectionName);
        CheckAndCreateSubEditors (conditionCollection.requiredConditions);
    }
    private void OnDisable ()
    {
        CleanupEditors ();
    }
    protected override void SubEditorSetup (ConditionEditor editor)
    {
        editor.editorType = ConditionEditor.EditorType.ConditionCollection;
        editor.conditionsProperty = conditionsProperty;
    }
    public override void OnInspectorGUI ()
    {
        serializedObject.Update ();
        CheckAndCreateSubEditors(conditionCollection.requiredConditions);
        
        EditorGUILayout.BeginVertical(GUI.skin.box);
        EditorGUI.indentLevel++;
        EditorGUILayout.BeginHorizontal();
        descriptionProperty.isExpanded = EditorGUILayout.Foldout(descriptionProperty.isExpanded, descriptionProperty.stringValue);
        if (GUILayout.Button("Remove Collection", GUILayout.Width(collectionButtonWidth)))
        {
            collectionsProperty.RemoveFromObjectArray (conditionCollection);
        }
        EditorGUILayout.EndHorizontal();
        
        if (descriptionProperty.isExpanded)
        {
            ExpandedGUI ();
        }
        
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();
        serializedObject.ApplyModifiedProperties();
    }
    private void ExpandedGUI ()
    {
        EditorGUILayout.Space();
        EditorGUILayout.PropertyField(descriptionProperty);
        EditorGUILayout.Space();
        float space = EditorGUIUtility.currentViewWidth / 3f;
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Condition", GUILayout.Width(space));
        EditorGUILayout.LabelField("Satisfied?", GUILayout.Width(space));
        EditorGUILayout.LabelField("Add/Remove", GUILayout.Width(space));
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginVertical(GUI.skin.box);
        for (int i = 0; i < subEditors.Length; i++)
        {
            subEditors[i].OnInspectorGUI();
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace ();
        if (GUILayout.Button("+", GUILayout.Width(conditionButtonWidth)))
        {
            Condition newCondition = ConditionEditor.CreateCondition();
            conditionsProperty.AddToObjectArray(newCondition);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();
        EditorGUILayout.PropertyField(reactionCollectionProperty);
    }
    public static ConditionCollection CreateConditionCollection()
    {
        ConditionCollection newConditionCollection = CreateInstance<ConditionCollection>();
        newConditionCollection.description = "New condition collection";
        newConditionCollection.requiredConditions = new Condition[1];
        newConditionCollection.requiredConditions[0] = ConditionEditor.CreateCondition();
        return newConditionCollection;
    }
}