仅当对象完成放大后才能开始对话吗?

时间:2019-05-11 20:01:04

标签: c# unity3d

缩放脚本:

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

public class Scaling : UnityEngine.MonoBehaviour
{
    public GameObject objectToScale;
    public GameObject lookAtTarget;
    public float duration = 1f;
    public Vector3 minSize;
    public Vector3 maxSize;
    public bool scaleUp = false;
    public Coroutine scaleCoroutine;

    [HideInInspector]
    public bool scaledFinishedUp = false;

    [HideInInspector]
    public bool scaledFinishedDown = false;

    public void Inits()
    {
        objectToScale.transform.localScale = minSize;
    }

    public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, Camera objectToScaleCamera)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            if (scaleUp)
            {
                var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
                lookPos.y = 0;
                var rotation = Quaternion.LookRotation(lookPos);
                objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
            }
            else
            {
                var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
                lookPos.y = 0;
                var rotation = Quaternion.LookRotation(objectToScaleCamera.transform.forward);//SwitchCameras.GetCurrentCamera().transform.forward);//Camera.main.transform.forward);
                objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
            }

            yield return null;
        }

        if(objectToScale.transform.localScale.x >= maxSize.x
            && objectToScale.transform.localScale.y >= maxSize.y
            && objectToScale.transform.localScale.z >= maxSize.z)
        {
            scaledFinishedUp = true;
        }

        if (objectToScale.transform.localScale.x <= maxSize.x
            && objectToScale.transform.localScale.y <= maxSize.y
            && objectToScale.transform.localScale.z <= maxSize.z)
        {
            scaledFinishedDown = true;
        }
    }

    public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, float rotationSpeed)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.transform.localScale;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);

            targetObj.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime, Space.Self);

            yield return null;
        }
    }
}

还有对话脚本:

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

public class NaviDialogue : MonoBehaviour
{
    public ObjectsManipulation op;
    public bool scale = true;
    public Scaling scaling;
    public ConversationTrigger conversationTrigger;

    private void Start()
    {
        op.canScale = false;
    }

    private void Update()
    {
        if (scaling == true && DOFControl.hasFinished == true)
        {
            DOFControl.hasFinished = false;
            PlayerController.disablePlayerController = true;
            NaviConversations(0);
        }
    }

    public void NaviConversations(int Index)
    {
        scaling.scaleUp = true;
        op.Scaling(false);
        StartCoroutine(conversationTrigger.PlayConversation(Index));
    }
}

仅在一行之后:

op.Scaling(false);

我想检查对象是否已完成按比例放大,然后启动StartCoroutine。问题在于,在更新中,我仅调用一次NaviConversations。

然后,当StartCoroutine完成时,我想做其他类似的事情:这将缩小对象:

scaling.scaleUp = false;
op.Scaling(false);

最后使用PlayConversatio方法上课:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class ConversationTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();
    public GameObject canvas;

    [HideInInspector]
    public static int conversationIndex;

    public bool conversationEnd = false;

    private bool activateButton = false;
    private DialogueManager dialoguemanager;
    private bool startDialogue = false;

    private void Start()
    {
        conversationIndex = 0;
        dialoguemanager = FindObjectOfType<DialogueManager>();
    }

    public IEnumerator PlayConversation(int index)
    {
        if (conversations.Count > 0 &&
            conversations[index].Dialogues.Count > 0)
        {
            for (int i = 0; i < conversations[index].Dialogues.Count; i++)
            {
                if (dialoguemanager != null)
                {
                    dialoguemanager.StartDialogue(conversations[index].Dialogues[i]);
                }

                while (DialogueManager.dialogueEnded == false)
                {
                    yield return null;
                }
            }

            conversationIndex = index;
            conversationEnd = true;
            canvas.SetActive(false);
            Debug.Log("Conversation Ended");
        }
    }

    public void SaveConversations()
    {
        string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
        File.WriteAllText(@"d:\json.txt", jsonTransform);
    }

    public void LoadConversations()
    {
        string jsonTransform = File.ReadAllText(@"d:\json.txt");
        conversations.Clear();
        conversations.AddRange(JsonHelper.FromJson<Conversation>(jsonTransform));
    } 
}

2 个答案:

答案 0 :(得分:1)

我看不到会发生什么

conversationTrigger.PlayConversation(Index),但您可以在其顶部使用WaitUntil,以使其等待缩放结束。

类似

yield return new WaitUntil(scalingIsDoneCondition);

其中scalingIsDoneCondition是检查缩放是否完成。一旦协程返回true,协程就会继续。


或者,您可以拥有一个额外的IEnumerator,然后在该处等待,例如

public void NaviConversations(int Index)
{
    scaling.scaleUp = true;
    op.Scaling(false);
    StartCoroutine(StartConversationAfterScaling(Index));
}

private IEnumerator StartConversationAfterScaling(int index)
{
    yield return new WaitUntil(scalingIsDoneCondition);

    StartCoroutine(conversationTrigger.PlayConversation(Index));
}

如果您需要从同一个班级检查病情。


另一种形式是使整个缩放更像是public IEnumerator并从另一个缩放中产生。

public void NaviConversations(int Index)
{
    scaling.scaleUp = true;
    StartCoroutine(StartConversationAfterScaling(Index));
}

private IEnumerator StartConversationAfterScaling(int index)
{
    yield return op.Scaling(false);

    StartCoroutine(conversationTrigger.PlayConversation(Index));
}

这需要将op.Scaling也设为IEnumerator

答案 1 :(得分:1)

我什至都没看完你的全部剧本。强烈建议您对这些内容使用补间库。您可以使用这些库轻松缩放,旋转,平移,混合颜色等等。 看看DoTween,使用DoTween可以像这样编写缩放比例:

transform.DoScale(Vector3.one * 2, 1f).onCompelete( ()=>
    {
        print("Scaling finished!");
    }

但是补间并不仅限于设置回调,通过补间,您可以以编程方式为对象设置动画。

您可以查看有关功能的快速教程: YouTube Video