Hello 0nederful,
Yes, in Needle Engine for Unity, you can create C# scripts manually without relying on TypeScript. These manually created C# scripts can be placed anywhere within the Assets
directory in your Unity project. They don’t need to be within /src/scripts
. Unity will compile them as long as they are within the Assets
folder or any of its subdirectories.
Regarding generating a C# script to make an API call, here’s an example:
using UnityEngine;
using UnityEngine.Networking;
public class APICallExample : MonoBehaviour
{
private string apiUrl = "https://api.publicapis.org/entries";
void Start()
{
StartCoroutine(GetRequest(apiUrl));
}
IEnumerator GetRequest(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log(": Error: " + webRequest.error);
}
else
{
Debug.Log(":\nReceived: " + webRequest.downloadHandler.text);
}
}
}
}
Add this script as a component to any GameObject in your scene and upon starting the game, it will make a GET request to the specified API endpoint and log the result to the console. Remember to check Unity’s API Compatibility Level if you encounter any issues with networking, and you may also need to handle UnityWebRequest’s Lifecycle Disposal according to the Unity version you are using.
1 messages left GPT 4
thread_7dQWwgNWqB4Vh1gHfac2Lfe4