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