How can I get string from console text

Hello,
How can I get variable from console text. In console i have a message “Login Success” or “Wrong Password”. I want to get this text to string.160545-unity.png

Hey @mati_1102,

You can add a callback to Application.logMessageReceived. The signature should look like void YourFunctionName(string logString, string stackTrace, LogType type). The logString parameter is the one that holds the actual message that was passed to Debug.Log (“Login success” in your example image).


Below is a simple, but complete example that uses a custom UIElements editor window to echo messages logged to the Debug console. First, a screenshot of the result:


Editor/MyConsole.uxml

<?xml version="1.0" encoding="utf-8"?>
<engine:UXML
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:engine="UnityEngine.UIElements"
    xmlns:editor="UnityEditor.UIElements"
    xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"
>
  <engine:Button name="addMessagesButton" text="Click to log sample messages" />
  <engine:Button name="clearButton" text="Click to clear the messages below" />
  <engine:VisualElement name="logMessagesContainer" />
</engine:UXML>

Editor/MyConsole.cs

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class MyConsole : EditorWindow
{
    VisualElement logMessagesContainer;

    [MenuItem("ExploringUnity.com/MyConsole")]
    public static void ShowExample() { GetWindow<MyConsole>(); }

    public void OnEnable()
    {
        var uiTemplate = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/MyConsole.uxml");
        var ui = uiTemplate.CloneTree();
        rootVisualElement.Add(ui);
        logMessagesContainer = ui.Q<VisualElement>("logMessagesContainer");
        var addMessagesButton = ui.Q<Button>("addMessagesButton");
        addMessagesButton.clicked += LogRandomMessages;
        var clearButton = ui.Q<Button>("clearButton");
        clearButton.clicked += ClearConsole;

        // NOTE: This is how you register your function to be called for every log message.
        // NOTE: The function must return void and take (string, string, LogType)
        Application.logMessageReceived += EchoDebugMessage;
    }
    
    void EchoDebugMessage(string logString, string stackTrace, LogType type)
    {
        var label = new Label($"-- MSG: {logString}

" +
"-- STACK: {stackTrace}" + "-- TYPE: {type}
");
logMessagesContainer.Add(label);
}

    void ClearConsole() { logMessagesContainer.Clear(); }

    void LogRandomMessages()
    {
        Debug.Log("Regular log message.");
        Debug.LogError("Error message.");
        Debug.LogWarning("Warning message.");
        Debug.LogException(new System.Exception("Exception message."));
    }
}

Note: Tested with Unity 2019.3.13f1

Do you want any and all text from the console or only console messages that you have defined? If you only want to capture the “Login Success” and “Wrong Password” messages you could do it by making a custom log class

public class CustomLog
{
    // Static reference to the log obj
    public static Object LogStatus { get; private set; }
    public static void Log(Object obj)
    {
        // Output to Unity's console
        Debug.Log(obj);
        // Update the static reference
        LogStatus = obj;
    }

    // The above code would be called like this
    private void Main()
    {
        Log("Login Success");

        Object logStatus = LogStatus;
        //  logStatus now = "Login Success"

        Log("Wrong Password ");
            
        logStatus = LogStatus;
        // logStatus now = "Wrong Password"
    }
}

// From outside the class
public class TestClass
{
    private void Main()
    {
        CustomLog.Log("Login Success");

        Object logStatus = CustomLog.LogStatus;
        //  logStatus now = "Login Success"

        CustomLog.Log("Wrong Password ");

        logStatus = CustomLog.LogStatus;
        // logStatus now = "Wrong Password"
    }
}