qq_52019234 2022-09-01 20:54 采纳率: 100%
浏览 41
已结题

关于#unity#的问题,如何解决?

大lao们请问一下,我想让unity在一开始运行时脚本里的代码和动画停止执行,然后只有当按下按钮时才继续运行,但是我用Time.timeScale = 0;这行代码会发现确实会暂停,但是在运行一开始仍会有一些代码会被执行,就是我想当我按下按钮的时候,代码和动画才会被执行,该怎么做啊,下面是代码和后台输出的信息

img

img

img

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;
using System.Runtime.InteropServices;
using UnityEngine.UI;
using System.Threading.Tasks;

public class deom : MonoBehaviour
{
    int count = 0;
    public Text p;
    [DllImport("__Internal")]
    public static extern void SayHello(string str);
    [DllImport("__Internal")]
    private static extern string ReportReady();
    private Tweener _tweener;
    public GameObject[] Obj;
    public GameObject[] Obj1;
    //public int play = 0;
    int m = 0, n = 0;
    void Start()
    {
        Time.timeScale = 0;
        for (int i=0;i<Obj.Length;i++)
        {
            Obj1[i].GetComponent<Text>().text = Obj[i].GetComponent<Transform>().lossyScale.y.ToString();
        }
        StartCoroutine(startMove());
    }
    public void sendMsgToVue()
    {
        SayHello("modelLoadingFinish");//向vue发送消息
        Debug.Log("sendMsgToVue unity发送消息执行完毕");
    }

    public void receiveMsgFromVue(string  msg)
    {
        Debug.Log("接受来自vue的消息 == " + msg);
        p.text = msg;
        string numberString = System.Text.RegularExpressions.Regex.Replace(msg, @"[^0-9]+", "");
        count = Convert.ToInt32(numberString);
    }
    public void receiveMsgFromVuea(string msg1)
    {
        Debug.Log("获取数据1" + msg1);
        string numberString = System.Text.RegularExpressions.Regex.Replace(msg1, @"[^0-9]+", "");
        int numberInt = Convert.ToInt32(numberString);
        Debug.Log("转换后的数字: " + numberInt);
        float x = (numberInt + 1) / 2f;
        if (count >= 1)
        {
            Obj[0].GetComponent<Transform>().localScale = new Vector3(1, numberInt, 1);
            Obj[0].GetComponent<Transform>().position = new Vector3(-6.5F, x, 1);
            Obj1[0].GetComponent<Text>().text = Obj[0].GetComponent<Transform>().lossyScale.y.ToString();
        }
    }
    public void receiveMsgFromVueb(string msg1)
    {
        Debug.Log("获取数据2" + msg1);
    }
    public void receiveMsgFromVuec(string msg1)
    {
        Debug.Log("获取数据3" + msg1);
    }
    public void receiveMsgFromVued(string msg1)
    {
        Debug.Log("获取数据4" + msg1);
    }
    public void receiveMsgFromVuee(string msg1)
    {
        Debug.Log("获取数据5" + msg1);
    }
    public void receiveMsgFromVuef(string msg1)
    {
        Debug.Log("获取数据6" + msg1);
    }
    public void receiveMsgFromVueg(string msg1)
    {
        Debug.Log("获取数据7" + msg1);
    }
    public void receiveMsgFromVueh(string msg1)
    {
        Debug.Log("获取数据8" + msg1);
    }
    public void receiveMsgFromVuei(string msg1)
    {
        Debug.Log("获取数据9" + msg1);
    }
    public void receiveMsgFromVuej(string msg1)
    {
        Debug.Log("获取数据10" + msg1);
    }
    IEnumerator startMove()
    {
        int i, j;
        for (i = 0; i < Obj.Length - 1; i++)
        {
            for (j = 0; j < Obj.Length - i - 1; j++)
            {
                Debug.Log(DateTime.Now.Second.ToString());
                if (Obj[j].GetComponent<Transform>().lossyScale.y > Obj[j + 1].GetComponent<Transform>().lossyScale.y)
                {
                    GameObject x,y;
                    Sequence s = DOTween.Sequence();
                    Material mal = Obj[j].GetComponent<MeshRenderer>().material;
                    Material mal1 = Obj[j + 1].GetComponent<MeshRenderer>().material;
                    Debug.Log(Obj[j]);
                    Debug.Log(Obj[j + 1]);
                    Vector3 a = Obj[j + 1].GetComponent<Transform>().position;
                    Vector3 b = Obj[j].GetComponent<Transform>().position;
                    Vector3 c = Obj1[j + 1].GetComponent<Transform>().position;
                    Vector3 d = Obj1[j].GetComponent<Transform>().position;
                    var tweener = Obj[j].GetComponent<Transform>().DOMoveX(a.x, 2).SetId("1");
                    var tweener1 = Obj[j + 1].GetComponent<Transform>().DOMoveX(b.x, 2).SetId("1");
                    var tweener2 = Obj1[j].GetComponent<Transform>().DOMoveX(c.x, 2).SetId("1");
                    var tweener3 = Obj1[j + 1].GetComponent<Transform>().DOMoveX(d.x, 2).SetId("1");
                    mal.DOBlendableColor(Color.yellow, 2).OnComplete(() => { mal.DOColor(Color.white, 0).OnComplete(() => { mal.DOKill(); }); }).SetId("1");
                    mal1.DOBlendableColor(Color.yellow, 2).OnComplete(() => { mal1.DOColor(Color.white, 0).OnComplete(() => { mal1.DOKill(); }); }).SetId("1");
                    //if (play == -1)
                    //{
                    //    Debug.Log("暂停");
                    //    DOTween.Pause("1");
                    //    //yield return StartCoroutine("stop");
                    //    yield return StartCoroutine(stop());
                    //    //yield return new WaitForSeconds(3);
                    //}
                    //else if (play == 1)
                    //{
                    //    DOTween.Play("1");
                    //    x = Obj[j];
                    //    Obj[j] = Obj[j + 1];
                    //    Obj[j + 1] = x;
                    //    Debug.Log(Obj[j]);
                    //    Debug.Log(Obj[j + 1]);
                    //    Debug.Log(Obj[j + 1]);
                    //    yield return new WaitForSeconds(3);
                    //}
                    x = Obj[j];
                    Obj[j] = Obj[j + 1];
                    Obj[j + 1] = x;
                    y = Obj1[j];
                    Obj1[j] = Obj1[j + 1];
                    Obj1[j + 1] = y;
                    Debug.Log(Obj[j]);
                    Debug.Log(Obj[j + 1]);
                    yield return new WaitForSeconds(3);
                }
            }
        }
    }
    //IEnumerator stop()
    //{
    //    int seconds = 0;
    //    while (true)
    //    {
    //        for (float timer = 0; timer < 1; timer += Time.deltaTime)
    //            yield return 0;
    //        seconds++;
    //        Debug.Log(seconds + " seconds have passed since the Coroutine started.");
    //        if (play == 1)
    //        {
    //            StopCoroutine("stop");
    //            yield return StartCoroutine("startMove");
    //            break;
    //        }
    //    }

    //}
    //void f()
    //{
    //    StartCoroutine("stop");
    //}
    public void Pause()
    {
        //play = 0;
        Time.timeScale = 0;
    }
    public void Play()
    {
        //play = 1;
        Time.timeScale = 1;
    }
    void Update()
    {

    }
}


  • 写回答

2条回答 默认 最新

  • 陈言必行 Unity领域优质创作者 2022-09-01 22:13
    关注

    问题产生原因:在Start中开启了startMove协程,所以当你运行后,协程里面的逻辑就执行了

    解决方法:

    1. 首先,删除第30行代码:StartCoroutine(startMove());
    2. 然后,在177行添加:StopCoroutine("startMove");
    3. 最后,在182行添加:StartCoroutine("startMove");

    1步骤解决问题产生原因,2步骤解决点击暂停按钮可以停止协程,3步骤解决点击开始按钮开启协程

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月10日
  • 已采纳回答 9月2日
  • 赞助了问题酬金10元 9月1日
  • 创建了问题 9月1日

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题