Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by drok0920 · Jun 21, 2015 at 02:03 AM · assetloadresourcespath

Loading in Sprites outside of unity?

I need to load a .png file at a specified location on the computer. In this case im making a way to mod the game i am making and i need to read a string from a file(which works) and find the file at the location and load it to a variable. I was using: tempGolem.icon = Resources.LoadAssetAtPath(Vars.modsPath + "/" + golems[a]["icon"]) as Sprite;

but it doesn't seem to find anything. I know everything is spelled right, but when i try to use that sprite its just a white box. Any help?

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
5
Best Answer

Answer by Dave-Carlile · Jun 21, 2015 at 02:05 AM

According to the documentation, Resources.LoadAssetAtPath is deprecated and shouldn't be used. It was replaced with AssetDatabase.LoadAssetAtPath. However, I'm not sure you'll be able to use that for modding since it's only available in the Unity editor. It can't load arbitrary files off the disk. So I guess it depends on exactly what you're doing.

That said, you can load a .PNG from anywhere in the file system into a texture using something like this...

 using System.IO;
 ...

 byte[] data = File.ReadAllBytes(path);
 Texture2D texture = new Texture2D(64, 64, TextureFormat.ARGB32, false);
 texture.LoadImage(data);
 texture.name = Path.GetFileNameWithoutExtension(path);
 ...

Then create a Sprite instance using that texture and Sprite.Create.

This likely won't work in the web player, but if you're supporting modding I assume that isn't an issue for you.

Comment
Add comment · Show 4 · 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 drok0920 · Jun 21, 2015 at 11:03 AM 0
Share

Thank you but now will that work for a specific .png file or will it load all of them at the specified location? Because i have the user input the location of a specific .png file.

EDIT: I likely wont be using the webplayer unless i want to make a demo of the game in which case ill be removing modding anyway.

EIDT2: and i tried it and i get an access denied exception... but i can read text files from teh same location. Any idea why?

EDIT3: If it helps this is where im trying to get the file from: C:\Users\USERNA$$anonymous$$E\AppData\LocalLow\AcornRifle\Golemancy\$$anonymous$$ods And i am cosidered an ad$$anonymous$$ on that computer since i think that matters aswell.

Alright i managed to get it to not throw an error but when i get the sprite an assign it to the sprite of an image component its just blank.

avatar image Dave-Carlile · Jun 21, 2015 at 12:57 PM 0
Share

$$anonymous$$y sample code is only going to load a single image at a time. You would need to identify all of the files you want to load and load them one at a time.

avatar image drok0920 · Jun 21, 2015 at 01:33 PM 0
Share

I know that, and i have it in a for loop that hsould do what i need but its still just blank.

 for(int a = 0; a < golems.Count; a++) {
                 Golem tempGolem = new Golem();
 
                 tempGolem.golemName = golems[a]["name"];
                 tempGolem.tier = golems[a]["tier"].AsInt;
 
                 byte[] data = File.ReadAllBytes(Vars.modsPath + "/$$anonymous$$ainContent/icon.png");
                 Texture2D texture = new Texture2D(128, 128, TextureFormat.ARGB32, false);
                 texture.LoadImage(data);
                 texture.name = Path.GetFileName(Vars.modsPath + "/$$anonymous$$ainContent/icon.png");
 
                 tempGolem.icon = Sprite.Create(texture, new Rect(0.0f, 0.0f, 128.0f, 128.0f), new Vector2(0.5f, 0.5f), 1000);
 
                 $$anonymous$$od.golems.Add(tempGolem);
             }

Some of that code isnt relevant but i'll keep it in there just in case. i wasnt exactly sure what the Rect was for so i just set it to the size of the image.

and this is how i assign it

 currentButton.GetComponentInChildren<Image> ().sprite = $$anonymous$$od.golems[i].icon;


EDIT: and as i posted this i checked again in it suddenly works... I quess i forgot to save one of the scripts. Thanks for your help.

avatar image Dave-Carlile · Jun 21, 2015 at 02:15 PM 1
Share

You're welcome. Don't forget to click the check mark under the voting buttons.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Resources.Load renaming/moving resources. 0 Answers

Get path to loaded resource. 1 Answer

Subfolders with Resources.Load not working 2 Answers

Resources.Load dynamic path 2 Answers

Resources.Load folders path? 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