Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Aug 22, 2016 at 02:00 PM · c#ui imageswappingimage loader

Swap UI Image from images Folder C#

If I have a folder called "Factoid Images" that is located in Textures/Factoid Images

How can I change that image to a new image from that folder with each:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class FactoidManager : MonoBehaviour {
 
     public Image factoidImagePlaceholder;
     public Sprite[] gallery;    //Store Factoid images in here
     public int i = 0;    //Will Control where in the array we are
 
 
     public Text factoidScreenText;
     public TextAsset factoidText;
     public string[] factoidLines;
 
 
     public int startLine;
     public int endLine;
 
 
     void Start(){
         factoidImagePlaceholder.enabled = false;
         if (factoidText != null) {
             factoidLines = (factoidText.text.Split ('\n'));
         }
 
         if (endLine == 0) {
             endLine = factoidLines.Length - 1;
         }
     }
 
     void Update () {
         factoidImagePlaceholder.sprite = gallery[i];
     }
 
 
 
     //Factoid Calls
     public void  GoFactoid01(){//Billy The Kidd
         factoidScreenText.text = "BILLY THE KIDD\nREAL NAME WILLIAM BONNEY\nBORN: 1860\nDIED: JULY 13, 1881"; //NEEDS to Display multiple lines from the .txt doc
 
         //Swap Image from ""Factoid Images" foldeer Code
         factoidImagePlaceholder.enabled = true;
         i = 2;
 
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid02(){//Darth Vader
         factoidScreenText.text = "DARTH VADER\nALSO KNOWN AS ANAKIN SKYWALKER A FICTIONAL CHARACTER IN THE STAR WARS UNIVERSE.\nCHARACTER CREATED BY DIRECTOR GEORGE LUCAS"; //NEEDS to Display multiple lines from the .txt doc
 
         factoidImagePlaceholder.enabled = true;
         i = 5;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid03(){//Parapsychology
         factoidScreenText.text = "THE STUDY OF MENTAL PHENOMENON THAT ARE EXCLUDED FROM THE OR INEXPLICABLE BY ORTHODOX SCIENTIFIC PSYCHOLOGY SUCH AS HYPNOSIS, TELEPATHY, ETC."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = false;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid04(){//U.F.O.s
         factoidScreenText.text = "AN UNIDENTIFIED FLYING OBJECT, OR U.F.O. IN IT'S MOST GENERAL DEFINITION, IS ANY APPARENT ANOMALY IN THE SKY THAT IS NOT IDENTIFIABLE AS A KNOWN OBJECT OR PHENOMENON."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 11;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid05(){//R2D2
         factoidScreenText.text = "R2D2 or ARTOO-DETOO IS A FICTIONAL CHARACTER IN THE STAR WARS UNIVERSE\nCREATED BY GEORGE LUCAS\nREFERED TO IN THE NOVEL AS A THERMOCAPSULARY DEHOUSING ASSISTER"; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 10;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid06(){//C3PO
         factoidScreenText.text = "C-3PO or SEE-THREEPIO IS A FICTIONAL CHARACTER IN THE STAR WARS UNIVERSE\nCREATED BY GEORGE LUCAS\nPROTOCOL DROID TO ASSIST IN ETIQUETTE, CUSTOMS, AND TRANSLATION."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 3;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid07(){//Ancient Astronaughts
         factoidScreenText.text = "ANCIENT ASTRONAUTS (OR ANCIENT ALIENS) IS A PSEUDOSCIENTIFIC CONCEPT BASED ON THE BELIEF THAT INTELLIGENT EXTRATERRESTRIAL BEINGS VISITED AND MADE CONTACT WITH HUMANS IN PREHISTORIC TIMES"; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 0;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid08(){//Crop Circles
         factoidScreenText.text = "AN AREA OF STANDING CROPS THAT HAS BEEN FLATTENED IN THE FORM OF A CIRCLE OR MORE COMPLEX PATTERN. NO GENERAL CAUSE OF CROP CIRCLES HAS BEEN IDENTIFIED ALTHOUGH VARIOUS NATURAL AND UNORTHODOX EXPLANATIONS HAVE BEEN PUT FORWARD. MANY OF THE CIRCLES ARE KNOW TO BE HOAXES WHILE OTHERS REMAIN UNEXPLAINED."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 4;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid09(){//Big Foot
         factoidScreenText.text = "BIG FOOT (ALSO KNOWN AS SASQUATCH) IS THE NAME GIVEN A MYTHICAL SIMIAN, APE, OR HOMINID-LIKE CREATURE THAT IS SAID TO INHABIT FORESTS, MAINLY IN THE PACIFIC NORTHWEST. IN AMERICAN FOLKLORE, BIGFOOT IS USUALLY DESCRIBED AS A LARGE, HAIRY, BIPEDAL HUMANOID."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 1;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid10(){//Dracula
         factoidScreenText.text = "THOUGH DRACULA IS A PURELY FICTIONAL CREATION, BRAM STOKER NAMED HIS INFAMOUS CHARACTER AFTER A REAL PERSON WHO HAPPENED TO HAVE A TASTE FOR BLOOD: VLAD III, PRINCE OF WALLACHIA OR - AS HE IS BETTER KNOW - VLAD THE IMPALER."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 7;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid11(){//Doc. Brown
         factoidScreenText.text = "EMMETT LATHROP (DOC. BROWN), Ph.D, IS A FICTIONAL CHARACTER IN THE BACK TO THE FUTURE TRILOGY IN WHICH HE IS THE INVENTOR OF THE FIRST TIME MACHINE HE BUILT OUT OF A DELOREAN SPORTS CAR. THE CHARACTER IS PORTRAYED BY CHRISTOPHER LLOYD."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 6;
         StartCoroutine(LoadFactoid());
     }
 
     public void  GoFactoid12(){//Frankenstein
         factoidScreenText.text = "FRANKENSTEIN: OR, THE MODERN PROMETHEUS IS A NOVEL WRITTEN BY ENGLISH AUTHOR MARY WOLLSTONECRAFT SHELLEY THAT TELL THE STORY OF A YOUNG SCIENCE STUDENT VICTOR FRANKENSTEIN, WHO CREATES A GROTESQUE BUT SENTIENT CREATURE IN AN UNORTHODOX SCIENTIFIC EXPERIMENT."; //NEEDS to Display multiple lines from the .txt doc
         factoidImagePlaceholder.enabled = true;
         i = 8;
         StartCoroutine(LoadFactoid());
     }
 
 
     //Factoid Calls
     IEnumerator LoadFactoid(){
         yield return new WaitForSeconds(1.5f); // wait time
         MasterPanelManager mpm = FindObjectOfType<MasterPanelManager>();
         mpm.EnableFactoidPanel();
         CameraSwitcher cs18 = FindObjectOfType<CameraSwitcher>();
         cs18.EnableCamera18();
 
         yield return new WaitForSeconds(15.5f); // wait time
         MasterPanelManager ebd = FindObjectOfType<MasterPanelManager>();
         ebd.DisableFactoidPanel();
         CameraSwitcher csdef = FindObjectOfType<CameraSwitcher>();
         csdef.CameraDefault();
 
     }
 }
 
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
1

Answer by JedBeryll · Aug 23, 2016 at 05:30 AM

If you don't want to reference them in the inspector then i think you have to put the entire folder into a Resources folder. Then you can load them with Resources.Load<Image>("Factoid Images/imgName");

Comment
Add comment · Show 7 · 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 KnightRiderGuy · Aug 23, 2016 at 01:24 PM 0
Share

Thanks @JedBeryll, Yeah I was wondering about that, I heard that the resources folder loads everything in it and that if you have a lot in there it can be not so good. I'm currently using an array and displaying any images needed on a single image place holder. But I'm wondering that if my array get quite large if I do a huge data base of "Factoids" then this method might become a little cumbersome. I'm wide open to any ideas on how to handle something like this a little better.

 public Image factoidImagePlaceholder;
     public Sprite[] gallery;    //Store Factoid images in here
     public int i = 0;    //Will Control where in the array we are
 
 
     public Text factoidScreenText;
     public TextAsset factoidText;
     public string[] factoidLines;
 
 
     public int startLine;
     public int endLine;
 
 
     void Start(){
         factoidImagePlaceholder.enabled = false;
         if (factoidText != null) {
             factoidLines = (factoidText.text.Split ('\n'));
         }
 
         if (endLine == 0) {
             endLine = factoidLines.Length - 1;
         }
     }
 
     void Update () {
         factoidImagePlaceholder.sprite = gallery[i];
     }
 
 
 
     //Factoid Calls
     public void  GoFactoid01(){//Billy The $$anonymous$$idd
         factoidScreenText.text = "BILLY THE $$anonymous$$IDD\nREAL NA$$anonymous$$E WILLIA$$anonymous$$ BONNEY\nBORN: 1860\nDIED: JULY 13, 1881"; //NEEDS to Display multiple lines from the .txt doc
 
         //Swap Image from ""Factoid Images" foldeer Code
         factoidImagePlaceholder.enabled = true;
         i = 2;
 
         StartCoroutine(LoadFactoid());
     }

avatar image JedBeryll KnightRiderGuy · Aug 23, 2016 at 02:05 PM 0
Share

I don't think it Loads everything by default... what would be the point of Resources.LoadAll and Resources.UnloadAsset functions if it did?

avatar image KnightRiderGuy · Aug 23, 2016 at 02:45 PM 0
Share

Thanks JedBeryll, That makes sense. Although I can't seem to get my image to load as:

 factoidImagePlaceholder = Resources.Load<Image>("FactoidImages/Geode");
avatar image JedBeryll KnightRiderGuy · Aug 23, 2016 at 03:13 PM 0
Share

Try Sprite if it's going to be used in UI Image or Texture if RawImage.

avatar image KnightRiderGuy JedBeryll · Aug 24, 2016 at 12:53 PM 0
Share

Thanks @JedBeryll, Are you saying that if in the top of my C# script I'm publicly declaring an image like this:

 public Image factoidImagePlaceholder;

Which is an image right? Should that be a reference to a Sprite ins$$anonymous$$d?

And then I would make my call to display this:

 factoidImagePlaceholder = Resources.Load<Sprite>("FactoidImages/Geode");

??? Would I put the image extension on the end like: "FactoidImages/Geode.png"

Sorry I'm pretty new to calling to outside folder sources.

Show more comments
Show more comments

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

221 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 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

How to make FunRace3D Style Player-Target Distance with UI 0 Answers

Swapping values randomly every 2 seconds. 0 Answers

I tried fixing "Using GUiTexture has been removed, use UI.Image instead" But it wont work 1 Answer

Can I use a string to load an image in UI? 0 Answers

Reset Raw UI Image For Audio Spectrum Data 0 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