-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileReader.cs
85 lines (79 loc) · 1.84 KB
/
FileReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.Networking;
public class FileReader
{
public bool LoadCompleted { get; private set; }
public FileStream FileStr;
public StreamReader Sr;
public byte[] Buffer;
public void LoadFile(string path)
{
LoadCompleted = false;
#if UNITY_EDITOR || UNITY_IPHONE
try
{
LoadCompleted = false;
FileStr = File.OpenRead(path);
Buffer = new byte[FileStr.Length];
FileStr.BeginRead(Buffer, 0, (int)FileStr.Length, OnReadCallback, this);
}
catch
{
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
#else
CoroutineExecutor.ExecuteCoroutine(DoLoadFile(path));
#endif
}
private IEnumerator DoLoadFile(string path)
{
using (WWW uwr = new WWW(path))
{
yield return uwr;
if (uwr.isDone)
{
Buffer = Encoding.UTF8.GetBytes(uwr.text);
}
LoadCompleted = true;
}
}
private void OnReadCallback(IAsyncResult iar)
{
FileReader re = iar.AsyncState as FileReader;
try
{
re.FileStr.EndRead(iar);
if (iar.IsCompleted)
{
Debug.Log("Read Successfully!");
LoadCompleted = true;
}
}
catch
{
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
finally
{
FileStr.Close();
FileStr.Dispose();
}
}
}