Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Nerond · Nov 17, 2016 at 11:39 AM · 2dprefabsfileinheritancemonobehaviour

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?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

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 Nerond · Nov 17, 2016 at 10:00 PM 0
Share

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.

avatar image
0

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.

https://blogs.msdn.microsoft.com/yojoshi/2011/05/14/auto-generating-entity-classes-with-xsd-exe-for-xml-serialization-and-de-serialization/

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instances of one prefab work wrong together,OnMouseOver works wrong with instances of prefabs 0 Answers

[Tilemap] Can you spawn a Prefab (with colliter) together with a tile when placing it? 0 Answers

Checking if a gameobjects active in your heirarchy. 2 Answers

2D Animation does not start 1 Answer

Why child ScriptableObjects class instance cannot be placed in their parent ScriptableObject field in the inspector? 1 Answer


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