//creates a unit regulator asset void CreateUnitRegulator(Unit unit) { if (unit == null) { return; } string folderPath = regulatorFolderPath + "/Unit Regulators"; //does the unit regulator folder not exist? if (AssetDatabase.IsValidFolder(folderPath) == false) { AssetDatabase.CreateFolder(regulatorFolderPath, "Unit Regulators"); //create it. } //see if the regulator already exists by going through the already created NPC Unit Regulators: int i = 0; while (i < targetNPCMgr.unitRegulatorAssets.Count) { NPCUnitRegulator nur = targetNPCMgr.unitRegulatorAssets[i]; SerializedObject nur_SO = new SerializedObject(nur); if (nur != null) //if the unit regulator is valid { //if there are actual prefabs in this regulator: if (nur.prefabs.Count > 0) { if (nur.prefabs[0].GetCode() == unit.GetCode()) //if it has the same code with this unit { //if the prefab isn't already in: if (nur.prefabs.Contains(unit) == false) { //simply add it as a new prefab in the same regulator: nur_SO.FindProperty("prefabs").InsertArrayElementAtIndex(nur.prefabs.Count); nur_SO.FindProperty($"prefabs.Array.data[{nur.prefabs.Count - 1}]").objectReferenceValue = unit; } return; //do not proceed. } } i++; } else //regulator isn't valid: { //remove it: targetNPCMgr.unitRegulatorAssets.RemoveAt(i); } } //no exisitng regulator the unit has been found, create new one NPCUnitRegulator newRegulator = ScriptableObject.CreateInstance <NPCUnitRegulator>(); AssetDatabase.CreateAsset(newRegulator, folderPath + "/" + unit.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 = unit; //add the unit. //add to list: targetNPCMgr.unitRegulatorAssets.Add(newRegulator); }
//creates a unit regulator asset void CreateUnitRegulator(Unit unit) { if (unit == null) { return; } string folderPath = regulatorFolderPath + "/Unit Regulators"; //does the unit regulator folder not exist? if (AssetDatabase.IsValidFolder(folderPath) == false) { AssetDatabase.CreateFolder(regulatorFolderPath, "Unit Regulators"); //create it. } //see if the regulator already exists by going through the already created NPC Unit Regulators: int i = 0; while (i < targetNPCMgr.unitRegulatorAssets.Count) { NPCUnitRegulator nur = targetNPCMgr.unitRegulatorAssets[i]; if (nur != null) //if the unit regulator is valid { //if there are actual prefabs in this regulator: if (nur.prefabs.Count > 0) { if (nur.prefabs[0].Code == unit.Code) //if it has the same code with this unit { //if the prefab isn't already in: if (nur.prefabs.Contains(unit) == false) { //simply add it as a new prefab in the same regulator: nur.prefabs.Add(unit); EditorUtility.SetDirty(nur); } return; //do not proceed. } } i++; } else //regulator isn't valid: { //remove it: targetNPCMgr.unitRegulatorAssets.RemoveAt(i); } } NPCUnitRegulator newRegulator = ScriptableObject.CreateInstance <NPCUnitRegulator>(); AssetDatabase.CreateAsset(newRegulator, folderPath + "/" + unit.Name + "Regulator_" + targetNPCMgr.code + ".asset"); //create an asset file for it. newRegulator.prefabs.Add(unit); //add the unit. EditorUtility.SetDirty(newRegulator); //add to list: targetNPCMgr.unitRegulatorAssets.Add(newRegulator); }