I want to load the image like this:
void info(string channel) { //Something like that channelPic.Image = Properties.Resources.+channel }
Because I don't want to do
void info(string channel) { switch(channel) { case "chan1": channelPic.Image = Properties.Resources.chan1; break; case "chan2": channelPic.Image = Properties.Resources.chan2; break; } }
Is something like this possible?
Load SpriteFirst convert the image into sprite in unity editor, then you can load that at runtime just like textures. In the scene, you need to use “Image” game object instead of “RawImage” game object to load the sprite. Add below given script to Main Camera or any other game object. Run the application.
Add Image ResourceIn Visual Studio, click the Project menu, and select Add Existing Item. Find and select the image you want to add to your project. In the Solution Explorer window, right-click the image file you just added to your project, and select Properties from the popup menu.
You can always use System.Resources.ResourceManager
which returns the cached ResourceManager
used by this class. Since chan1
and chan2
represent two different images, you may use System.Resources.ResourceManager.GetObject(string name)
which returns an object matching your input with the project resources
Example
object O = Resources.ResourceManager.GetObject("chan1"); //Return an object from the image chan1.png in the project channelPic.Image = (Image)O; //Set the Image property of channelPic to the returned object as Image
Notice: Resources.ResourceManager.GetObject(string name)
may return null
if the string specified was not found in the project resources.
Thanks,
I hope you find this helpful :)
You can do this using the ResourceManager
:
public bool info(string channel) { object o = Properties.Resources.ResourceManager.GetObject(channel); if (o is Image) { channelPic.Image = o as Image; return true; } return false; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With