コード例 #1
0
ファイル: TotalAISetupEditor.cs プロジェクト: ming81/TotalAI
        private void CreateCoreScriptableObjects()
        {
            TotalAIManager totalAIManager = FindObjectOfType<TotalAIManager>();
            if (totalAIManager == null)
            {
                Debug.LogError("Trying to create core ScriptableObjects but there is no TotalAIManager.");
                return;
            }
            TotalAISettings settings = totalAIManager.settings;

            if (!Directory.Exists(defaultSOFolderRoot))
            {
                Debug.LogError("Trying to create core ScriptableObjects but root directory is missing: " + defaultSOFolderRoot);
                return;
            }
            if (!defaultSOFolderRoot.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                defaultSOFolderRoot += Path.DirectorySeparatorChar;
            }

            InputConditionType currentActionTypeICT = null;
            AttributeType movementSpeedAT = null;
            AttributeType detectionRadiusAT = null;
            Sphere3DST sphere3DST = null;
            Circle2DST circle2DST = null;
            UtilityAIPT utilityAIPT = null;
            GoToBT goToBT = null;
            foreach (KeyValuePair<string, string[]> DirToSOType in coreSOTypesToCreate)
            {
                string directory = DirToSOType.Key;
                string[] typeNames = DirToSOType.Value;
                int inventorySlotNum = 0;
                int attributeTypeNum = 0;
                foreach (string typeName in typeNames)
                {
                    if (directory == "InventoryTypes" || directory == "MovementTypes" || directory == "SensorTypes")
                    {
                        if (typeName.Contains("2D") && !settings.for2D)
                            continue;
                        else if (typeName.Contains("3D") && settings.for2D)
                            continue;
                    }

                    string fullDirectoryPath = defaultSOFolderRoot + directory;
                    if (!Directory.Exists(fullDirectoryPath))
                    {
                        try
                        {
                            Directory.CreateDirectory(fullDirectoryPath);
                            AssetDatabase.Refresh();
                        }
                        catch (Exception e)
                        {
                            Debug.LogError("Trying to create directory for Core SO " + typeName + " at " + fullDirectoryPath + " - Error = " + e);
                            return;
                        }
                    }

                    string assetName = typeName;
                    if (typeName == "InventorySlot")
                    {
                        assetName = inventorySlotNames[inventorySlotNum];
                        ++inventorySlotNum;
                    }
                    else if (typeName == "MinMaxFloatAT")
                    {
                        assetName = attributeTypeNames[attributeTypeNum];
                        ++attributeTypeNum;
                    }
                    else if (typeName == "TypeCategory")
                    {
                        assetName = "All";
                    }

                    string fullPath = fullDirectoryPath + Path.DirectorySeparatorChar + assetName + ".asset";

                    // See if this Asset already exists
                    if (AssetDatabase.AssetPathToGUID(fullPath) != null && AssetDatabase.AssetPathToGUID(fullPath) != "" &&
                        AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(fullPath) != null)
                    {
                        Debug.Log("This core SO already exists: " + fullPath + " - Skipping it.");
                        continue;
                    }

                    ScriptableObject asset = CreateInstance(typeName);

                    if (asset == null)
                    {
                        Debug.LogError("Unable to create SO " + typeName + " - Skipping it.");
                        continue;
                    }

                    AssetDatabase.CreateAsset(asset, fullPath);

                    // See if this Asset has any Setup required
                    if (asset is InputConditionType inputConditionType)
                    {
                        if (inputConditionType.name == "DriveLevelICT")
                        {
                            if (settings.driveLevelOCT == null)
                            {
                                Debug.LogError("Trying to create DriveLevelICT and set its mathingOCTs to DriveLevelOCT but settings.driveLevelOCT " +
                                               "is not set.  Please set it and manually fix DriveLevelICT MatchingOCTs.");
                            }
                            else
                            {
                                inputConditionType.matchingOCTs = new List<OutputChangeType>
                                {
                                    settings.driveLevelOCT
                                };

                                EditorUtility.SetDirty(inputConditionType);
                            }

                            settings.driveLevelICT = inputConditionType;
                        }
                        else if (inputConditionType.name == "CurrentActionTypeICT")
                        {
                            currentActionTypeICT = inputConditionType;
                        }
                    }
                    else if (asset is OutputChangeType outputChangeType && outputChangeType.name == "DriveLevelOCT")
                    {
                        settings.driveLevelOCT = outputChangeType;
                    }
コード例 #2
0
ファイル: TotalAISetupEditor.cs プロジェクト: ming81/TotalAI
        private void DoSetup()
        {
            Debug.Log("Starting Total AI Setup:");


            if (createFolders)
            {
                if (!defaultSOFolderRoot.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    defaultSOFolderRoot += Path.DirectorySeparatorChar;
                }
                Debug.Log("Creating SO Folders");
                if (!Directory.Exists(defaultSOFolderRoot))
                {
                    Debug.Log("Creating Root Directory: " + defaultSOFolderRoot);
                    Directory.CreateDirectory(defaultSOFolderRoot);

                    Debug.Log("Creating Sub Directories");
                    foreach (string typeName in allTypeNameDirectories)
                    {
                        Directory.CreateDirectory(defaultSOFolderRoot + typeName);
                    }
                }
                else
                {
                    Debug.Log("Root Directory: " + defaultSOFolderRoot + " Already Exists.  NOT creating any folders.");
                }

                AssetDatabase.Refresh();
            }

            if (createPrefabsFolders)
            {
                if (!defaultPrefabsFolderRoot.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    defaultPrefabsFolderRoot += Path.DirectorySeparatorChar;
                }
                Debug.Log("Creating Prefabs Folders");
                if (!Directory.Exists(defaultPrefabsFolderRoot))
                {
                    Debug.Log("Creating Root Directory: " + defaultPrefabsFolderRoot);
                    Directory.CreateDirectory(defaultPrefabsFolderRoot);

                    Debug.Log("Creating Sub Directories");
                    Directory.CreateDirectory(defaultPrefabsFolderRoot + "Agents");
                    Directory.CreateDirectory(defaultPrefabsFolderRoot + "WorldObjects");
                    Directory.CreateDirectory(defaultPrefabsFolderRoot + "AgentEvents");
                }
                else
                {
                    Debug.Log("Root Directory: " + defaultPrefabsFolderRoot + " Already Exists.  NOT creating any folders.");
                }

                AssetDatabase.Refresh();
            }


            if (createTotalAIManager)
            {
                GameObject manager = null;
                TotalAIManager totalAIManager = FindObjectOfType<TotalAIManager>();
                if (totalAIManager != null)
                {
                    Debug.LogError("TotalAIManager already exists in the Scene.  NOT creating a new one.");
                    manager = totalAIManager.gameObject;
                }
                else
                {
                    manager = new GameObject("TotalAIManager", typeof(TotalAIManager));
                    Debug.Log("Created TotalAIManager in the Scene.");
                }

                if (createTotalAISettings)
                {
                    if (!defaultSettingsPath.EndsWith(Path.DirectorySeparatorChar.ToString()) ||
                        !defaultSettingsPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
                    {
                        defaultSettingsPath += Path.DirectorySeparatorChar;
                    }
                    if (!Directory.Exists(defaultSettingsPath))
                    {
                        Debug.Log("Creating Directory: " + defaultSettingsPath);
                        Directory.CreateDirectory(defaultSettingsPath);
                        AssetDatabase.Refresh();
                    }
                    else
                    {
                        Debug.Log("Directory Exists: " + defaultSettingsPath);
                    }

                    if (File.Exists(defaultSettingsPath + defaultSettingsName + ".asset"))
                    {
                        Debug.Log("Total AI Settings file already exists.  NOT overwritting it.");
                    }
                    else
                    {
                        TotalAISettings settings = CreateInstance<TotalAISettings>();
                        AssetDatabase.CreateAsset(settings, defaultSettingsPath + defaultSettingsName + ".asset");
                        manager.GetComponent<TotalAIManager>().settings = settings;

                        // TODO: Add in defaults - maybe search for commone ICTs and OCTs
                        settings.scriptableObjectsDirectory = defaultSOFolderRoot.Remove(defaultSOFolderRoot.Length - 1, 1);
                        settings.prefabsDirectory = defaultPrefabsFolderRoot.Remove(defaultPrefabsFolderRoot.Length - 1, 1);
                        settings.movementTypes = new List<MovementType>();
                        settings.for2D = for2D;

                        EditorUtility.SetDirty(settings);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                        Debug.Log("Created '" + defaultSettingsPath + defaultSettingsName + ".asset' and attached it to the TotalAIManager.");
                    }
                }
            }

            if (createTimeManager)
            {
                TimeManager timeManager = FindObjectOfType<TimeManager>();
                if (timeManager != null)
                {
                    Debug.Log("TimeManager already exists in the Scene.  NOT creating a new one.");
                }
                else
                {
                    new GameObject("TimeManager", typeof(TimeManager));
                    Debug.Log("Created TimeManager in the Scene.");
                }
            }

            if (createGOAPManager)
            {
                GOAPPlannerManager manager = FindObjectOfType<GOAPPlannerManager>();
                if (manager != null)
                {
                    Debug.Log("GOAPPlannerManager already exists in the Scene.  NOT creating a new one.");
                }
                else
                {
                    new GameObject("GOAPPlannerManager", typeof(GOAPPlannerManager));
                    Debug.Log("Created GOAPPlannerManager in the Scene.");
                }
            }

            if (createLayers)
            {
                Debug.Log("Creating layers: Ground, Agent, WorldObject, AgentEvent, Inventory");
                List<string> layersToCreate = new List<string>() { "Ground", "Agent", "WorldObject", "AgentEvent", "Inventory" };
                CreateLayers(layersToCreate);
            }

            if (createScriptableObjects)
            {
                CreateCoreScriptableObjects();
            }
        }