0
votes

Comment puis-je attendre ou servir dans Ienumerator et continuer la Coroutin?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectsManipulation : UnityEngine.MonoBehaviour
{
    //Camera
    public Camera playerCamera;

    //Scaling
    public bool canScale = true;
    private Scaling scaling;

    //Lights
    public DimLights dimlights;
    private Coroutine lightCoroutine;

    //Colors
    private Colors colors;

    //Rotating
    public bool stopRotation = false;
    private Rotating rotating;

    private void Start()
    {
        scaling = GetComponent<Scaling>();
        scaling.Inits();

        colors = GetComponent<Colors>();
        colors.Start();

        rotating = GetComponent<Rotating>();
    }

    // Use this for initialization
    void Update()
    {
        if (playerCamera != null)
        {
            //Scaling
            if (Input.GetKeyDown(KeyCode.F) && canScale == true)
            {
                Scaling();
            }
        }

        //Rotate
        if (Input.GetKey(KeyCode.R) && !scaling.scaleUp)
        {
            rotating.x += Time.deltaTime * rotating.rotationSpeed;
            scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
            rotating.keyPressed = true;
        }
        if (Input.GetKeyUp(KeyCode.R))
        {
            rotating.keyPressed = false;
        }

        if (!rotating.keyPressed && !scaling.scaleUp && rotating.rotateBack == false
            && DetectInteractable.detected == false)
        {
            scaling.objectToScale.transform.rotation = Quaternion.LookRotation(playerCamera.transform.forward);
        }

        if (DetectInteractable.detected == true && !scaling.scaleUp && stopRotation == false)
        {
            rotating.x += Time.deltaTime * rotating.rotationSpeed;
            scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
        }
    }

    public void Scaling()
    {
        //Flip the scale direction when F key is pressed
        scaling.scaleUp = !scaling.scaleUp;

        //Stop old coroutine
        if (scaling.scaleCoroutine != null)
            StopCoroutine(scaling.scaleCoroutine);

        if (lightCoroutine != null)
            StopCoroutine(lightCoroutine);


        //Scale  up
        if (scaling.scaleUp)
        {
            //Start new coroutine and scale up within 5 seconds and return the coroutine reference
            rotating.rotateBack = false;
            scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.maxSize, scaling.duration, playerCamera));
            if (dimlights.lightsOnOff == false)
                lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(1, scaling.duration));
        }

        //Scale Down
        else
        {
            //Start new coroutine and scale up within 5 seconds and return the coroutine reference
            rotating.rotateBack = true;
            scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.minSize, scaling.duration, playerCamera));
            if (dimlights.lightsOnOff == false)
                lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(0, scaling.duration)); ;
        }
    }
}

0 commentaires

3 Réponses :


1
votes

Cela devrait faire ce que vous recherchez:

public IEnumerator NaviScaling()
{
    if (scaling.objectToScale.transform.localScale == scaling.minSize)
    {
        op.Scaling();
    }

    while (scaling.objectToScale.transform.localScale != scaling.maxSize)
    {
        yield return null;
    }

    conversationTrigger.PlayConversations();

    while (!conversationTrigger.conversationEnd)
    {
        yield return null;
    }

    op.Scaling();
}


0 commentaires

2
votes

Pour utiliser Patintille , vous devrez fournir un moyen de vérifier la condition qu'elle attend. Cela se fait en faisant passer un délégué dans son constructeur.

Alors d'attendre pendant que votre objet est toujours à l'échelle, vous feriez quelque chose comme ceci: xxx

notez le () => Avant la condition, cela transforme votre expression dans une fonction anonyme qui permet au Watile de réévaluer la condition sur chaque image.

Vous pouvez également transmettre une méthode en tant que délégué qui lit assez bien! xxx


0 commentaires

1
votes

Je ne suis pas très heureux avec les réponses jusqu'à présent.

Fondamentalement, la question était de savoir comment utiliser servultille code> qui n'était même pas utilisé par le réponse acceptée a> (qui à la hausse a fourni un code vraiment messi ..) p>

la raison pour laquelle L'autre a > Pourraient toujours être étendue, c'est que les deux réponses sont basées sur l'utilisation de == Code> pour vérifier l'égalité. Pour vector3 code>, mais cela suppose simplement que p> xxx pré>

ceci est généralement idéal pour les distances entre les positions mais pour les échelles, vous risquez de vouloir plus de valeurs plus précises. p>

Si c'est le but et que vous n'en avez pas besoin d'être plus précis que de vous coller. P>

sinon je ferais le contrôle plutôt en utilisant mathf.approxement code> , par exemple En tant que Méthode d'extension comme p>

public IEnumerator NaviScaling()
{
    // Scale up in 1 second and wait
    yield return op.Scaling(scaling.maxSize, 1f);

    // As you can see again this could be a Coroutine so you could directly yield it
    // instead of having to wait for the bool value to turn true
    conversationTrigger.PlayConversations();

    yield return new WaitUntil(() => conversationTrigger.conversationEnd);

    // Scale down in 1 second and wait
    yield return op.Scaling(scaling.minSize, 1f);
}


1 commentaires

Vous avez raison, j'ai oublié d'ajouter le script OP et de montrer comment ça marche. Alors j'ai ajouté cela maintenant à ma question.