gpt4 book ai didi

c# - 当类不是 MonoBehaviour 时启动协程

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:44 25 4
gpt4 key购买 nike

<分区>

编辑: 解决这个问题的答案确实在帖子的答案中,该帖子的副本就是这个。如果您遇到与标题相同的问题,请参阅其他帖子。

我制作了一个简单的装备,可以像这样随着时间移动东西:

 Transform from;
Transform to;
float overTime;
IUIAnimationEvent chain;
public delegate void UIchain();
public event UIchain NEXT_FUNCTION;
public MoveAction(Transform from, Transform to, float overTime, IUIAnimationEvent chain)
{
this.from = from;
this.to = to;
this.overTime = overTime;
this.chain = chain;
}
public void Move()
{
MonoBehaviour _lead = new MonoBehaviour();
if (moveRoutine != null)
{
_lead.StopCoroutine(moveRoutine);
}
moveRoutine = _Move(from, to, overTime);
_lead.StartCoroutine(moveRoutine);
}
IEnumerator _Move(Transform from, Transform to, float overTime)
{
Vector2 original = from.position;
float timer = 0.0f;
while (timer < overTime)
{
float step = Vector2.Distance(original, to.position) * (Time.deltaTime / overTime);
from.position = Vector2.MoveTowards(from.position, to.position, step);
timer += Time.deltaTime;
yield return null;
}
if(NEXT_FUNCTION != null)
{
NEXT_FUNCTION();
}
}

但是,为了让它像我希望的那样工作,我必须实例化它们,所以它们不能是MonoBehaviour。请注意我对 _lead 变量做了什么。我做到了,所以我可以像任何其他程序一样启动 coroutines。如果我的类不是 MonoBehaviour,如何从它启动一个coroutine

或者如果这不可能,如何实例化 MonoBehaviour 的类?我注意到 _use AddComponent 而不是,但这些类不是 组件。它们被另一个组件使用,不会被放在检查器的 GameObject 上。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com