- Home /
how can i change this script with 1 InputField and array of gameobjects?
hey guys . how can i improve this script with one InputField and array of gameobjects[] that saves the previous texture?
{
public InputField texUrlInput0;
public GameObject panel0;
public InputField texUrlInput1;
public GameObject panel1;
public void setTextureFromWeb()
{
string textureurl = texUrlInput0.text;
StartCoroutine(DownloadTexture(textureurl));
}
IEnumerator DownloadTexture(string textureurl)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(textureurl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
}
else
{
Texture texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
panel0.GetComponent<Renderer>().material.mainTexture = texture;
}
}
public void setTextureFromWeb1()
{
string textureurl = texUrlInput1.text;
StartCoroutine(DownloadTexture1(textureurl));
}
IEnumerator DownloadTexture1(string textureurl)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(textureurl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
}
else
{
Texture texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
panel1.GetComponent<Renderer>().material.mainTexture = texture;
}
}
}
Answer by Firnox · Feb 03 at 07:55 PM
Hi hesamkahn, here is a version that uses an array. I wasn't sure what you wanted to do if someone clicked it again before it finished so I just made it insist that you wait - this way you'd never end up with panels where the textures failed to download. If you want to fire off lots and any that fail result in blank panels, then instead you just want to cache the panel index in the DownloadTexture function and increase it straight away (you can then also do away with the downloading bool).
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Panels : MonoBehaviour {
public InputField texUrlInput;
public GameObject[] panels;
private int panelIndex = 0;
private bool downloading = false;
public void setTextureFromWeb() {
string textureurl = texUrlInput.text;
if (downloading) {
Debug.Log("Already downloading, please wait until it completes or errors");
} else {
// Catch to make sure we've not used up all our panels.
if (panelIndex < panels.Length) {
StartCoroutine(DownloadTexture(textureurl));
} else {
Debug.Log("Trying to download more textures than panels, oops.");
}
}
}
IEnumerator DownloadTexture(string textureurl) {
downloading = true;
UnityWebRequest request = UnityWebRequestTexture.GetTexture(textureurl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError) {
Debug.Log(request.error);
} else {
Texture texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
// Success, increment the panel index so next time it goes into the next one.
panels[panelIndex++].GetComponent<Renderer>().material.mainTexture = texture;
}
// Reset our downloading variable.
downloading = false;
}
}
hey firnox. tnx for ur support and help. i appreciate it . i think i didnt explained completely but according to ur solution i think i have to use another way. Apparently with ray casts to selecting objects and then changing their textures . im working on it. tnx bro
Your answer
Follow this Question
Related Questions
Find one inactive player (gameobject) 2 Answers
how can i spawn my 52 cards in list of my 52 spawn points gameobject? 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
how do I generate a drop-down list of functions on scripts attached to a game object? 1 Answer
Public Array of GameObjects 2 Answers