Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mati_1102 · May 27, 2020 at 07:18 PM · unity 5stringconsole.log

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.alt text

unity.png (9.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by exploringunity · May 27, 2020 at 09:54 PM

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:

A custom editor window that copies debug log messages.


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}\n" +
                               $"-- STACK: {stackTrace}" +
                               $"-- TYPE: {type}\n");
         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


screenshot-6.png (115.9 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Konomira · May 27, 2020 at 10:45 PM

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"
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Konomira · May 27, 2020 at 10:51 PM 0
Share

Furthermore if you only care about the logs being string, you can change the Object type to string.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

225 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get string, but not the same twice in a row 1 Answer

overlapshere wont convert its hit colliders name to a string 2 Answers

Random String from list of string without repeated letters 2 Answers

cant get if (String.Equals(magicsetom[x], boxadd)) working 0 Answers

How to get Dictionary ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges