# 数字分解成数组

1.读取外部数据

if (File.Exists("array.Config"))
{
string arr = sr.ReadLine().Trim().ToString();
sr.Close();
numbers = JsonConvert.DeserializeObject<List<int>>(arr);
}
2.数字拆解方法
List<int> numbers = new List<int>();//数组在初始时获取
List<int> ResoleNum(int number)
{
int index = 0;//数组下标
int i = 0;//大小缓存
int value = 0;//当前相加数字
List<int> resoleNum = new List<int>();
int sign = number > 0 ? 1 : -1;//判断参数正负，因为电机需要下降所以需要添加负数
number = Mathf.Abs(number);
while (i < number)
{
if (index < numbers.Count)
{
value = numbers[index];
index++;
}
else
{
value = numbers[numbers.Count - 1];
}
if ((i + value) > number)
{
value = number - i;
}
i += value;
//Debug.Log(value);
}
return resoleNum;
}
3.调用出资拆解并每帧发送给伺服电机
IEnumerator Movement(int target1,int target2,int target3) {
List<int> nums1 = ResoleNum(target1-Value_A);
List<int> nums2 = ResoleNum(target2-Value_B);
List<int> nums3 = ResoleNum(target3-Value_D);
List<List<int>> moves = new List<List<int>>();
moves.OrderByDescending(m=>m.Count);
int index = 0;
while (index<moves[0].Count)
{
if (nums1.Count!=0&&index<nums1.Count)
Value_A = Value_C += nums1[index];
if (nums2.Count != 0 && index < nums2.Count)
Value_B = Value_F += nums2[index];
if (nums3.Count != 0 && index < nums3.Count)
Value_D = Value_E += nums3[index];
Debug.Log("Value_A:"+ Value_A+ "    Value_B:"+ Value_B+ "    Value_D:"+ Value_D);
Work1();
index++;
yield return new WaitForEndOfFrame();
}
}

posted on 2021-10-09 18:51  陌洛  阅读(41)  评论(0编辑  收藏  举报