2020년 8월 12일 수요일

[Unity] LogManager

 using UnityEngine;

using System.Collections;
using System.Collections.Generic;

using System.IO;
using System;
using UnityEngine.UI;

public class LogManager : MonoBehaviour
{
    //해야할일
    //컨텐츠가 추가되면 스크롤이 가장 밑으로 내려가는 기능 추가해야함~


    public string logTimetext;
    public GameObject prefab;
    public Transform Log_Content;

    //test
    public int textidx;
    //test


    public delegate void handleEventLog(string log);
    public static handleEventLog del_eventLog;


    void Start()
    {
        Screen.fullScreen = false;

        del_eventLog += func_Write;

        func_Write ("Start--------------------------" +System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
    }

    void Update()
    {
        if (Input.GetKeyDown("a"))
        {
            func_Write("dnddbd"textidx);
            textidx++;
        }
    }

    void OnApplicationQuit()
    {
        func_Write ("End-----------------------------" + System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
    }



    public void func_Write(string strData)
    {
        WriteData(strData);
    }


    private IEnumerator ForceScrollDown()
    {
        yield return new WaitForEndOfFrame ();
        Canvas.ForceUpdateCanvases ();
        
        Log_Content.transform.parent.parent.GetComponent<ScrollRect>().verticalNormalizedPosition = 0;
        Canvas.ForceUpdateCanvases ();
    }

//    private IEnumerator TextRead(string strData)
//    {
////        logTimetext = "";
       
//        WriteData(strData);//파일에 write
//        yield return new WaitForSeconds(0.1f);
////        logTimetext += "[" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff tt") + "]" + " :";
//        //testtext.text += ReadLastLine() + "\n";
       
////        GameObject G;
////        G =Instantiate(prefab, Log_Content);
////        G.GetComponent<Text>().text += logTimetext + ReadLastLine();
////        StartCoroutine (ForceScrollDown ());
//      //맨밑으로 초기화 
//      //Log_Content.transform.parent.parent.GetComponent<ScrollRect>().normalizedPosition = new Vector2(0,0);
//    }


    public void WriteData(string strData)
    {
        // FileMode.Create는 덮어쓰기.
        FileStream f = new FileStream(Application.dataPath"/StreamingAssets" + "/"+"Log_"+System.DateTime.Now.ToString("yyyy-MM-dd") +".txt"FileMode.Append,FileAccess.Write);//, FileAccess.Write);

        StreamWriter writer = new StreamWriter(fSystem.Text.Encoding.Unicode);
        writer.WriteLine("[" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff tt") + "] :" + strData);

        logTimetext = "";
        logTimetext += "[" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff tt") + "]" + " :";

        GameObject G;
        G =Instantiate(prefabLog_Content);
        G.GetComponent<Text>().text += logTimetext + strData ;
        StartCoroutine (ForceScrollDown ());

        writer.Close();
    }

    //모든 데이터 읽기 지금은 필요없음
    //public void ReadData()
    //{
    //    StreamReader sr = new StreamReader(Application.dataPath + "/StreamingAssets" + "/" + "Log.txt");
    //    Debug.Log("Read Data: " + sr.ReadLine());
    //    sr.Close();
    //}

  //  public string ReadLastLine()
  //  {
        //StreamReader sr = new StreamReader(Application.dataPath + "/StreamingAssets" + "/" +"Log.txt");
  //      string result;
  //      result = "";
  //      while (!sr.EndOfStream)
  //      {
  //          result = sr.ReadLine();
  //      }
  //      sr.Close();
  //      return result;
  //  }

}

댓글 없음:

댓글 쓰기

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...