Search Unity

Prefabs and creating assets at runtime and in builds

Discussion in 'General Discussion' started by Tomza, May 11, 2017.

  1. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Hi,

    I'm working over a 3D platform for clients and I wish them to create at least prefabs in game. I know that it is designed to work only in Editor, but it is important for me to include such a feature in builds. My question: is it possible to provide Unity users with this possibility? I need my clients are able to import 3D models from the disk and then make them into prefabs to create their prefab collections. How to work with things imported at runtime (changing them, saving and loading).
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    I'm not entirely sure about prefabs. Have you looked into asset bundles?
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    You'll need to roll your own system for that. Basically, track models uploaded by client, and parameters set to hose models. Nothing really prevents you from saving those to a json file and loading them later.

    Another option is asset bundles.
     
  4. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Thank you, guys!
    I know about asset bundles and persistent data like JSON or serialization. I use them in my projec. But why don't Unity Technologies include such a feature in Unity? It will be very useful because Unity is a platform not only for games but also for various tools. Those Editor functions should be available in builds. What is the obstacle to use them in builds. It would make work easier for many users including me.

    Unity Team, please consider that. It is the only problem I have in Unity.
     
    tonytopper and Highavenue01 like this.
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    Editor functions create assets within the project. Those assets are later baked into data that is used by your runtime build. Editor functions can also modify those assets and even destroy them.

    The reason why editor functions are not available at runtime is because the asset database is "frozen" and cannot be changed.

    While having some sort of "PrefabsLite" for runtime purposes is not a bad idea, having editor functions available is not a good thing, simply because those functions can wipe out data used by your game, and having ability to wipe the data often would require elevated privilegies in runtime build.

    In short what you're looking for is not "Editor functions at runtime", but rather ability to serialize and deserialize any object at runtime. Which is not exactly the same thing.
     
    RaveBox and tonytopper like this.
  6. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    I understand, not all is probably possible at runtime. It's not a game, it is a tool. Dangerous? So there should be a possibility to switch that on or off. All in a developer's hands, of course.

    As I said, I use serialization. However, it would be great to have a control over prefabs at runtime, for example you take two prefabs and make one from them, replace them, nest, etc. In other words, similar functions to those we have in editor. Now, you can only instantiate a prefab at runtime. What can I do more with it?
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The simple answer why Unity doesn't support building prefabs at runtime is Unity already built a tool for that, which is the Editor. The Editor's purpose is to create games. You're attempting to use the Editor to create standalone builds from the Editor that function as tools with output that goes back into the Editor.... That's not really the workflow Unity had in mind.

    Since the Editor is free, there is no barrier to entry. Unity also included Editor Extensions to fill the role you're trying to use a standalone build for. My opinion is whatever you're trying to do you should be doing as an Editor Extension.
     
  8. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    I have no experience with writing Editor Extensions, so my question: is there a possibility to write such an Editor Extension by me? Is there an API for that? My idea is to import a mesh at runtime by a client and create a prefab from it and then change this prefab, place in a Resource folder, load from the folder, etc. All done at runtime.

    I think providing Unity users with such a possibility would make Unity more universal and multipurpose. We have investors and money, so it is the sphere Unity should be interested in. We are going to use the Enterprise level of Unity because we need special features.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    Importing models is one thing, creating prefabs is another. What is it that you need to do?
     
    theANMATOR2b likes this.
  10. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    I have in-build prefabs in Resources folders that can be used by users at runtime (they will be default prefabs/models available for all). They can be spawned, scaled, their material and texture can be changed. They can also be saved, loaded and deleted. All is done in my app and works great. Now, I need users to be able to increase the collections of the models/prefabs at runtime. I need to allow my clients to import stuff like meshes, textures, etc. and then let them do things like in the Unity Editor. For example, a user import his custom mesh into my app, texture it and do other things and makes a prefab. Then, the prefab is placed in a Resources folder and is available for him in game if he wants to use it.

    I have just seen some videos about Editor Scripting. Alas, that cool stuff works only in Editor namespace, not in builds My idea is to get some functions that are in the Editor in builds. My question is why so powerful functions available in the Editor on now cannot be available in build either. For example, this video:



    teaches Unity users how to use the Editor scripting. Why aren't those things available in builds? I know that it can be dangerous because we can give too much power to users, but I think a developer should have a possibility to limit the features that are available for users. A develop can decided what will be exposed to a user and what not.

    In short, I need the Editor stuff in builds. At least, some features.
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    So what you're after is a model import and texture selection feature?
     
  12. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    I can import meshes, textures, movies, etc. No problem. The problem is that I need a user to have a possibility to place this mesh (imported by a user.) as a prefab in a Resources folder to be available in the menu for spawning in the scene. Users can spawn the default prefabs that will be in-built as I said before (a default collection of objects prepared by me) and I wish users be able to add their own object (prefabs) to the default collection. And the added objects must be available for using like the default ones. No difference. The problem is that the prefab is the Editor stuff. So users are unable to create a prefab from an imported model/mesh at runtime. This is the problem. It is not a game, but a tool. There are many good things but they are available only in the Editor. Why not in builds? Unity would be a very flexible platform.

    I though about using UE4 for that because the source code is available but UE4 is more complex and we have no time to lose.
     
  13. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    Unrela engine explicitly forbids using editor tools at runtime or providing editor functionality at runtime.

    https://www.unrealengine.com/eula
    https://unity3d.com/learn/tutorials/temas/best-practices/resources-folder
    https://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html
    You need to check how resource folder works. The content from that folder is cooked during build time, so it is not really supposed to be used for user data exchange.
     
  14. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    We were informed by Rama. But I am curious WHY?

    I know how the folder works. My question is: is it possible to make it more flexible.

    Taking both engines into consideration, what the purpose of those limitations? We, users, could create more cool stuff. What's wrong with that?
     
  15. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    So you won't create a competitor to Unreal Engine.

    Allowing editor tools at runtime would allow you to easily make a runtime editor and distribute it to an unlimited number of people who will not be subject to unreal eula. Meaning they will be legally allowed to make games with your tool without paying a thing to epic games.

    I think it should be possible to prototype basic prefab-like system in a week or less. It'll have to be a custom solution, though. Roll out your custom format, scan local folders for user data present, etc. It is not difficult.

    1. Preventing someone from hijacking tools and creating competitor (UNreal)
    2. Portability/Simplicity (Unity). Some platforms may not even HAVE user-writeable folders. Meaning the easiest way to make prefabs is make assets frozen and non-editable at runtime. It is also not a common scenario. If you're working only on PC and want runtime prefabs, you have an edge case scenario, not a common one.

    Also, please note that rather than "users making cool stuff" a company would be interested in "features that make engine more profitable". Runtime prefabs are edge case and most games don't need them. So, it'll be a low priority request.
     
    theANMATOR2b likes this.
  16. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Ok, it makes sense. However, they should avoid that in another way. Now, it is just a limiting developers.

    I can write a similar system too. I thought it would be possible to do that with Unity. But of course, if there's no other solutions, a custom prefab system should be created.

    Another thing, creating prefabs at runtime/build was not the only question. I wished to know about the possibility including other Editor things in builds. Now, I see it is impossible.

    Like above. I can add that there should be more flexibility. Now, a game is a something more than a game in the 90s. End user interactivity seems a very important feature.

    It is not my duty. Other people take care of that. Yes, in games, nobody needs that, but I repeat: it is not a game within the meaning of what most people think.

    This topic was started to get any info about that because there was a possibility that I missed something. But, I see that the only solution is asset bundles.
     
  17. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    I suggest to file a feature request. Either via forums (I think you can edit the thread title), either via unity feedback system. Also, I suspect there might've been something related on asset store already.
     
  18. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Thank you! I did a feature request a few days ago. I haven't found anything on the Asset Store.

    I'm going to create my own prefab system. In this way, I will be independent from Unity decisions. I created my own animation system in the project for recording and playing animations. Users can edit animations too. The same will be with my own prefab system. Much work, but there are some advantages.
     
  19. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    You don't need prefabs. Make a game object, add scripts, components(mesh filter, colliders, etc), create other game objects for the children you need, parent them, set their local positions, etc, etc. It's trivial and needing to use prefabs instead of knowing how yo do that isn't good for you as a programmer.
     
  20. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Well, you don't know all the project and the functions. Using prefabs has some advantages. My question: Why do you use prefabs instead of just objects? In my project, I have selecting, parenting and other stuff needed for a true level editor, but for now only for the in-built prefabs I keep in Resource folders. Every object will have many sub-objects, dynamic content, notes and all other stuff. It can be shared, changed, sent via network among people, etc. Really, I cannot imagine do that without prefab system. It must be easy and handy for the end users.
     
  21. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    All the things you listed can be built at runtime without issues. Save things to JSON files or whatever file type you can use to store the data, then reload it during the game at the start. I have a moldable game where players create custom content and it's added to the game at runtime. No prefabs are required for it. I add scripts and components at runtime, load models using a runtime importer in obj format, read tga at runtime, and read other filetypes at runtime. I think you can do the same and not need prefabs. Prefabs just aren't good if you're creating things at runtime.
     
    FlightOfOne, Tzan and angrypenguin like this.
  22. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Probably, you are right. And of course I can import obj models, add scripts and other components. Really, no problem. But still it would be great to have something like prefab. Soon, I will start write my own prefab system that will work at runtime. For increase control over entity data.

    And tell me... Do the users of your app can animate the content automatically and manually? And edit the anim clips, save, load and use them for various objects in the scene? Really, there are and will be many features that make me think that having some a handy container is a good idea.
     
  23. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    No, they don't do any animation, but you could load animation clips at runtime yourself. I don't think a prefab system would be the best unless what you're doing is loading asset bundles or something.
     
  24. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Yes, I use asst bundles too :) I included UMA and clothes are in asset bundles. I need to make the app universal as much as possible.
     
  25. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    If you can get models et. al. imported and you want users to be able to add their own then the issue isn't that you need them "as a prefab", it's that you need to unify the user interface so that using default and custom imported assets is the same.

    In other words, if you think of it as a UI issue rather than as a functionality issue then you should be able to approach it with the functionality already present.

    That said, heed what @neginfinity is saying about the license. Building something that supports user generated content is one thing, building your own editor is quite another and may run up against the license's restrictions.
     
  26. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Yes, I tested my .OBJ importer a couple months ago and it worked. The UI interface is complex enough :). For sure, it is enough working with models and components plus UI if you think about typical games.

    I still see some advantages in using prefabs. We will be adding new functionalities to the app all the time, so I think having one common consistent system on the top of models and components is useful. It is just convenient. For me and for the end users.

    I would like to hear Unty Team's opinion. There are level editors on Asset Store. I completely don't understand where the problem is. If we are successful, it is a good promotion for Unity. There will be logos of used software everywhere. Now, I use Unity Personal, but later on we will buy Unity Pro and I will leave the Unity logo. And then, Enterprise. We can talk. Besides, providing Unity users with more powerful tool (that is Unity Editor stuff at runtime) is making the engine more universal. Why not? Unity Technologies can control that in other way, not limiting functionalities. For me, it makes no sense.
     
  27. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    The "limited functionalities" probably aren't for that. There are loads of reasons that it makes complete sense to have extra functionality in your Editor that's not included in the runtime. For example, the Editor needs to be able to support a whole raft of model formats and conversions for us to do our work, but under normal runtime circumstances we only want our builds to support the relevant internal model format most suited to our target platform. (As for why, source and runtime model data formats are optimised for different things.)
    I'm not saying that there is a problem, just that there might be depending on specifically what you're doing. I think the idea is that you're not meant to use Unity to build something that might compete with Unity. If you're including some modding tools for your game that's probably not an issue. If you're building a program that is itself an editor for interactive applications then the problem Unity might have with that ought to be pretty near self evident...
     
  28. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    OK, I understand. But some Editor things can be available at runtime. I haven't written that all of them. Just "some". But Ok, no problem, I will live without.

    For sure, my app won't be for creating games. It is not a purpose. Also, it is not addressed to game developers. Even if I wanted, it could not be: you cannot build from a build. Even if our users would create some content, the content would live in the app only. There's no possibility to create something what can live outside of the app. So how it is possible to be a competitor for Unity?
     
  29. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    The problem is it doesn't work this way, wasn't designed to work this way, changing this behavior takes time (and costs money), and won't provide many benefits to most people. It is low-priority feature at best. You could file a feature request, and maybe it'll get implemented in a couple of years. Trying to convince people how awesome it would be to have this feature most likely also won't do a thing.

    As for unity team... perhaps @Buhlaine , @superpig, @Andy-Touch will chime in and say something about it. Either way, I wouldn't hold breath for getting this implemented quickly. Grabbing an existing solution from asset store or implementing your own is your best bet.
     
  30. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    So I am forced to take matters into my own hands. I won't be waiting for that.
     
  31. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,563
    You aren't "forced" to do anything. You have options. You can wait till unity maybe implements it someday, search for alternative implementations on asset store, implement it yourself or give up on it.

    However, amount of time it takes unity (or any big company) to roll out a new feature is significant, so unless your project has no deadline, asking unity to implement somethign and then waiting for it to happen usually is not the best idea.
     
  32. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    As I said, all in my hands. In this week, I am going to start the work over the custom prefab system.
     
  33. vinhphu3000

    vinhphu3000

    Joined:
    Jan 3, 2019
    Posts:
    1
    Hi @Tomza - I have a similar use case like what you are mentioning, please could you share us some of the ideas (design idea) how would you want to build a similar prefab system yourself?
     
  34. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Hi,
    I gave the project up and I'm doing something else. This question is to the Unity Team.
     
    Highavenue01 likes this.