//creates a building regulator asset void CreateBuildingRegulator(Building building) { //if the building is invalid then do not proceed. if (building == null) { return; } string folderPath = regulatorFolderPath + "/Building Regulators"; //does the building regulator folder not exist? if (AssetDatabase.IsValidFolder(folderPath) == false) { AssetDatabase.CreateFolder(regulatorFolderPath, "Building Regulators"); //create it. } //see if the regulator already exists by going through the already created NPC Building Regulators: int i = 0; while (i < targetNPCMgr.buildingRegulatorAssets.Count) { NPCBuildingRegulator nbr = targetNPCMgr.buildingRegulatorAssets[i]; SerializedObject nbr_SO = new SerializedObject(nbr); if (nbr != null) //if the unit regulator is valid { //if there are actual prefabs in this regulator: if (nbr.prefabs.Count > 0) { if (nbr.prefabs[0].GetCode() == building.GetCode()) //if it has the same code with this building { //if the prefab isn't already in: if (nbr.prefabs.Contains(building) == false) { //simply add it as a new prefab in the same regulator: nbr_SO.FindProperty("prefabs").InsertArrayElementAtIndex(nbr.prefabs.Count); nbr_SO.FindProperty($"prefabs.Array.data[{nbr.prefabs.Count - 1}]").objectReferenceValue = building; } return; //do not proceed. } } i++; } else //invalid building regulator { targetNPCMgr.buildingRegulatorAssets.RemoveAt(i); //remove it. } } //reaching this stage means we need to create a new one. NPCBuildingRegulator newRegulator = ScriptableObject.CreateInstance <NPCBuildingRegulator>(); //new instance of the building regulator AssetDatabase.CreateAsset(newRegulator, folderPath + "/" + building.GetName() + "Regulator_" + targetNPCMgr.code + ".asset"); //create an asset file for it. SerializedObject newRegulator_SO = new SerializedObject(newRegulator); newRegulator_SO.FindProperty("prefabs").InsertArrayElementAtIndex(0); newRegulator_SO.FindProperty($"prefabs.Array.data[0]").objectReferenceValue = building; //add the building //add to list: targetNPCMgr.buildingRegulatorAssets.Add(newRegulator); }
//creates a building regulator asset void CreateBuildingRegulator(Building building) { //if the building is invalid then do not proceed. if (building == null) { return; } string folderPath = regulatorFolderPath + "/Building Regulators"; //does the building regulator folder not exist? if (AssetDatabase.IsValidFolder(folderPath) == false) { AssetDatabase.CreateFolder(regulatorFolderPath, "Building Regulators"); //create it. } //see if the regulator already exists by going through the already created NPC Building Regulators: int i = 0; while (i < targetNPCMgr.buildingRegulatorAssets.Count) { NPCBuildingRegulator nbr = targetNPCMgr.buildingRegulatorAssets[i]; if (nbr != null) //if the unit regulator is valid { //if there are actual prefabs in this regulator: if (nbr.prefabs.Count > 0) { if (nbr.prefabs[0].Code == building.Code) //if it has the same code with this building { //if the prefab isn't already in: if (nbr.prefabs.Contains(building) == false) { //simply add it as a new prefab in the same regulator: nbr.prefabs.Add(building); EditorUtility.SetDirty(nbr); } return; //do not proceed. } } i++; } else //invalid building regulator { targetNPCMgr.buildingRegulatorAssets.RemoveAt(i); //remove it. } } //reaching this stage means we need to create a new one. NPCBuildingRegulator newRegulator = ScriptableObject.CreateInstance <NPCBuildingRegulator>(); //new instance of the building regulator AssetDatabase.CreateAsset(newRegulator, folderPath + "/" + building.Name + "Regulator_" + targetNPCMgr.code + ".asset"); //create an asset file for it. newRegulator.prefabs.Add(building); //add the building. EditorUtility.SetDirty(newRegulator); //add to list: targetNPCMgr.buildingRegulatorAssets.Add(newRegulator); }