Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Editor Console logs from script

Tags:

c#

unity3d

This is my script and I tried to make "" in case it's not one of the cases:

private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.name == "Platform")
         Debug.Log("Touching Platform");
    else Debug.Log("");
}
private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.name == "OnTop Detector")
    {
        Debug.Log("On Top of Platform");
        
        GameObject findGo  = GameObject.Find("ThirdPersonController");
        GameObject findGo1 = GameObject.Find("Elevator");
        
        findGo.transform.parent = findGo1.transform;
    }
    else Debug.Log("");
}

But this "" not working. It's not deleting the text from the console while the game is running. And also when i stop the game and running it again it keep showing the last text of the Debug.Log.

I want to clear it if none of the If's happen in my script.

I saw this answer:

Answer

But i'm not sure if this is what I need and how to use it. Make a new script ?

And also what about errors. If I clear the Log in the console it will delete also erorrs if there will be some ?

In this screenshot it's in state after I touched the platform and then moved away from it but the text still exist in the console log:

Screenshot

like image 218
moshe ralf Avatar asked Nov 13 '16 18:11

moshe ralf


People also ask

How to remove all console logs from your project before production?

Removing all console.log () from your project before production can be very difficult, Here is how to remove all console.log () from your project in less than a minute. Open your project in VS Code ( Since we are using Regex, It is easier to use it in VS Code ) Click on the search icon on VS Code sidebar. It will open the search option

What is the use of console log in JavaScript?

console.log () statements are really handy tools for JavaScript development. We can easily track our app’s execution and peek its states. They can cause serious performance issues in applications especially when accidentally left in render logics. It is also risky to log sensitive data on the console in production such as passwords, keys, etc.

How to match all console logs with empty string?

You will be able to match all console.logs with " or ' wrapping strings. Then you replace then with empty string. You have to select g flag, or GLOBAL option or Replace All button. Also consider, that console object has multiple methods, such as dir, table, group, etc...

Can they rewrite the source code for console logs?

Well, they can rewrite the console.log source code, but why? "it may be a good idea to write your own logger object/function that wraps around the console object": I've done this in past and it's a bad idea. The trace of the console output refers to the wrapper and not to the line that invokes it, making debugging more difficult.


1 Answers

The Debug.ClearDeveloperConsole() function is used when you clear logs from an application that was built while Debug Build is enabled in your project. There is no official API for clearing the Editor log.

Most Editor functionality can be replicated with Reflection just like hiding Gizmos and toggling the Stats Panel. I was going to write one but found this one.

This should clear every log on the Console tab.

using System.Reflection;

public void ClearLog()
{
    var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
    var type = assembly.GetType("UnityEditorInternal.LogEntries");
    var method = type.GetMethod("Clear");
    method.Invoke(new object(), null);
}

Now, you can call ClearLog(); in your else statements.

EDIT:

This has changed recently in about ~Unity 2017. Since it is done with reflection, I consider it to change again anytime if any class, variable or function used in this code is renamed by Unity. Below is the new way to do this:

public void ClearLog()
{
    var assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
    var type = assembly.GetType("UnityEditor.LogEntries");
    var method = type.GetMethod("Clear");
    method.Invoke(new object(), null);
}
like image 193
Programmer Avatar answered Sep 30 '22 13:09

Programmer