Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Values from a server

Discussion in 'Unity Reflect' started by chiarello, Mar 4, 2022.

  1. chiarello

    chiarello

    Joined:
    Jun 6, 2018
    Posts:
    5
    How can I make a text take values imported from a server?
     
  2. Unity_Derek

    Unity_Derek

    Unity Technologies

    Joined:
    Aug 24, 2021
    Posts:
    48
    Hi @chiarello, can you please elaborate on that? What are you trying to achieve?
     
  3. chiarello

    chiarello

    Joined:
    Jun 6, 2018
    Posts:
    5
    Hello, it is an augmented reality project, in which the temperature of an engine is shown to me, the temperature would be taken from a server
     
  4. Unity_Derek

    Unity_Derek

    Unity Technologies

    Joined:
    Aug 24, 2021
    Posts:
    48
    If I understand you correctly, I think you need to create something like REST API which you can use UnityWebRequest api to get the temperature from the server.
     
  5. chiarello

    chiarello

    Joined:
    Jun 6, 2018
    Posts:
    5
    Thank you very much, I'm going to investigate this. Sorry my command of English is not good. Greetings
     
    Unity_Derek likes this.
  6. chiarello

    chiarello

    Joined:
    Jun 6, 2018
    Posts:
    5
    Hello, I was looking at the WebRequest topic, how do I make this value taken from the web pass to the textmeshpro





    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;
    using TMPro;

    public class web : MonoBehaviour
    {

    [ContextMenu("Read")]

    public void Read()
    {
    StartCoroutine(CorrutinaLeerSimple());
    }

    private IEnumerator CorrutinaRead()
    {
    UnityWebRequest web = UnityWebRequest.Get("https://www......");
    yield return web.SendWebRequest();

    if(!web.isNetworkError && web.isHttpError)
    {
    Debug.LogWarning("problem");
    }
    else
    {

    Debug.Log(web.downloadHandler.text);
    }



    }
    }
     
  7. Unity_Derek

    Unity_Derek

    Unity Technologies

    Joined:
    Aug 24, 2021
    Posts:
    48
    Hello @chiarello,

    By looking at your script, I think this will only work within the editor, as you are only running it using the "Context Menu" attribute. Here is what you can do:

    1. Use this following script:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6. using TMPro;
    7.  
    8. public class web : MonoBehaviour
    9. {
    10.     public TextMeshProUGUI label;
    11.  
    12.     private void Start()
    13.     {
    14.         StartCoroutine(CorrutinaLeerSimple());
    15.     }
    16.  
    17.     private IEnumerator CorrutinaLeerSimple()
    18.     {
    19.         UnityWebRequest web = UnityWebRequest.Get("https://www......");
    20.         yield return web.SendWebRequest();
    21.  
    22.         if(!web.isNetworkError && web.isHttpError)
    23.         {
    24.             Debug.LogWarning("problem");
    25.         }
    26.         else
    27.         {
    28.             //Debug.Log(web.downloadHandler.text);
    29.             label.text = "Value from server: " + web.downloadHandler.text;
    30.         }
    31.     }
    32. }
    33.  
    2. Add the script to one of the object from "Hierarchy":

    Untitled-1.png


    3. With the same object selected, drag your textmeshpro object into the label which was defined in your script:

    Untitled-2.png

    Let me know if this helps or not.

     
  8. chiarello

    chiarello

    Joined:
    Jun 6, 2018
    Posts:
    5
    Thank you very much!, you were right the context menu was holding me back, now it works fine. Some time ago I made a video game, now I'm taking up Unity again. Thank you very much again for your dedication!
     
    Unity_Derek likes this.