IOS and Android--------outlog

Source: Internet
Author: User

If you are on the iOS platform, it is strongly recommended that the script call optimization inside the playersetting be set to slow and Safe, such as errors such as a null pointer or an array out of bounds, the program will not flash directly. (not set on Android) Here I create an array of errors that are out of bounds.


void Start () {
int []test = new int[1];
TEST[2] = 0;
}


Using Unityengine;
Using System.Collections;
Using System.Collections.Generic;
Using System.IO;
Using System.Text;

public class Zyoutlog:monobehaviour
{
static list<string> Mlines = new list<string> ();
static list<string> Mwritetxt = new list<string> ();
private string Outpath;
void Start ()
{
Application.persistentdatapath Unity is the only path that can be read or written.
Outpath = Application.persistentdatapath + "/outlog.txt";
Log saved before each startup client is deleted
if (System.IO.File.Exists (Outpath))
{
File.delete (Outpath);
}
To do a log monitor here.
Application.registerlogcallback (Handlelog);
An output
Debug.Log ("Yufei");
}

void Update ()
{
Because the operation to write to the file must be done in the main thread, in the update, you write the file.
if (Mwritetxt.count > 0)
{
string[] temp = Mwritetxt.toarray ();
foreach (String t in temp)
{
using (StreamWriter writer = new StreamWriter (Outpath, True, Encoding.UTF8))
{
Writer. WriteLine (t);
}
Mwritetxt.remove (t);
}
}
}

void Handlelog (String logstring, String stackTrace, LogType type)
{
Mwritetxt.add (logstring);
if (type = = Logtype.error | | type = = logtype.exception)
{
Log (logstring);
Log (StackTrace);
}
}

Here I save the wrong information for output on the phone screen
static public void Log (params object[] objs)
{
String text = "";

for (int i = 0; i < Objs. Length; ++i)
{
if (i = = 0)
{
Text + = Objs[i]. ToString ();
}
Else
{
Text + = "," + objs[i]. ToString ();
}
}
if (application.isplaying)
{
if (Mlines.count > 20)
{
Mlines.removeat (0);
}
Mlines.add (text);

}
}

void Ongui ()
{
Gui.color = color.red;

for (int i = 0, Imax = mlines.count; i < IMAX; ++i)
{
Guilayout.label (Mlines[i]);
}
}
}

IOS and Android--------outlog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.