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();
}
}
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");
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());
}
I don't think it Loads everything by default... what would be the point of Resources.LoadAll and Resources.UnloadAsset functions if it did?
Thanks JedBeryll, That makes sense. Although I can't seem to get my image to load as:
factoidImagePlaceholder = Resources.Load<Image>("FactoidImages/Geode");
Try Sprite if it's going to be used in UI Image or Texture if RawImage.
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.