Невозможно остановить сопрограммы даже с помощью StopAllCoroutines(), C# UnityC#

Место общения программистов C#
Ответить
Anonymous
 Невозможно остановить сопрограммы даже с помощью StopAllCoroutines(), C# Unity

Сообщение Anonymous »

Используя метод setMapTypeRoadMap(), я вижу, что начальная сопрограмма «StartCoroutine(UpdateMap());» не останавливается. и из-за этого для типа карты не установлено значение type.roadMap.
Я пробовал все, используя создание переменной и остановку переменной, настроенной с помощью моей сопрограммы, я пытался использовать Threads, но ничего не работает
// Utilized Libraries

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using System.Collections;

// Map
// This class represents an interactive map that updates every 5 seconds by taking the location from the server via phone app.

public class Map : MonoBehaviour
{
// Questo è la API di Google per l'utilizzo della API google STATIC Map API.
private string apiKey = "MY API";
public enum resolution { low = 1, high = 2 }; // MAP RESOLUTION
public resolution mapResolution = resolution.high; // HIGH RES.
public enum type { roadmap, satellite, gybrid, terrain }; // TYPES OF MAP OF GOOGLE STATIC MAP API
private type mapType = type.satellite; // INITIAL MAP TYPE
private int mapWidth = 800;
private int mapHeight = 540;

// Definition of latitude and longitude variables.
private float lat = 0;
private float lon = 0;
private int zoom = 18;

//DEFINITION OF URL
private string url = "";
private Rect rect;

// START
void Start()
{

StartCoroutine(UpdateMap());
}

IEnumerator UpdateMap()
{
while (true)
{
string latLast = "";
string lonLast = "";

//Request from the server to take the coordinates.

using (UnityWebRequest webRequest = UnityWebRequest.Get("MY SERVER"))
{
// SEND REQUEST
yield return webRequest.SendWebRequest();

if (webRequest.result != UnityWebRequest.Result.Success)
{ //Errore.
Debug.LogError("Error: " + webRequest.error);
}
else
{
// GET CORDINATES
Coordinates data = JsonUtility.FromJson(webRequest.downloadHandler.text);
// Debug.Log("Coordinate: " + data.coordinates);

//EXTRACT LAT AND LON.
String[] parts = data.coordinates.Split(',');
latLast = parts[0];
lonLast = parts[1];

//Taking map measurements on Unity
//rect = gameObject.GetComponent().rectTransform.rect;
//mapWidth = (int)Math.Round(rect.width);
//mapHeight = (int)Math.Round(rect.height);

//Query
Debug.Log(mapType);
url = "https://maps.googleapis.com/maps/api/staticmap?" +
"center=" + latLast + "," + lonLast +
"&zoom=" + zoom +
"&markers=color:red|" + latLast + "," + lonLast +
"&size=" + mapWidth + "x" + mapHeight +
"&scale=" + (int)resolution.high +
"&maptype=" + mapType +
"&key=" + apiKey;

// Get RAW IMAGE Unity.

UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{//Errore
Debug.Log("WWW ERROR: " + www.error);
}
else
{

gameObject.GetComponent().texture = ((DownloadHandlerTexture)www.downloadHandler).texture;

}
}
}

yield return new WaitForSeconds(2); // WAIT OF 5 SEC
}
}

[Serializable]
private class Coordinates
{
public string coordinates;
}

public void setMapTypeRoadMap()
{
StopAllCoroutines();
mapType = type.roadmap;
StartCoroutine(UpdateMap());
}

}


Подробнее здесь: https://stackoverflow.com/questions/783 ... harp-unity
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»