这里使用txt来保存剧本,为了便于解析,格式如下。
类型 | 角色位置 | 角色名称 | 角色对话内容 | 角色图片 |
0/1 | left/right | 阿黄 | 你好 | Test.png |
类型:0:代表载入背景图片 1:代表载入人物对话内容 后期也可以加上人物选择,跳转剧本等等....
角色位置:就是人物出现的位置,左啊有啊,当然后期也能加入自定义位置的功能...
角色名称:没什么好解释的了
角色对话内容:同上
角色图片:需要载入
图片:根据类型载入的图片,例如是人物图片或者背景图片
下面是根据基本存储格式的创建的一个简单数据类。
using UnityEngine;
using System.Collections;
public class ScriptData {
public int type;
public string pos;
public string name;
public string talk;
public string picName;
public ScriptData(int type,string pos,string name,string talk,string picName)
{
this.type = type;
this.pos = pos;
this.name = name;
this.talk = talk;
this.picName = picName;
}
public ScriptData(int type, string picName)
{
this.type = type;
this.picName = picName;
}
}
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
public class LoadScripts : MonoBehaviour {
public static LoadScripts instance;
int index;
List txt;
// Use this for initialization
void Awake()
{
instance = this;
index=0;
}
void Start () {
}
public void loadScripts(string txtFileName)
{
index=0;
txt = new List();
StreamReader stream = new StreamReader("Scripts/" + txtFileName);
while (!stream.EndOfStream)
{
txt.Add(stream.ReadLine());
}
stream.Close();
}
public ScriptData loadNext()
{
if (index < txt.Count)
{
string[] datas = txt[index].Split(',');
int type = int.Parse(datas[0]);
if (type == 0)
{
string picName = datas[1];
index++;
return new ScriptData(type, picName);
}
else
{
string pos = datas[1];
string name = datas[2];
string talk = datas[3];
string picName = datas[4];
index++;
return new ScriptData(type, pos, name, talk, picName);
}
}
else
{
return null;
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
public class GameManager : MonoBehaviour {
public Text name;
public Text talk;
public Image background;
public Image left;
public Image right;
// Use this for initialization
void Start () {
LoadScripts.instance.loadScripts("first.txt");
handleData(LoadScripts.instance.loadNext());
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
handleData( LoadScripts.instance.loadNext());
}
}
public void setText(Text text, string content)
{
print(content);
text.text = content;
}
public void setImage(Image image,string picName)
{
image.sprite = loadPicture("Picture/" + picName);
}
public Sprite loadPicture(string picPath)
{
//创建文件读取流
FileStream fileStream = new FileStream(picPath, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建文件长度缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream = null;
//创建Texture
int width = Screen.width;
int height = Screen.height;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
//创建Sprite
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
return sprite;
}
public void handleData(ScriptData data)
{
if (data == null)
return;
if (data.type == 0)
{
setImage(background,data.picName);
print(data.picName);
handleData(LoadScripts.instance.loadNext());
}
else
{
if (data.pos.CompareTo("left") == 0)
{
left.gameObject.SetActive(true);
setImage(left, data.picName);
right.gameObject.SetActive(false);
}
else
{
right.gameObject.SetActive(true);
setImage(right, data.picName);
left.gameObject.SetActive(false);
}
setText(name, data.name);
setText(talk, data.talk);
}
}
}