FileTool

using System;
using System.Collections;
using System.IO;
using UnityEngine;

public class FileTool : MonoBehaviour
{
  private static FileTool instance;

  public static FileTool Instance
  {
    get
    {
      if ((UnityEngine.Object) FileTool.instance == (UnityEngine.Object) null)
        FileTool.instance = new GameObject(nameof (FileTool)).AddComponent();
      return FileTool.instance;
    }
  }

  public static void SaveFile(string url, string contents)
  {
    try
    {
      File.WriteAllText(url, contents);
    }
    catch (Exception ex)
    {
      Debug.LogError((object) ("SAVE FILE " + url + " FAILED!----" + (object) ex));
    }
  }

  public static void SaveFile(string url, string[] lines)
  {
    try
    {
      File.WriteAllLines(url, lines);
    }
    catch (Exception ex)
    {
      Debug.LogError((object) ("SAVE FILE " + url + " FAILED!----" + (object) ex));
    }
  }

  public static string[] LoadFile(string url)
  {
    try
    {
      return File.ReadAllLines(url);
    }
    catch (Exception ex)
    {
      Debug.LogError((object) ("LOAD FILE " + url + " FAILED!----" + (object) ex));
      return (string[]) null;
    }
  }

  public static string LoadFile(string url, bool isString)
  {
    try
    {
      return File.ReadAllText(url);
    }
    catch (Exception ex)
    {
      return (string) null;
    }
  }

  public void LoadFile(string url, Action back)
  {
    this.StartCoroutine(this.GetFile(url, back));
  }

  private IEnumerator GetFile(string path, Action back)
  {
    WWW www = new WWW(path);
    while (!www.isDone)
      yield return (object) null;
    if (!string.IsNullOrEmpty(www.error))
      Debug.LogError((object) www.error);
    else
      back(www.text);
  }

  public static string[] LoadFileAllLine(string url)
  {
    try
    {
      return File.ReadAllLines(url);
    }
    catch (Exception ex)
    {
      Debug.LogError((object) ("LOAD FILE " + url + " FAILED!----" + (object) ex));
      return (string[]) null;
    }
  }
}

你可能感兴趣的:(FileTool)