Skip to content

Instantly share code, notes, and snippets.

@klkucan
Last active September 12, 2017 13:01
Show Gist options
  • Save klkucan/fb5a44fce8b0a68b08d6e64e2fb3e805 to your computer and use it in GitHub Desktop.
Save klkucan/fb5a44fce8b0a68b08d6e64e2fb3e805 to your computer and use it in GitHub Desktop.
Use csv file in Unity
public class Story
{
public int INDEX;
/// <summary>
/// 剧情阶段
/// </summary>
public int Step;
/// <summary>
/// 剧情类型(int)
/// 0:动画,1:声音(说话等),2:移动,3:展示与隐藏(用于剧情人物出现在不同的地方),
/// 4:特殊剧情(例如穿越和反穿)
/// </summary>
public int Type;
/// <summary>
/// 游戏中的对象名称(string),根据此名字通过代码找到GameObject。
/// </summary>
public string Object;
/// <summary>
/// 类型为动画时对应的状态机的条件参数(string)
/// </summary>
public string AimatorParameter;
/// <summary>
/// 类型为声音时播放的声音片段,对应Sound.xml中的index。
/// 如果为-1,则认为是静音。
/// </summary>
public int Sound;
/// <summary>
/// 类型为移动时对应的路线(int),对应MovePath.xml中的index。
/// </summary>
public int Path;
/// <summary>
/// 类型为展示与隐藏时对应数值,(int 0:隐藏,1:展示)
/// </summary>
public int IsShow;
/// <summary>
/// 对应特别剧情表的index。
/// </summary>
public int SpecialPlot;
/// <summary>
/// 此剧情是否可以与其它剧情并发(int 0:不能 1:可以 ),直接决定了是否可以做多个动作
/// </summary>
public int IsConcurrency;
/// <summary>
/// 剧情执行的时间,如果是动画就是动画播放时间,如果是path就是移动所需要的事件,对应MovePath中的time(float)
/// 如果是声音、显示等可以是瞬间人物完成了,虽然声音有播放的时间
/// </summary>
public float Time;
/// <summary>
/// 剧情延迟多少秒开始(float)
/// </summary>
public float Delay;
/// <summary>
/// 动画播放速度(float)
/// </summary>
public float AnimSpeed;
/// <summary>
/// 坐下起来起来的距离
/// </summary>
public float SetDis;
}
public List<Story> list { get; set; }
public void Load(string[,] data)
{
this.list = new List<Story>();
int rowCount = data.GetLength(0);
for (int i = 0; i < rowCount; i++)
{
Story story = new Story();
story.INDEX = int.Parse(data[i, 0]);
story.Step = int.Parse(data[i, 1]);
story.Type = int.Parse(data[i, 2]);
story.Object = data[i, 3];
story.AimatorParameter = data[i, 4];
story.Sound = int.Parse(data[i, 5]);
story.Path = int.Parse(data[i, 6]);
story.IsShow = int.Parse(data[i, 7]);
story.SpecialPlot = int.Parse(data[i, 8]);
story.IsConcurrency = int.Parse(data[i, 9]);
story.Delay = float.Parse(data[i, 10]);
story.Time = float.Parse(data[i, 11]);
story.AnimSpeed = float.Parse(data[i, 12]);
story.SetDis = float.Parse(data[i, 13]);
list.Add(story);
}
}
public string[,] LoadCsv(string url)
{
string text_org = File.ReadAllText(Application.streamingAssetsPath + "/Config/" + url);
string text = text_org.Replace("\n", "");
string[] sep = { "\r" };
string[] lineArray = text.Split(sep, StringSplitOptions.RemoveEmptyEntries);
string title = lineArray[0]; //第一行是title
int arrRow = lineArray.Length - 2;
int arrCol = title.Split(',').Length - 1;
string[,] array = new string[arrRow, arrCol];
for (int i = 2; i < lineArray.Length; i++)
{
string[] col = lineArray[i].Split(',');
for (int j = 1; j < col.Length; j++)
{
array[i - 2, j - 1] = col[j];
}
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment