Mik*_*ink 5 c# unity-game-engine
你怎么能在一个非Monobehaviour阶级的实例中传递Monobehaviour?我找到了这个链接,TonyLi提到你可以通过一个Monobehaviour来启动和停止一个类的实例中的协同程序,但他没有说明你怎么能这样做.他做了这个theEvent.StartEvent(myMonoBehaviour); 但是他并没有表明他从哪里获得了我的神经行为.我在互联网上环顾四周,但我似乎无法找到.
这是我想要做的.我想在一个类的实例中运行一个协同程序.我还希望能够在类的实例中停止协程.我想这样做,以便我的场景中没有任何具有大型管理器的对象,并且我可以将代码重用于我想以这种方式打乒乓的任何对象.代码在一个方向上移动一个Gameobject然后休息一下并将其向另一个方向移动并再次休息等等.但我不能从课外开始协同程序.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent (typeof(Image))]
public class SpecialBar : MonoBehaviour {
public float rangeX;
public float breakTime;
public float step;
float startProgress = 0.5f;
PingPongGameObject pingPonger;
Color[] teamColors = new Color[]{new Color(255,136,0),new Color(0,170,255)};
void Start()
{
for(int i = 0; i < teamColors.Length; ++i)
{
teamColors[i] = StaticFunctions.NormalizeColor (teamColors[i]);
}
pingPonger = new PingPongGameObject (gameObject.transform.position,
new Vector3(rangeX,0.0f,0.0f),
gameObject,
startProgress,
breakTime,
step
);
}
}
Run Code Online (Sandbox Code Playgroud)
第二堂课是我的协程所在的地方.
public class PingPongGameObject
{
float step;
Vector3 center;
Vector3 range;
GameObject ball;
float progress;
float breakTime;
Vector3 endPos;
Vector3 oppositePosition;
public PingPongGameObject(Vector3 _center, Vector3 _range, GameObject _ball, float _startProgress, float _breakTime, float _step)
{
center = _center;
range = _range;
ball = _ball;
progress = _startProgress;
breakTime = _breakTime;
step = _step;
endPos = center - range;
oppositePosition = center + range;
// This is where I want to start the coroutine
}
public IEnumerator PingPong()
{
while (progress < 1) {
progress += Time.deltaTime * step;
Vector3 newPos = Vector3.Lerp (oppositePosition, endPos, progress);
ball.transform.position = newPos;
yield return null;
}
Vector3 temp = endPos;
endPos = oppositePosition;
oppositePosition = temp;
progress = 0;
yield return new WaitForSeconds (breakTime);
yield return null;
}
public float Step
{
set{step = value;}
}
public void StopCoroutine()
{
// This is where I want to stop the coroutine
}
}
Run Code Online (Sandbox Code Playgroud)
Pro*_*mer 11
TonyLi提到你可以通过一个Monobehaviour来启动和停止一个类的实例中的协同程序,但他没有说明你如何做到这一点.他这样做了
你可以用this关键字做到这一点.this关键字将获取当前实例MonoBehaviour.
在这个例子中有一棵树,恰好有一个组件MonoScript:
MonoScript如果它想要的那个特定实例(因为它是ac#程序)实例化一般的c#类,NonMonoScript:
传递的类别MonoBehaviour:
public class MonoScript : MonoBehaviour
{
void Start()
{
NonMonoScript nonMonoScript = new NonMonoScript();
//Pass MonoBehaviour to non MonoBehaviour class
nonMonoScript.monoParser(this);
}
}
Run Code Online (Sandbox Code Playgroud)
接收传递MonoBehaviour实例的类:
public class NonMonoScript
{
public void monoParser(MonoBehaviour mono)
{
//We can now use StartCoroutine from MonoBehaviour in a non MonoBehaviour script
mono.StartCoroutine(testFunction());
//And also use StopCoroutine function
mono.StopCoroutine(testFunction());
}
IEnumerator testFunction()
{
yield return new WaitForSeconds(3f);
Debug.Log("Test!");
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以将函数的mono引用存储在monoParser要重用的局部变量中.