Опитвам се да използвам действия и събития, изброители и корутини, но ставам толкова объркан.
Целта: Накарайте MoveCubes() да се изпълнява веднъж за 3 секунди и след това да накарате LerpSine() да се изпълнява веднъж за 3 секунди.
Секундите се проследяват от CountdownTimer.cs
. Тогава SkinnedMeshSpawn.cs
взаимодейства с него.
Таймер за обратно броене:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CountdownTimer : MonoBehaviour
{
float currentTime = 0f;
float startingTime = 10f;
public static event Action RaiseReady;
public static event Action RaiseReady2;
public SkinnedMeshSpawn SkinnedMeshSpawn;
void Start()
{
currentTime = startingTime;
StartCoroutine(UpdateCoroutine());
}
IEnumerator UpdateCoroutine()
{
while (true)
{
SkinnedMeshSpawn.GetComponent<SkinnedMeshSpawn>().MoveCubes();
currentTime -= 1 * Time.deltaTime; //does it each frame
int n = Convert.ToInt32(currentTime);
if (n == 7)
{
RaiseReady?.Invoke();
RaiseReady = null; // clean the event
yield break; // Kills the coroutine
}
yield return new WaitForFixedUpdate();
}
}
IEnumerator UpdateCoroutine2()
{
while (true)
{
SkinnedMeshSpawn.GetComponent<SkinnedMeshSpawn>().LerpSine();
currentTime -= 1 * Time.deltaTime; //does it each frame
int n = Convert.ToInt32(currentTime);
if (n == 4)
{
RaiseReady2?.Invoke();
RaiseReady2 = null; // clean the event
yield break; // Kills the coroutine
}
yield return new WaitForFixedUpdate();
}
}
}
SkinnedMeshSpawn:
using UnityEngine;
[RequireComponent(typeof(SkinnedMeshRenderer))]
public class SkinnedMeshSpawn : MonoBehaviour
{
public GameObject CubePrefab;
public GameObject[] objects;
SkinnedMesh mesh;
public Rigidbody cubePrefabRb;
public Vector3[] verts;
int runOnce = 1;
void Awake()
{
mesh = GetComponent<SkinnedMesh>();
verts = new Vector3[mesh.vertexCount];
}
void Start()
{
verts = mesh.vertices;
CountdownTimer.RaiseReady += CountdownTimer_RaiseReady;
CountdownTimer.RaiseReady2 += CountdownTimer_RaiseReady2;
mesh.OnResultsReady += DrawVertices;
}
void CountdownTimer_RaiseReady()
{
Debug.Log("Done");
CountdownTimer.RaiseReady -= CountdownTimer_RaiseReady; // Remove listener though the other class is already clearing it
}
private void CountdownTimer_RaiseReady2()
{
Debug.Log("Done2");
CountdownTimer.RaiseReady2 -= CountdownTimer_RaiseReady; // Remove listener though the other class is already clearing it
}
void DrawVertices(SkinnedMesh mesh)
{
if (runOnce == 1)
{
for (int i = 0; i < mesh.vertexCount; i++)
{
Vector3 position = verts[i];
var cubeClone = Instantiate(CubePrefab, position, transform.rotation);
cubeClone.tag = "CubePFInst";
}
runOnce = 0;
}
}
public void MoveCubes()
{
if (runOnce == 0)
{
objects = GameObject.FindGameObjectsWithTag("CubePFInst");
for (int i = 0; i < mesh.vertexCount; i++)
{
Vector3 position = verts[i];
cubePrefabRb = objects[i].GetComponent<Rigidbody>();
cubePrefabRb.MovePosition(Vector3.Lerp(position, position, Time.deltaTime * 6));
}
}
}
public void LerpSine()
{
Debug.Log("LerpSine");
}
}
Най-многото, което успях да постигна досега, е да накарам UpdateCoroutine2
да бъде извикан и да накарам Done2
да се отпечата в журнала, но LerpSine
се отпечатва многократно, сякаш стартирам съпрограмата с всяка актуализация на рамка.
Ще съм благодарен, ако някой може да прегледа кода и да даде предложения как да се постигне целта, посочена по-горе. По-късно исках да внедря GameManager, но това вече става много сложно.
UpdateCoroutine2
. - person Ruzihm   schedule 10.07.2019UpdateCoroutine2
, когато достигне определено време, но това също го извика многократно по някаква причина. Ще се опитам да го напиша отново как го имах в последния си опит, но не знам как да се опитам да променя нещата. - person rashapou   schedule 11.07.2019while
цикъл, който повтаря всяка фиксирана актуализация за 3 секунди... - person Ruzihm   schedule 11.07.2019