以前没怎么用过文件读入和输出,现在项目中用到这个必须要学学。学长给简单讲解了一下,发现不是很难,表示出文件路径,然后用File.ReadAllLines()方法就搞定了。当然读入的数据是一字符串的形式存储到了字符串数组里面的。想要使用的话还要根据具体情况进行一下处理。比如使用字符串分割、将字符串转化为整形数据等等。
string[] _str = File.ReadAllLines(@"C:\Users\liu_baoxi\Desktop\path2.txt");
这里是项目中用到的文件读入方法,很简单,不再多说。这里想记录一下当时在处理字符串是用到的一些小技巧,
_currentIndex = 0;
_positions = new List<Vector3>();
for (Int32 i = 0; i < _str.Length; i++)
{
if (String.IsNullOrEmpty(_str[i]))
{
break;
}
string[] _numValue = _str[i].Split(' ');
int _x, _y, _z;
_x = int.Parse(_numValue[0]);
_y = int.Parse(_numValue[1]);
_z = int.Parse(_numValue[2]);
_positions.Add(new Vector3(_x, _y, _z));
}
_personMovePoint = _positions[0];
_targetPoint = _positions[1];
本来用数组存储节点数据的,学长给改成了用list,确实list比较方便一些。当然文件读入还不是最重要的,重要的是实现人物运动,思想是计算关键点之间的方向向量,每次在方向向量上加上人物移动的步长,如果当前的位置和下一个关键点之间的距离小于某一数值,我们就认为人物已经到了下一个关键点,同时把当前位置重置为关键点位置。如果当前位置被认为还没有到达下一个关键点,那么久将当前位置在向前移动。
if (GetDistance(_personMovePoint, _targetPoint) >= 2.0)
{
_forward = _positions[_currentIndex + 1] - _positions[_currentIndex];
_forward.Normalize();
_personMovePoint = _personMovePoint + _forward*2;
}
else
{
if (_currentIndex == _positions.Count - 2)
{
_currentIndex = 0;
_personMovePoint = _positions[0];
_targetPoint = _positions[_currentIndex + 1];
}
else
{
_personMovePoint = _targetPoint;
_currentIndex++;
_targetPoint = _positions[_currentIndex + 1];
}
}