Hi!
I’ve been trying to use unity for two days now to create a simple project. It’s basically a very simple implementation of basic minecraft-like functionality. Just to see if I could do it.
But I got stuck at the asset database because if I remember correctly, You can’t use the editor-scripts at runtime and there’s the only set of functions that can parse the asset database.
So.
What I want to do:
-----!!!All at runtime!!!-----
- My script receives a “folder” variable (what is it’s type, anyway?) and in that folder are “Textures”, “Models” and “Scripts”.
- I download or read from the “resources” folder or(ideally) from the “scripts” asset folder) a set of textfiles with the object information.
- I generate a set of invisible objects(not-active) in a GameObject list using the data from the textfiles and assets from the folder that I have.
- I put those objects into the scene according to the information I get from net or files etc…
I can’t make my script extract a list of all the assets from a given path. I read about “resources” but they are really bad from what I hear. I want to use built-in assets to create something like “prefab list” in a GameObject.
Would anyone be able to explain to me how do You list all the textures(assets) form a given folder(passed to the script)?
Thank You.
The problem is that what you want to create goes quite far and indepth against what Unity is designed for (editor driven work with the assets provided and setup in the editor).
So a few facts to keep in mind:
- Whats not in resources folder will not even exist at runtime unless it was assigned to an object in a scene.
- Scripts never end in a readable form in the build, they are compiled and cease to exist
- Stuff in the resource folder does not end in the build as files either but as part of the compiled resources - Resources.LoadAll is your friend here. Also there is nothing to download here, they are loaded with the application and downloaded with the initial data on a streaming webplayer.
For what you want to do, offer modding, you will have to do quite a bit of extra work:
- Textures: Through WWW or reading all bytes and using texture2d loadimage you can get them in rather easily from true outside.
- Models: you will have to write a loader or buy one. Unity does not allow loading models from outside
- Scripts: not possible unless you use reflection and work on that base. I think it was AngryAnt who had a blog entry on the matter on his private blog.
O…k… Thank You.
So basically:
- If I create a 100 different models and put them into the project, only those that are assigned to a particular scene or an object within are available to me? I can’t just tell Unity to load “Assets/Entities/Chair.mdl” like a prefab and be able to put it onto the scene of it wasn’t previously put there?
Seems so. All the loadable assets need to be resources. Neat. (Streaming By-Resource is impossible then.)
- That I know. I just used the “scripts” name as a container for text files - basically scripts that are parsed by the scripts in Unity.
- Ok then. So my information about all the resources being in the memory was incorrect then? They are simply on the HD(for offline builds) or streamed. Thanks to Your information I read more about resources and it seems to be exactly what I need, thank You.
For future ref: [http://unity3d.com/support/documentation/Manual/Loading Resources at Runtime.html](http://unity3d.com/support/documentation/Manual/Loading Resources at Runtime.html)
Scripts: reflection? As in a VM inside a VM?
Thank You again.
-
Correct although you mix two things here: stuff not in the project does not exist at all to unity (stuff you put into the project is processed and stored in an internal format in case of meshes). You can’t load it through any unity functionality.
Stuff you put into the project and didn’t put into the resources folder isn’t included in the build unless its used (-> so you save build size for stuff you never use), stuff that you create only through code is meant to be in the resources folder within the project
-
So you mean text? Cause if not it makes no difference, unity does not parse anything. technically its neither scripting, its programming as all the stuff gets compiled when you build the project
-
Depends on. You are totally mixing things.
The docs talk about the resources folder in the project, which is then fully included in the build, but not as distinct files and not modifyable by the user.
They are present in a single file and upon request you can load data from them to use in the game (Resources.Load(…))
Also they aren’t “streamed” in the sense of “being downloaded after the game is runnin”. They are downloaded at start and as long as they aren’t around the first scene won’t start its execution, if we talk about a streamed webplayer.
For this kind of usage, downloading while we already play, you need unity pro to use asset bundles
Reflection: no its no vm. Reflection allows you to introspect whats inside a .net assembly to create classes basing on this or using them. as the assembly was not around at build time, you can’t just create class instances and use the functions otherwise.
Mhm. Ok.
I managed to do what I needed using the info You gave me.
And learn a lot about the structure of Unity.
Thank You Very Much 
I load everything at run time procedurally. Nested folders, quite extensive. I use .txt files to store the information and just start reading “master.txt” from the Resources folder.
But I do find that I need to put lists of all the asset names in a folder to get things going. It’s slightly inconvenient, but it works.
something like “master.txt” would have names like “object one,object two,object three” My script would read from folders named object one, object two, and object three. Each folder would contain a .txt file called, for example, “object one information”, there would be a prefab called “object one”, and a texture called “object one”. Unity would load each one by type, but they’d have the same name. If 'object one" was made up of other game objects, there would be a folder called “object one detail” which would be a .txt folder with other names in it. There would be similar folders for the items listed there, so there would develop a system of folders and sub folders. My scripts would keep track of the items in the game.
That is an idea. Actually I am now working on editor scripts to pre-bake things like that.
I will do that. Thanks for the help.