PlayMaker Help & Tips > PlayMaker Help

Logging output to string

(1/2) > >>

artician:
This is probably a simple problem, but I want to get whatever the debug/console/log spits out and store it in a string variable.  My intention is just to see what errors are being produced when I'm running a web build.

Thanks for any help.

Alex Chouls:
Actually there isn't a super easy way to do this with Unity log commands (that I know of anyway).

I assume you want something like this:
http://forum.unity3d.com/threads/108400-Isn-t-there-no-way-to-show-debug-console-in-standalone-build

This might also be useful:
http://answers.unity3d.com/questions/18134/debug-console-in-standalone.html

artician:
From the first link, someone shared the perfect script:
http://zaxisgames.blogspot.com/2012/03/let-me-debug-in-your-gui.html

Which is exactly what I needed, and I thought I'd share it here.

Thank you for the help!

(The script itself)::

--- Code: ---using UnityEngine;
using System.Collections;
 
public class MyLog : MonoBehaviour
{
    static string myLog;
    static Queue myLogQueue = new Queue();
    public string output = "";
    public string stack = "";
    private bool hidden = true;
    private Vector2 scrollPos;
    public int maxLines = 30;
 
    void OnEnable()
    {
        Application.RegisterLogCallback(HandleLog);
    }
 
    void OnDisable()
    {
        Application.RegisterLogCallback(null);
    }
 
    void HandleLog(string logString, string stackTrace, LogType type)
    {
        output = logString;
        stack = stackTrace;
        string newString = "\n [" + type + "] : " + output;
        myLogQueue.Enqueue(newString);
        if (type == LogType.Exception)
        {
            newString = "\n" + stackTrace;
            myLogQueue.Enqueue(newString);
        }
 
        while (myLogQueue.Count > maxLines)
        {
            myLogQueue.Dequeue();
        }
 
        myLog = string.Empty;
        foreach (string s in myLogQueue)
        {
            myLog += s;
        }
    }
 
    void OnGUI()
    {
        if (!hidden)
        {
            GUI.TextArea(new Rect(0, 0, Screen.width / 3, Screen.height), myLog);
            if (GUI.Button(new Rect(Screen.width - 100, 10, 80, 20), "Hide"))
            {
                hide(true);
            }
        }
        else
        {
            if (GUI.Button(new Rect(Screen.width - 100, 10, 80, 20), "Show"))
            {
                hide(false);
            }
        }
    }
 
    public void hide(bool shouldHide)
    {
        hidden = shouldHide;
    }
}
--- End code ---

Alex Chouls:
A word of caution: Apparently Unity only allows one log callback to register at a time, and Playmaker already registers one. So does Script Inspector+, and UnityVS... so this might not be reliable with multiple scripts fighting for access to the log :/ But I also haven't done a lot of testing of this yet... so I'm curious to hear your results...

artician:
Good to know.  For my own curiosity: Does playmaker make log callbacks even on standalone builds?  How about when Enable Logging is disabled in the Debugging Preferences?

I have not changed PM preferences or tried this script with different settings, but I have 'Enable Logging' enabled, and 'Forward Playmaker Log to Unity log' disabled.  In the week of use I've had with it, it's worked as expected in both the editor and standalone builds.  If there's any interference I definitely haven't noticed it.

Navigation

[0] Message Index

[#] Next page

Go to full version