[英]Why Does My Script Hang When I Run The Timer To Call Back The Method ? Unity C#
Why Does my Script Hang when I Run My Timer ? 为什么运行计时器时脚本会挂起? and when the Timer Stop, It will call back the method.
当计时器停止时,它将回调该方法。
For example my code below : 例如我的代码如下:
Script File - LakeSpot.cs (This script is generate random the spot Like Easy Spot, Very Easy Spot, and soon)
脚本文件-LakeSpot.cs(此脚本随机生成类似Easy Spot,Very Easy Spot之类的斑点,并很快生成)
I have shorten the code and delete the same code. 我已经缩短了代码并删除了相同的代码。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Linq;
using System.Collections.Generic;
public class LakeSpot : MonoBehaviour {
player Player;
fishingDatabase fishDatabase;
Image itemImage;
Sprite icon;
int maxSpot = 5;
List<spawnSpot> spot = new List<spawnSpot>();
int k = 0;
int maxVES;
int index;
getSpotLakeScript spotscript;
// Use this for initialization
void Start () {
Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
fishDatabase = GameObject.FindGameObjectWithTag ("fishingDatabase").GetComponent<fishingDatabase> ();
index = fishDatabase.spawnRateLake.FindIndex (j => j.Level == spawnSpot.spotLevel.VES);
maxVES = fishDatabase.spawnRateLake [index].maxSpawn;
GenerateSpot ();
}
// Update is called once per frame
void Update () {
}
public void GenerateSpot() {
Player.lakerollFishing = new List<int> ();
Player.lakeSpotRoll = new List<int> ();
Player.lakeNotActiveSpot = new List<int> ();
for(int i = 0; i < maxSpot; i++) {
int roll = Random.Range(0,fishDatabase.spawnRateLake.Count);
if(fishDatabase.spawnRateLake[roll].Level == spawnSpot.spotLevel.VES) {
if(maxVES > 0) {
this.gameObject.transform.GetChild(i).gameObject.SetActive(true);
icon = Resources.Load<Sprite> ("spotLevel" + "/" + "Very Easy Spot");
itemImage = this.gameObject.transform.GetChild(i).GetComponent<Image>();
itemImage.sprite = icon;
maxVES--;
Player.lakerollFishing.Add(roll);
Player.lakeSpotRoll.Add(i);
} else {
i--;
}
}
}
}
}
The methode GenereratSpot() is for generate randomly spot Like Very Easy Spot, Easy Spot, and soon GenereratSpot()方法用于生成随机斑点,例如Very Easy Spot,Easy Spot和很快
And Then i Have the script file - LakeVisitTimer.cs (This Script is for countdown the timer. When Timer is zero that it will call the GenerateSpot() method again.
然后我有了脚本文件-LakeVisitTimer.cs(此脚本用于倒计时计时器。当Timer为零时,它将再次调用GenerateSpot()方法。
I have shorten the code. 我已经缩短了代码。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
public class LakeVisitTimer : MonoBehaviour {
public Text TimerText;
public string dy;
public float days;
public float Hours;
public float Minutes;
public float Seconds;
int code;
LakeSpot spots;
player Player;
GameObject lakeside;
// Use this for initialization
void Start () {
StartCoroutine(Wait());
}
void Awake() {
Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
lakeside = GameObject.Find ("LakeSide").gameObject;
spots = lakeside.gameObject.transform.GetChild (1).GetComponent<LakeSpot>();
code = PlayerPrefs.GetInt ("LakeVisitCode");
if (code == 1) {
OnResumeSession ();
}
}
public void StopTimer() {
Seconds = 0;
Minutes = 0;
Hours = 0;
days = 0;
Player.maxLakeFishing = 2;
Player.lakerollFishing = new List<int> ();
Player.lakeSpotRoll = new List<int> ();
Player.lakeNotActiveSpot = new List<int> ();
PlayerPrefs.SetInt("LakeVisitCode",0);
spots.GenerateSpot ();
}
}
The Method StopTimer() is work when Timer is Zero. 当Timer为零时,方法StopTimer()起作用。 And It call back the spots.GenerateSpot() which is a method from LakeSpot.cs where function used to Generate randomly spot.
并且它会调用spot.GenerateSpot(),这是LakeSpot.cs中的一种方法,该函数用于随机生成斑点。
For Example the timer is zero now and it call back the GenerateSpot(). 例如,计时器现在为零,它回调了GenerateSpot()。 When call back it become Hang.
当回叫它成为挂起。
And I check the Task Manager the memory Take until 300 MB. 然后我检查任务管理器的内存,直到300 MB。
What is going on ? 到底是怎么回事 ?
I found the solution. 我找到了解决方案。
I just removed the script : 我刚刚删除了脚本:
Player.lakerollFishing = new List<int> ();
Player.lakeSpotRoll = new List<int> ();
Player.lakeNotActiveSpot = new List<int> ();
from LakeSpot.cs at GenerateSpot() method 来自LakeSpot.cs的GenerateSpot()方法
That script I have already call it LakeVisitTimer.cs at method StopTimer(). 该脚本我已经在StopTimer()方法中将其称为LakeVisitTimer.cs。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.