You are developing an RPG with procedurally generated dungeons. Your task is to implement a logging system that helps debug level generation and the combat system.
You are given a DungeonGenerator class that generates rooms and enemies, and a CombatSystem class that handles combat.
In their current form, they use raw Debug.Log, which clutters the console and will slow down the release build.
- Create a
GameLoggerclass with the following methods:LogGeneration(string message)— logs for the dungeon generation process. Should work ONLY inside the Unity editor.LogCombat(string message)— logs for the combat system. Should work in Development builds and in the editor.LogCriticalError(string message)— critical error. Should work ALWAYS (including Release builds) and write the message to a filecritical_errors.txtinsideApplication.persistentDataPath.
- Modify the code of the classes, replacing direct
Debug.Logcalls with calls to yourGameLogger. - Set up interception of all logs via
Application.logMessageReceivedso that allLogGenerationandLogCombatmessages are also duplicated to afull_debug.logfile, but only in Development builds (check viaDebug.isDebugBuild).
// DungeonGenerator.cs
using UnityEngine;
public class DungeonGenerator : MonoBehaviour
{
public void GenerateDungeon()
{
Debug.Log("Starting dungeon generation...");
// ... generation logic
Debug.Log("Created room #" + Random.Range(1, 20));
// ... more logic
Debug.Log("Generation complete. Total enemies: " + Random.Range(5, 50));
}
}
// CombatSystem.cs
using UnityEngine;
public class CombatSystem : MonoBehaviour
{
public void Attack( GameObject target, int damage )
{
Debug.Log($"Attack on {target.name} with {damage} damage");
// ... combat logic
if (Random.value < 0.1f)
Debug.LogError("CRITICAL ERROR: target not found in the combat system!");
}
}- When running in the Unity editor — all logs (generation, combat, and errors) appear in the console.
- In a Release build (without the Development Build checkbox) — calls to
LogGenerationandLogCombatare not executed at all, but errors are written to a file. - In a Development build — a
full_debug.logfile appears insidepersistentDataPathcontaining ALL logs (duplicated from the console), and a separatecritical_errors.txtfile with critical errors. - The code must not contain
#if ... #endifdirectives (use[Conditional]and theDebug.isDebugBuildcheck).
- The script with the solution of the task -
GameLogger.cs - Modified source scripts: