Jak możesz przekazać Monobehaviour wewnątrz instancji klasy innej niż Monobehaviour? Znalazłem to: link, w którym TonyLi wspomina, że możesz przekazać Monobehaviour, aby rozpocząć i zatrzymać kontury wewnątrz instancji klasy, ale nie pokazuje, jak możesz to zrobić. Robi to theEvent.StartEvent (myMonoBehavour); ale on nie pokazuje, skąd pochodzi mójMonobehavour. Rozglądałem się w Internecie, ale nie potrafię znaleźć odpowiedzi.Użyj coroutine wewnątrz klasy innej niż MonoBehaviour
- Edit
Oto co próbuję zrobić. Chcę uruchomić coroutine wewnątrz instancji klasy. Chciałbym również móc powstrzymać coroutine wewnątrz instancji klasy. Chcę to zrobić w ten sposób, że nie mam żadnych obiektów w mojej scenie, które mają dużych menedżerów, a także, że mogę ponownie użyć kodu dla dowolnego obiektu, który chcę pingpong w ten sposób. Kod przesuwa obiekt Gameobject w jednym kierunku, a następnie robi przerwę i przesuwa go w innym kierunku, i znowu robi przerwę itp. Ale nie mogę uruchomić korupcji spoza klasy.
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
);
}
}
Druga klasa to gdzie mój współprogram jest.
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
}
}
Tak, na chwilę zadam moje pytanie. –
Przebudowałem to pytanie. –
Sprawdź odpowiedź, którą zostawiłem. Możesz użyć słowa kluczowego 'this'. – Programmer