-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreatmentController.cs
350 lines (289 loc) · 11.9 KB
/
TreatmentController.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;//正则表达式
public class TreatmentController : MonoBehaviour {
public static TreatmentController _instance;
//private Text TalkText;
private Text LeftName;
public bool AutoPlaying=false;//是否在执行自动播放协程
private AudioSource voiceSource;
private AudioSource musicSource;
private AudioSource soundSource;
public string[] Treatment;
public GameObject LogScrollBar;
public int startIndex = 0;//读档用
public int index=0;
public string[] info;
public float TextSpeed;
public string protagonist="将臣";//主人公名称
public float turnBgTime = 1.5f;
public GameObject Logs,LogInstance,LogParent;
public bool End = false;
public float waitAudioTime=1;//自动播放时间间隔
public bool treatmentAutoPlay = false;//自动播放
public float playTime=-1;//当前播放时间
public string treatmentName = "Test";//无txt后缀
public string currentTreatmentText, currentPersonName, currentPersonPicturePath;
public string oldPersonPicture;//用于在无法获得人物图片路径时的旧的人物图片路径
private void Awake()
{
_instance = this;
LeftName = GameObject.Find("LeftName").GetComponent<Text>();
voiceSource = GetComponent<AudioSource>();
musicSource = gameObject.AddComponent<AudioSource>();
soundSource = gameObject.AddComponent<AudioSource>();
UIController._instance.SwitchPerson("All", "off");//清除所有人物
}
public void ReadTreatment(string treatmentName,int index)//读取至第某剧本第index行
{
Debug.Log("读取新剧本" + treatmentName);
this.treatmentName = treatmentName;
this.index = 0;
Treatment = FileController._instance.ReadFile(treatmentName+".txt");
UIController._instance.SwitchPerson("All", "off");
ReadTreatmentToIndex(index);
}
public int FindIndexByText(string text)
{
for(int i=index-1;i>=0;i--)
{
if (Treatment[i].Split('|').Length<2)//如果不是对话行
{
continue;
}
string treatmentTextInIndex = Treatment[i].Split('|')[1];
Debug.Log("寻找index,当前行文本为" + treatmentTextInIndex + ",index为" + i);
if(text==treatmentTextInIndex)
{
return i;
}
}
return 0;
}
public void ReadTreatmentToIndex(int index)//从第0行读到第index行位置的命令和立绘,这样读档时才能显示正确立绘,否则可能会缺少立绘
{
for (int i = 0; i < index; i++)//执行一次所有的命令
{
Debug.Log(i);
ReadTreatmentLine(i);//读取第几行剧本
}
if(index==0)
{
this.index = 0;
}
else
{
this.index = index;
}
Debug.Log("读取到第" + this.index+"行");
ReadTreatmentLine(this.index);
}
public void PlayMusic(string musicName, bool loop)
{
AudioClip musicClip = Resources.Load<AudioClip>("Music/" + musicName);
if (musicClip != null)
{
musicSource.clip = musicClip;
musicSource.loop = loop;
musicSource.Play();
}
else
{
Debug.LogWarning("Music not found: " + musicName);
}
}
public void StopMusic()
{
musicSource.Stop();
}
private void PlaySound(string soundName, int playCount)
{
StartCoroutine(PlaySoundCoroutine(soundName, playCount));
}
private IEnumerator PlaySoundCoroutine(string soundName, int playCount)
{
for (int i = 0; i < playCount; i++)
{
AudioClip soundClip = Resources.Load<AudioClip>("Sounds/" + soundName);
if (soundClip != null)
{
soundSource.clip = soundClip;
soundSource.Play();
yield return new WaitForSeconds(soundClip.length);
}
}
}
public void StopAllSounds()
{
voiceSource.Stop();
musicSource.Stop();
soundSource.Stop();
}
private void Update()
{
//自动播放算法说明,在每次读取剧本时计算出应该等待的时间,并且开始计时播放时间。如果已经开启自动播放,则启动协程,在到时间后自动读取下一行剧本
//同时将AutoPlaying作为是否在使用协程自动播放的标志,如果当前剧本没有启动协程(AutoPlaying==false),说明在读取此行时没有开启自动播放。
//所以如果此时开启自动播放,应等待这行语音播放完或字显示完后再读取下一行剧本,也就是当(playTime>waitAudioTime)时。然而如果当正在播放某行剧本时关闭自动播放
//name就应该关闭协程,并将AutoPlaying(是否在运行协程)设置为false;
if (playTime>=0)
{
playTime += Time.deltaTime;
}
if(treatmentAutoPlay&&!AutoPlaying)
{
if(playTime>waitAudioTime)
{
playTime = -1;
ReadTreatmentLine(index);
}
}
if(!treatmentAutoPlay)//停止了自动播放
{
StopCoroutine("AutoPlay");
AutoPlaying = false;
}
}
public void ReadTreatmentLine(int line)
{
StopCoroutine("AutoPlay");
playTime = 0;//播放时间计时
string tempText = Treatment[line];//读取剧本行
if (tempText[0] == '#')//读取到注释行,读取下一行
{
Debug.Log(tempText);
ReadTreatmentLine(line + 1);
index++;
return;
}
if (tempText[0] == '@')//命令行
{
//命令
string command = tempText.Substring(1);//获取命令
string[] parameters = Regex.Split(command, "\\s+");
Debug.Log("命令:" + parameters[0] );
if (parameters[0] == "StopAllSounds")
{
// 停止所有声音
StopAllSounds();
return; // 停止解析剩余的命令
}
if (parameters[0] == "TurnBg")//切换背景
{
UIController._instance.TurnBackground(parameters[1], float.Parse(parameters[2]));
}
else if (parameters[0] == "SwitchPerson")
{
UIController._instance.SwitchPerson(parameters[1], parameters[2]);
}
else if (parameters[0] == "AddPerson")
{
UIController._instance.AddPerson(parameters[1], parameters[2], parameters[3]);
}
else if(parameters[0] == "End")
{
UIController._instance.TurnBackground("画面_黑", 2);
UIController._instance.SwitchWindow("off");
End = true;
return;
}
if (parameters[0] == "PlayMusic")//切换music
{
PlayMusic(parameters[1], parameters.Length > 2 && parameters[2] == "loop");
}
else if (parameters[0] == "StopMusic")
{
StopMusic();
}
else if (parameters[0] == "PlaySound")
{
int playCount = 1; // 默认播放一次
int parsedCount;
if (parameters.Length > 2 && int.TryParse(parameters[2], out parsedCount))
{
playCount = parsedCount; // 获取指定的播放次数
}
PlaySound(parameters[1], playCount);
}
else
{
// ... (之前的代码部分)
}
ReadTreatmentLine(line + 1);
index++;
return;
}
info = tempText.Split('|');
//Debug.Log("读取第" + line + "行,info[0]="+info[0]+",info[1]="+info[1]+ ",info[2]=" + info[2]);
currentPersonName = info[0];
currentTreatmentText = info[1];
currentPersonPicturePath = info[2];//由于人物是“我”或者主人公名字是或者路径是"null"时不会获取到的人物图片路径是无法使用的
string voicePath = info[3];
string picturePosition = info[4];
//@开头的行是命令行
//图片路径处开头是@的话则使用自定义路径
if (info[2][0] == '@')
{
//自定义图片路径
currentPersonPicturePath = currentPersonPicturePath.Substring(currentPersonPicturePath.IndexOf('@') + 1);
UIController._instance.LoadPicture(currentPersonPicturePath, picturePosition);
}
else if (currentPersonName != "我" && currentPersonPicturePath != "null" && currentPersonName != protagonist)//否则使用默认路径显示立绘
{
currentPersonPicturePath = "Pictures/Person/" + currentPersonName + "/" + currentPersonPicturePath;//根据名字查找表情
oldPersonPicture = currentPersonPicturePath;
UIController._instance.LoadPicture(currentPersonPicturePath, picturePosition);
}
else if (currentPersonPicturePath == "null" || currentPersonName == "我" || currentPersonName == protagonist)//如果名字是“我”或主人公或者路径处是空,则不对立绘做处理
{
currentPersonPicturePath = oldPersonPicture;
//不处理
}
else if (currentPersonPicturePath == "hide")//如果路径处是hide,则隐藏立绘
{
currentPersonPicturePath = "None";//全透明图片,隐藏立绘
UIController._instance.LoadPicture(currentPersonPicturePath, picturePosition);
}
//播放语音
AudioClip voice = (AudioClip)Resources.Load("Voice/" + currentPersonName + "/" + voicePath, typeof(AudioClip));
voiceSource.clip = voice;
voiceSource.Play();
//自动播放逻辑
if(voicePath!=""&&voicePath!="null")//有语音路径
{
waitAudioTime = voiceSource.clip.length + 1;//获取语音长度,根据语音长度来决定自动播放时间间隔,开启计时协程,到时间自动播放下一条对话,注意协程必须在读取剧本时关闭。
}
else
{
waitAudioTime =Mathf.CeilToInt((currentTreatmentText.Length / UIController._instance.showTextSpeed))+2;
}
if (treatmentAutoPlay)
{
StartCoroutine("AutoPlay");
}
//名字显示
string leftNameText = currentPersonName;
//对话内容
if (currentPersonName != "我"&¤tPersonName!="")
{
leftNameText= "【" + currentPersonName + "】";
currentTreatmentText = "「" + currentTreatmentText + "」";
}
if(currentPersonName=="我")
{
leftNameText = "";
}
LeftName.text = leftNameText;
UIController._instance.ShowTalkText(currentTreatmentText,UIController._instance.showTextSpeed);
index++;
}
IEnumerator AutoPlay()//自动播放
{
AutoPlaying = true;
yield return new WaitForSeconds(waitAudioTime);
AutoPlaying = false;
ReadTreatmentLine(index);
}
}