Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

430
Vistas
Unity Cómo usar las variables cargadas desde json string como entrada en la sección de actualización

Soy bastante nuevo en la codificación de Unity y C# en general, por lo que mi pregunta puede ser tonta para algunos de ustedes, pero actualmente estoy tratando de crear un botón en la ubicación en la que sus coordenadas son de una cadena json. Simplemente no sé adónde ir después de aquí para archivar mis objetivos. ¿Cómo puedo usar el xcoor de json como entrada en la actualización? Aquí está mi código:

 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using SimpleJSON; public class JSONcontroller : MonoBehaviour { public GameObject obj; Vector3 buttonPos; private string url = "http://localhost:3000/coordinates/"; // Start is called before the first frame update void Start() { StartCoroutine(getData()); } IEnumerator getData() { UnityWebRequest request = UnityWebRequest.Get(url); yield return request.SendWebRequest(); if(request.isNetworkError||request.isHttpError) { Debug.LogError(request.error); yield break; } JSONNode coordinates = JSON.Parse(request.downloadHandler.text); string xcoor = coordinates["x1"]; string ycoor = coordinates["y1"]; //Debug.Log(xcoor); } // Update is called once per frame void Update() { buttonPos = new Vector3(xcoor, ycoor, 4f); if (Input.GetButton("Fire1")) Instantiate(obj, buttonPos, Quaternion.identity); } }

y mi archivo json de muestra

 { "coordinates": [ { "id": 1, "x1": "5", "y1": "2" }, { "id": 2, "x1": "3", "y1": "5" }, { "id": 3, "x1": "5", "y1": "7" } ] }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Sus variables "xcoor" y "ycoor" en realidad se declaran en su método getData(), esto significa que estas variables solo se pueden usar dentro del método getData().

Puede declararlos en la parte superior de la clase, y luego podrá usar las variables en la Actualización y establecer la posición del botón.

código editado:

 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using SimpleJSON; public class JSONcontroller : MonoBehaviour { public GameObject obj; Vector3 buttonPos; private string url = "http://localhost:3000/coordinates/"; string xcoor; string ycoor; // Start is called before the first frame update void Start() { StartCoroutine(getData()); } IEnumerator getData() { UnityWebRequest request = UnityWebRequest.Get(url); yield return request.SendWebRequest(); if(request.isNetworkError||request.isHttpError) { Debug.LogError(request.error); yield break; } JSONNode coordinates = JSON.Parse(request.downloadHandler.text); xcoor = coordinates["x1"]; coor = coordinates["y1"]; //Debug.Log(xcoor); } // Update is called once per frame void Update() { buttonPos = new Vector3(xcoor, ycoor, 4f); if (Input.GetButton("Fire1")) Instantiate(obj, buttonPos, Quaternion.identity); } }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda