C# 使用array.Copy将数组中的项上移到数组中

C# 使用array.Copy将数组中的项上移到数组中,c#,arrays,unity3d,C#,Arrays,Unity3d,尝试在Unity Monodevelop环境中使用Array.Copy,具体来说,我要做的是将数组的第一个插槽中的值移动到holder变量中,然后将数组中的每个值向前移动一个插槽,然后将holder变量中的值移回最后一个插槽中的数组中。我的相关代码如下: using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class TurnController

尝试在Unity Monodevelop环境中使用Array.Copy,具体来说,我要做的是将数组的第一个插槽中的值移动到holder变量中,然后将数组中的每个值向前移动一个插槽,然后将holder变量中的值移回最后一个插槽中的数组中。我的相关代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class TurnController : MonoBehaviour {

//Array will hold all units (Being that they all have the EidolonClass script attached), then will be sorted by Speed, descending
private EidolonClass[] AllUnitArray;

...

void Awake(){
   //Find anything with the EidolonClass and then add it to the Array
   AllUnitArray = FindObjectsOfType (typeof(EidolonClass)) as EidolonClass[];
   //Sort the array by speed, descending (Highest speed listed first)
   Array.Sort (AllUnitArray, delegate(EidolonClass one, EidolonClass two) {
          return two.Speed.CompareTo(one.Speed);
          });
}

void pushArray(){
        EidolonClass store = AllUnitArray [0];
        for(int i=1;i<=AllUnitArray.Length;i++){
            Array.Copy (AllUnitArray, i, AllUnitArray, i-1, AllUnitArray.Length-1);
        }
        AllUnitArray [AllUnitArray.Length] = store;
        for(int i=0;i<=AllUnitArray.Length;i++) {
            Debug.Log (AllUnitArray[i].name.ToString ());
        }
    }

void Update () {
        if (Input.GetKeyDown (KeyCode.K)) {
            pushArray ();
        }
    }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用制度;
公共类控制器:单行为{
//数组将容纳所有的单元(因为它们都附加了EidolonClass脚本),然后按速度降序排序
私有的EidolonClass[]AllUnitArray;
...
无效唤醒(){
//查找具有EIDOLON类的任何内容,然后将其添加到数组中
AllUnitArray=findObjectSoftType(typeof(EidolonClass))作为EidolonClass[];
//按速度降序排列阵列(首先列出最高速度)
Sort(AllUnitArray,委托(EidolonClass 1,EidolonClass 2){
返回2.Speed.CompareTo(1.Speed);
});
}
void pushArray(){
EidolonClass store=AllUnitArray[0];

对于(int i=1;i发生异常是因为您尝试多次复制相同的长度,但每次都有新的起始偏移量。要移动数组的内容,只需调用
array.copy()

大概是这样的:

void pushArray(){
    EidolonClass store = AllUnitArray [0];

    Array.Copy (AllUnitArray, 1, AllUnitArray, 0, AllUnitArray.Length - 1);
    AllUnitArray[AllUnitArray.Length - 1] = store;

    for(int i=0;i<=AllUnitArray.Length;i++) {
        Debug.Log (AllUnitArray[i].name.ToString ());
    }
}
void pushArray(){
EidolonClass store=AllUnitArray[0];
复制(AllUnitArray,1,AllUnitArray,0,AllUnitArray.Length-1);
AllUnitArray[AllUnitArray.Length-1]=存储;

对于(int i=0;iwell,您可以更改起始偏移量,但始终使用相同的长度。这是行不通的。PS为什么循环,您肯定只需要执行1次复制基本上这是一个回合管理器。当该回合结束时(或出于测试目的,当我点击K时),第0个插槽中的任何内容都处于当前回合我需要整个数组移动,这样所有的东西都保持有序,并且在下一次旋转时继续工作。基本上,你想将数组中的每个对象向上移动/移动一次。那么第一个对象现在应该在最后一个索引中?基本上,是的。检查
shiftUp()
功能来自重复问题的答案。这正是你想要的,甚至标题也说明了一切。由于时间紧迫,我可能会同意你的第二个建议,只使用索引。出于好奇,我将如何使用该系统或队列系统来实现高级轮换顺序,例如,如果一个单位有mor如果速度是下一个最高速度单位的两倍,他们可以连续转两圈?