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
0
Question by inejwstine · Oct 31, 2014 at 09:24 PM · resources.loaduploadcopyfile-io

Resources.Load on a recently copied file.

In my game I wanted to allow the player to choose their own avatar by uploading a local file. I couldn't get my idea to work. Fortunately I found this answer which did work, but now I'm curious why mine did not.

My idea was to use File.Copy to copy the desired file from the user's hard drive to the game's Resources folder, and then use Resources.Load to load the file as a Texture2D object. Here's an example:

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class Test : MonoBehaviour {
     
     public GameObject _avatar;
 
     void Start () {}
     
     void Update () {}
 
     void OnGUI() {
         if (GUI.Button (new Rect (710, 200, 75, 30), "Load")) {
             File.Copy(@"C:\PATH_TO_FILE\avatar.gif", @"C:\PATH_TO_GAME\Resources\avatar.gif", true);
         }
         if (GUI.Button (new Rect (600, 250, 75, 30), "Create")) {
             var tex = Resources.Load<Texture2D> ("avatar");
             _avatar= new GameObject ("AvatarObject", new System.Type[]{typeof(SpriteRenderer)});
             var spr = Sprite.Create (tex, new Rect (0f, 0f, 128, 128), new Vector2 (0.5f, 0.5f));
             _avatar.GetComponent<SpriteRenderer> ().sprite = spr;
         }
     }
 
 }

I verified that clicking the "Load" button does copy the correct file to the correct location. However, when I then click the "Create" button, nothing happens. When I pause in the editor, I can see that the gameobject was created, but the "sprite" field in the Sprite Renderer says "None," as if the file doesn't exist. If I stop playing and start again and click "Create" again, the same thing happens. However, if I stop playing and force the editor to Refresh the assets and then restart and click "Create," it does work.

Why is this?

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

Answer by Eric5h5 · Oct 31, 2014 at 10:37 PM

The Resources folder does not exist as such in a build; files in it are bundled into a combined asset file. So the files must exist prior to making a build and can't be added afterward. You can sort of make it work in the editor by manually adding files to Resources, which still exist as files in the editor for obvious reasons (though you need to update the asset database for Unity to recognize them), but that will never work in a build.

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 inejwstine · Nov 21, 2014 at 03:35 PM 0
Share

That makes sense. Thanks for humoring my curiosity!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can you monitor or profile file I/O from within Unity? 0 Answers

Resources Load to open pdf is not working anymore in Android 7 0 Answers

Finding resources folder on iOs 3 Answers

Creating an texture asset in editor script 2 Answers

file saved to iOS persistent directory exists during session but is gone next session 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