【问题标题】:How to copy one array items to another array in unity Using C#? [closed]如何使用 C# 将一个数组项统一复制到另一个数组? [关闭]
【发布时间】:2019-01-16 11:18:09
【问题描述】:

我有两个长度相同的 GameObject 数组。我正在尝试将 array1 的相同值复制到 array2。 我尝试使用 system.Array.copy(Array1,Array2,4) 并且还尝试了 Array1= Array2 但不工作。 这两个数组包含 4 个按钮,每个按钮都有子文本。我想显示分配给这些按钮的 4 个答案,并同时将相同的答案复制到另外 4 个按钮。有点像双重播放器。 有人可以帮忙吗?

public class DuelMode : MonoBehaviour{
public static DuelMode instance;

// these are the question values a and b
private int a, b, a1, b1;

//the variable for answer value
[HideInInspector] public int answer;

//varible whihc will assign ans to any one of the 4 answer button
private int locationOfAnswer;

//ref to the button
public GameObject[] ansButtons;
private GameObject[] ansButtonsDuel;

//ref to image symbol so player can know which operation is to be done
public Image mathSymbolObject;

//ref to all the symbol sprites whihc will be used in above image
public Sprite[] mathSymbols;

//get the tag of button 
public string tagOfButton;

//ref to text in scene where we will assign a and b values of question
public Text valueA, valueB, valueA1, valueB1;
void Awake()
{
    MakeInstance();
}

//method whihc make this object instance
void MakeInstance()
{
    if (instance == null)
    {
        instance = this;
    }
}

//at start we need to do few basic setups
void Start()
{
    //we put the location value in tag of button variable
    tagOfButton = locationOfAnswer.ToString();

    MathsProblem();
}

//this method keeps the track of mode 

// Update is called once per frame
void Update()
{
    tagOfButton = locationOfAnswer.ToString();

}


//Below code is for maths calculation

public void MathsProblem()
{
    bool roundActive = true;

}


//this methode perform Multiplication process
void MultiplicationMethod()
{
    bool reloop;
    bool[] numbers = new bool[301];

    b = b1 = Random.Range(1, 10);

    locationOfAnswer = Random.Range(0, ansButtons.Length);


    answer = a * b;
    numbers[answer] = true;

    if (valueA != null && valueB != null && valueA1 != null && valueB1 != null)
    {
        valueA.text = a.ToString();
        valueB.text = b.ToString();
        valueA1.text = a.ToString();
        valueB1.text = b.ToString();
    }
    mathSymbolObject.sprite = mathSymbols[0];

    for (int i = 0; i < ansButtons.Length; i++)
    {
        if (i == locationOfAnswer)
        {
            ansButtons[i].GetComponentInChildren<Text>().text = "" + answer;

        }
        else
        {
            // the below code make sure that all the values assigned to the ans button are within the range

            if (a * b <= 100)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(1, 101);
            }
            else if (a * b <= 200 & a * b > 100)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(101, 201);
            }
            else if (a * b <= 300 & a * b > 200)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(201, 301);
            }
            else if (a * b <= 400 & a * b > 300)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(301, 401);
            }

            while (ansButtons[i].GetComponentInChildren<Text>().text == "" + answer)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(1, 401);
            }
        }

    }

}

}

【问题讨论】:

  • 您能否发布更多代码以便为您提供帮助
  • 你看过Object.Instantiate()吗?您应该能够在循环中使用它来将每个元素克隆到一个新数组中。
  • 那些数组只保存对 GameObjects 的引用......所以将引用复制到一个新数组中对于拥有更多 GameObjects 没有多大帮助......见上文
  • 是的,正在更新我的代码
  • @Sarita 你能把它分解到基本部分吗?我也看不到你试图复制/克隆数组的位置

标签: c# arrays unity3d


【解决方案1】:

我假设当您为 ansButtons 设置完文本后,您现在只想在两侧显示相同的文本,对吧?

for(int i = 0; i < ansButtons; i++)
{
    // get the GameObjects
    var ansButton = ansButtons[i];
    var ansDuelButton = ansButtonsDuel[i];

    // get the Text components 
    var ansText = ansButton.GetComponentInChildren<Text>(true);
    var duelText = ansDuelButton .GetComponentInChildren<Text>(true);

    // copy the text over
    duelText.text = ansText.text;
}

我当然假设您在 ansButtonsansButtonsDuel 设置中拥有所有引用,例如通过检查器或 Instantiate 对象。

【讨论】:

  • 好的,所以我不必实际复制数组元素。只需要分配给另一个数组。
  • 好吧,我当然假设您在 ansButtonsansButtonsDuel 设置中拥有所有参考资料,例如通过检查器或在生成(实例化)对象时。比是的
  • 现在我可以在另外 4 个按钮上获取数字,但它们与 ansButtons 值不同。我的意思是我得到 8 个不同的值而不是 4 个
  • 我看不出这些应该来自哪里,因为您将最终数字复制为字符串...您必须将 for 循环放在 之后 @987654329 上方在将所有文本设置为ansButtons 之后,您已经拥有了@loop
  • 是的,我也是这么做的。在我的上方输入了这个新的 for 循环。
【解决方案2】:

请您发布您的代码和对象数组的示例。这将帮助我们了解问题,以便我们可以解决问题。

你也可以看看下面的帖子。它可能对您的事业有所帮助。

Difference Between Array.CopyTo() and Array.CloneTo()

编辑:

    var foo = new String[] {"ab" , "cd", "ef"} ;
    var bar = new List<string>();

    foreach(var item in foo)
    {
        bar.Add(item);
    }
    var barArray = bar.ToArray();

    for(int i = 0; i < foo.Length; i++)
    {
        Console.WriteLine(foo[i]);
        Console.WriteLine(barArray[i]);
    }

上面的代码使用了列表和数组,因为这样你就不必索引你的重复数组了。您可以在 dot net fiddle 中运行代码以查看输出。我在这个例子中使用了字符串,请用你的对象替换。

【讨论】:

  • 根本问题是 GameObject[] 仅包含对现有 GameObjects 的引用 .. OP 的问题听起来像是他想要实际复制/克隆游戏对象而不仅仅是引用
  • 如果是这种情况,那么 OP 将不得不循环并将数组 1 中的每个项目添加到新数组 Array2 中。理想情况下,我们需要一个关于他们的代码和预期结果的示例。
  • 是的,这就是为什么你的帖子不应该是一个答案,而是一个评论(你必须等到你有足够的声誉才能在这个问题上发布 cmets)
  • 我只是在更新答案,伙计们,抓紧你们的马
猜你喜欢
  • 2022-07-05
  • 1970-01-01
  • 2011-05-08
  • 2014-08-24
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 2014-10-24
相关资源
最近更新 更多