为监听器创建协程
Creating Coroutine for Listeners
我的主脚本对一组球进行 1 对 1 排序,我正在调用该脚本,所以当我按下 click this 按钮时,它将触发排序按钮,它将通过等待示例 1 进行 1 对 1 排序每次交换之间 -2 秒。但是,当我单击用于立即触发自动排序的按钮时,协程不会执行它应该执行的操作。
这是一组未分类的球。 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;
}
}
}
}
}
你的第二个脚本(顺便说一句,Gameobjects
命名很糟糕)应该看起来像这样:
// 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;
}
您排序的数组是脚本的成员,因此将其作为参数传递给脚本内的函数是没有意义的。
我的主脚本对一组球进行 1 对 1 排序,我正在调用该脚本,所以当我按下 click this 按钮时,它将触发排序按钮,它将通过等待示例 1 进行 1 对 1 排序每次交换之间 -2 秒。但是,当我单击用于立即触发自动排序的按钮时,协程不会执行它应该执行的操作。
这是一组未分类的球。 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;
}
}
}
}
}
你的第二个脚本(顺便说一句,Gameobjects
命名很糟糕)应该看起来像这样:
// 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;
}
您排序的数组是脚本的成员,因此将其作为参数传递给脚本内的函数是没有意义的。