Skip to content
Snippets Groups Projects
PulseEngineDriverEditor.cs 3.11 KiB
Newer Older
/* Distributed under the Apache License, Version 2.0.
   See accompanying NOTICE file for details.*/

using UnityEngine;
using UnityEditor;
using System.IO;

[CustomEditor(typeof(PulseEngineDriver), true)]
public class PulseEngineDriverEditor : Editor
{
    SerializedProperty stateFileProperty;   // serialized initial state file
    SerializedProperty timeStepProperty;
    SerializedProperty formatProperty;

    void OnEnable()
    {
        stateFileProperty = serializedObject.FindProperty("initialStateFile");
        timeStepProperty = serializedObject.FindProperty("timeStepProperty");
        formatProperty = serializedObject.FindProperty("serializationFormat");
    }

    public override void OnInspectorGUI()
    {
        // Ensure serialized properties are up to date with component
        serializedObject.Update();

        var driver = target as PulseEngineDriver;
        string path = Application.streamingAssetsPath + driver.engineDataPath;
        DirectoryInfo directoryInfo = new DirectoryInfo(path);
        DirectoryInfo[] pulseDirectories = directoryInfo.GetDirectories();
        bool ecg = false,
        environments = false,
        nutrition = false,
        substances = false;
        foreach (DirectoryInfo dir in pulseDirectories)
        {
            if (dir.Name.Equals("ecg"))
                ecg = true;
            else if (dir.Name.Equals("environments"))
                environments = true;
            else if (dir.Name.Equals("nutrition"))
                nutrition = true;
            else if (dir.Name.Equals("substances"))
                substances = true;
        }
        bool success = ecg && environments && nutrition && substances;
        if (!success)
        {
            string message = "Missing data directories for Pulse expected in " +
                path +
                ".\nCopy the 'PulseDataFiles' directory from this repository " +
                "inside the top level 'StreamingAssets' directory of your Unity project.";

            EditorGUILayout.HelpBox(message, MessageType.Warning);

            stateFileProperty.objectReferenceValue = null;
            serializedObject.ApplyModifiedProperties();
            return;
        }

        // Draw UI to select initial state file
        EditorGUILayout.PropertyField(stateFileProperty,
                                      new GUIContent("Initial State File"));
        var state = stateFileProperty.objectReferenceValue as TextAsset;
        if (state == null)
        {
            string message = "A state file is required to initialize the " +
                "Pulse engine. You can find an example file in 'Data/states' " +
                "or generate one by running the Pulse engine and save a " +
                "state to file (Unity will only accept '.txt' files).";
            EditorGUILayout.HelpBox(message, MessageType.Warning);
            return;
        }

        // Show the default inspector property editor without the script field
        DrawPropertiesExcluding(serializedObject, "m_Script", "initialStateFile");

        // Apply modifications back to the component
        serializedObject.ApplyModifiedProperties();
    }
}