Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hesamkhan · Feb 03 at 07:21 PM · gameobjectfunctionsarray of gameobjects

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;
         }
     }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
   }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hesamkhan · Feb 04 at 10:04 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Three Spots For Three Random Objects 1 Answer

Retrieve multiple items from an array of GameObjects 2 Answers

Calculate position in an array of gameObjects 0 Answers

How to randomly select multiple gameobjects from array? 1 Answer

Find one inactive player (gameobject) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges