- Home /
Is it possible to create game object/prefab from file?
I have already finished some unity tutorials. Currently using '2D Roguelike' as a base for some experiments to learn. What boardManager does(creating map, zombies and stuff), making prefabs, addind animations etc. is clear to me, but I wonder how could I be able to to make it in files(like easy modding). For example let's say I get rid of zombies from my prefabs, and instead I want game to start like: 1.Open file enemies.xml(or any other file type, nevermind). enemies.xml like:
<enemies>
<zombie1>
<health>10</health>
<spritefile>zombie1</spritefile>
</zombie1>
</enemies>
2.Creates new prefab already containing collider, rigidbody and attached script, making zombie health 10, and loading sprites from files 'zombie1attack.png', 'zombie1idle.png'. 3.Adds all the enemies to boardManager, so it can spawn them around.
And making same thing with floors, walls, load from file, load relevant png and make it a prefab, which boardManager uses.
How hard is it to create a script interpreting file and creating enemy with rigidbody, collider, attached script and animations basing only on that file contents and sprites? I'd be grateful for any advices on how to start it or maybe tutorials about that?
Also a little question, when I should inherit from MonoBehaviour and when I shouldn't?
Answer by Erethan · Nov 17, 2016 at 02:03 PM
Interesting question. I am not sure there is a clean way to create prefabs from scratch via code. What you could do is to write something to fetch all files in a path in a given format(xml, for example), and for each file read, you instantiate a template prefab, and set stuff like health or sprites from the file read.
Let me give you some starting points:
StreamingAssets are good for adding stuff in the player's file system: https://docs.unity3d.com/Manual/StreamingAssets.html
If your want to facilitate the modding for custom abilities read about delegates
https://msdn.microsoft.com/en-us/library/ms173171.aspx
(some people find it to be quite advanced)
You can store info into a Scriptable Object. Think of them as data containers for all of your enemies information. As they are a asset, they don't use too much memory https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects
You mean I should make script creating x base prefabs(one for each file), and then customize those prefabs with what it found in corresponding file? Advancing further then, is it possible to attach interfaces basing on file content? Like: spittingZombie.xml contains line 'TRUE'. For me it appears not to be possible(game would need to create new class inheriting from entity and implementing X interfaces on run), but I am not sure. $$anonymous$$aking one universal template for everyone wouldn't be good idea I guess - why would Chicken have implemented things like ranged attacks(which he doesn't have), swim$$anonymous$$g(assu$$anonymous$$g it's normally impossible), skills, diseases, $$anonymous$$ing etc? Also such a class would be one big mess. On the other hand I could create different templates and creating them basing on file content, but their amount would rise exponentially. Like having just 'entity', then 'entityWithSwim$$anonymous$$g', 'entityWithRanged', 'entityWithSwim$$anonymous$$gAndRanged' etc. Is there the third way on avoiding this problem?
Thanks for the starting points you sent me also. I will definitely look at them deeper at weekend.
Answer by jmgek · Nov 17, 2016 at 10:19 PM
This is easily done, you want to create a entity (Class) that can populate XML data to the class.