- Home /
how to define a static texture2D?
i want to assign a picture from a folder in my computer to a texture2d, but not in the inspector! i want to do it in script. but it gives me an error like this:
"NullReferenceException: Object reference not set to an instance of an object"
Answer by Cyb3rManiak · Apr 26, 2011 at 08:26 AM
You can use Resources.Load or asset bundles to load textures on the fly without referencing them first. Just make sure to read the docs for its' limitations, especially if you're developing a streamed web player.
but looks like this is just for editor mode! how if I want to do this when the game is built? then I won't have any folder called asset :(
This is NOT for editor mode... The idea is that you create a folders called Resources somewhere inside your asset folder. Put anything that you want to load dynamically in those folders. At runtime call var myTexture: Texture2D = Resources.Load("$$anonymous$$yAssetToLoad"). The down side is that because Unity doesn't know which of the assets you want to load at compilation time - when the time comes (By default when the application starts) it will add ALL of the assets that are in folders called Resources to your build, so that it can use them on demand, and they will also take up runtime memory.
sorry I built the game but there was no folder called Resources! how should the user put some extra images in that folder? only I can put images in the folder before building the game, but end user can't.
Answer by Cyb3rManiak · May 01, 2011 at 08:46 AM
The Resources folder isn't exposed to the end user. It's used by you, and you alone when you develop your application. If you want your end user to add/change assets, you'll need to use other tools, such as the WWW class with the file:// syntax or the System.IO namespace of Mono (like this, for example). There are plenty of examples for it in the forum, and maybe even in the WIKI. Try searching for System.IO for example.
Your answer
Follow this Question
Related Questions
How to make a plane with a texture flicker, as if it was animated? 1 Answer
Assigning UV Map to model at runtime 0 Answers
Texture in GUI 0 Answers
How do I map a texture to a primitive instance? 4 Answers
Global Variables Refuse to Cooperate 1 Answer