unity中使用携程 Coroutine CoroutineNow; void OnStart(string path) { Debug.Log($"空引用追踪 5:{path}"); OnCloseText(true); Debug.Log($"空引用追踪 7:{path}"); if (CoroutineNow is null) { } else { StopCoroutine(CoroutineNow); } CoroutineNow = StartCoroutine(Type(path)); // 开始打字效果 } IEnumerator Type(string filePath) {方法。进入不了携程
时间: 2024-02-10 08:29:38 浏览: 24
从您提供的代码来看,您调用了StartCoroutine(Type(path))来启动携程。但是,您说进入不了携程,这可能是因为您没有在Type方法中使用yield return语句来控制携程执行的过程。
在Unity中,使用携程时,需要在携程方法中使用yield return语句来控制携程的执行过程。例如,您可以使用yield return new WaitForSeconds(1.0f)语句来让携程暂停1秒钟,然后继续执行。
所以,您需要在Type方法中使用yield return语句来控制携程的执行过程,否则携程将无法进入。例如,您可以使用如下代码:
IEnumerator Type(string filePath)
{
// 执行一些操作
yield return new WaitForSeconds(1.0f); // 暂停1秒钟
// 继续执行一些操作
}
希望这能帮助到您!
相关问题
在unity中使用unitask库实现这个脚本:public class Test : MonoBehaviour { // Start is called before the first frame update void Start() { Debug.Log("测试 1"); StartCoroutine(Type("huihiuhi")); // 开始打字效果 Debug.Log("测试 2"); } IEnumerator Type(string name) { Debug.Log($"进入协成:{name}"); yield return new WaitForSeconds(0.2f); // 等待一定时间 } }
可以使用Unitask库中的`UniTask.Delay`方法来替换`yield return new WaitForSeconds`,从而实现异步等待的效果。修改后的代码如下:
```csharp
using System.Collections;
using Cysharp.Threading.Tasks;
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
async void Start()
{
Debug.Log("测试 1");
await Type("huihiuhi"); // 开始打字效果
Debug.Log("测试 2");
}
async UniTaskVoid Type(string name)
{
Debug.Log($"进入协程:{name}");
await UniTask.Delay(200); // 等待一定时间
}
}
```
需要注意的是,由于使用了`async/await`语法,`Start`方法需要被声明为`async void`。同时,`Type`方法需要被声明为`async UniTaskVoid`,表示返回值为`UniTask.Void`,因为使用了`await`语法。
unity中 private void OnMouseUp() { if (EventSystem.current.IsPointerOverGameObject()) { //Debug.Log("EventSystem.current.IsPointerOverGameObject"); } else { //先关闭所有 OpenManager.Instance.CloseShowUI(); Debug.Log("空引用追踪 11"); foreach (var da in ShowTypes) { Debug.Log("空引用追踪 22"); //再调用展示方法 //OpenManager.Instance.ShowMessage(da.my_ShowType, System.IO.Path.Combine(Application.streamingAssetsPath, da.path)); OpenManager.Instance.ShowMessage(da.my_ShowType, da.path); } } }这个函数修改为安卓可用
您可以把 OnMouseUp() 方法修改为 OnPointerUp() 方法,这个方法在安卓平台和其他移动设备上是有效的。同时,您需要使用 EventSystem.current.currentSelectedGameObject 来检测当前是否有 UI 元素被选中,代码如下:
```csharp
using UnityEngine.EventSystems;
private void OnPointerUp(PointerEventData eventData)
{
if (EventSystem.current.IsPointerOverGameObject(eventData.pointerId))
{
//Debug.Log("EventSystem.current.IsPointerOverGameObject");
}
else
{
//先关闭所有
OpenManager.Instance.CloseShowUI();
Debug.Log("空引用追踪 11");
foreach (var da in ShowTypes)
{
Debug.Log("空引用追踪 22");
//再调用展示方法
//OpenManager.Instance.ShowMessage(da.my_ShowType, System.IO.Path.Combine(Application.streamingAssetsPath, da.path));
OpenManager.Instance.ShowMessage(da.my_ShowType, da.path);
}
}
}
```
注意,您需要在代码文件的开头添加 using UnityEngine.EventSystems; 来引用 EventSystem 类。