为听众创建协同程序

时间:2018-05-31 13:22:31

标签: c# sorting unity3d swap

我的主要脚本逐个对一组球进行排序,我将这个脚本调用到此,所以当我click this按钮时它将触发排序按钮,它将在示例1-2中等待1进1排序每次交换之间的秒数。但是当我点击立即触发自动排序的按钮时,Coroutine就不会做它想做的事情。

这是未分类的一组球。 Here 有没有什么办法解决这一问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;


namespace Assets
{
    public class Trigger : MonoBehaviour
    {
        public Gameobjects _Gameobjects; 
        public Button e_YourButton;
        public void Start()
        {
            Button btn = e_YourButton.GetComponent<Button>();
            for (int k = 0; k < 4; k++)
            {
                btn.onClick.AddListener(_Gameobjects.TaskOnClick);
                btn.onClick.AddListener(() => { _Gameobjects.Click1(); });
                StartCoroutine(Example());    
            }
        }   

        IEnumerator Example()
        {

           yield return new WaitForSeconds(1);
        }
    }
}

主要脚本;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;


namespace Assets
{
    public class Gameobjects : MonoBehaviour
    {
        public Button s_YourButton;
        [SerializeField]

        public GameObject[] Balls = new GameObject[5];

        public bool Click = false;

        private static int i = 0;
        private int j = i + 1;
        private int increasebyclick = 1;

        private Vector3 posA = Vector3.zero; //Vector3.zero is for initialization
        private Vector3 posB = Vector3.zero; //Vector3.zero is for initialization


        public GameObject[] instantiatedObjects= new GameObject[5];

        void Start()
        {
            Button btn = s_YourButton.GetComponent<Button>();
            //Calls the TaskOnClick method when you click the Button
            btn.onClick.AddListener(TaskOnClick);
            btn.onClick.AddListener(() => { Click1(); });
            Balls[0] = GameObject.Find("5");
            Balls[1] = GameObject.Find("3");
            Balls[2] = GameObject.Find("2");
            Balls[3] = GameObject.Find("4");
            Balls[4] = GameObject.Find("1");
        }


        public void TaskOnClick()
        {

            performInsertionSort(Balls);



        }
        public void Click1()
        {
            i += increasebyclick;
            print(i);
            if (i >= 4)
            {
                i = 0;            }
        }


        private void performInsertionSort(GameObject[] Balls)
        {
            {



                        if (string.Compare(Balls[i].name, Balls[i+1].name) > 0)
                        {
                            GameObject temp = Balls[i];
                            Balls[i] = Balls[i+1];
                            Balls[i+1] = temp;

                            posA = Balls[i].gameObject.transform.position;
                            posB = Balls[i + 1].gameObject.transform.position;
                            Balls[i].gameObject.transform.position = posB;
                            Balls[i + 1].gameObject.transform.position = posA;


                            }
                }
            }       
        }
    }

1 个答案:

答案 0 :(得分:1)

你的第二个脚本(Gameobjects非常糟糕命名btw)应该是这样的:

// Don't start another sort while the current isn't finished
// Or: stop the current one with StopAllCoroutines()
bool isSorting = false;

public void TaskOnClick()
{
    if(isSorting == false)
    {
        isSorting = true;
        StartCoroutine(PerformInsertionSort());
    }
}

private IEnumerator PerformInsertionSort()
{
    for(int i = 0; i < Balls.Length - 1; i++)
    {      
        if (string.Compare(Balls[i].name, Balls[i+1].name) > 0)                   
        {
            GameObject temp = Balls[i];
            Balls[i] = Balls[i+1];
            Balls[i+1] = temp;

            posA = Balls[i].gameObject.transform.position;
            posB = Balls[i + 1].gameObject.transform.position;
            Balls[i].gameObject.transform.position = posB;
            Balls[i + 1].gameObject.transform.position = posA;

            // If you want to wait only after a switch actually happend,
            // wait here.
        }

        // This is where you need to wait:
        yield return new WaitForSeconds(1f);
    }

    isSorting = false;
}

您排序的数组是脚本的成员,因此将其作为参数传递给脚本内的函数是没有意义的。