Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Harabeck · Feb 10, 2012 at 10:24 PM · gameobjectprefabserializationsaveload

How to save/load references to components and prefabs

Our game will require extensive save and load functionality. I've been researching on this site and found tons of useful stuff on serializing basic variables, but nothing I've found quite answers a major concern of mine. As part of our game, the user will be able to construct GameObjects with children and various Components attached. As an example, the player can choose a hull, then add a cockpit and then a turret module. On the turret they can either choose a weapon or a tractor beam, both of which have their own component. The player will have dozens, maybe hundreds of such modules to choose from.

But, how do I go from whatever data I serialize to the prefabs and components I need to call upon to reconstruct the ship? The only way I can think of is to have paired lists. One is list of strings, ints, or floats holding an identifier, and the other holds prefabs. So if the string "hull1" is loaded from the save file, and that matches index 20 of the list of identifiers, then I take the prefab at index 20 of the list of prefabs. However, this seems like a terribly awkward solution. If I needed to change something, then I'd have to manually go through the list and find the entries. And if I needed to remove something, I'll get blank spots in the list or have to manually move something else up.

And for Components, I can't even use a list. The only thing I can think of is a large switch statement in the loading class. (e.g. switch compID { case "Laser" obj.AddComponent(Laser); break;...)

By the way, I have found a plug-in the claims to handle this(http://www.anbsoft.com/middleware/ezs/index.htm), but it's expensive, the site doesn't look trustworthy, and I can't tell if they've updated their software any time recently. It does however, give me hope that a convenient solution is possible.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by asafsitner · Feb 11, 2012 at 12:03 AM

You can load a prefab by name if you know the path to it. Take a look at `Resources.Load`.

You can make your saved names more descriptive so that you can build the correct path from the name alone. Say something along the lines of hulls_tier1_hullA

Then the Load call looks like Resources.Load("/hulls/tier1/hullA"); (splitting the string at '_' char obviously)

No need to load everything into memory and maintain lists and what not. Unless you want to give your player a list of all available modules, and even then you can run several foreach loops to fetch all available modules of each type (directory), then filter by what they can/can't equip etc.

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 Harabeck · Feb 11, 2012 at 01:31 AM 0
Share

Aha, thanks a ton.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to load gameobject from one scene to other? 2 Answers

save and load renderer data on a unit composed prefab 0 Answers

Error serializing a class to save / load 2 Answers

Serializer deletes saved info on game restart 1 Answer

Create a terrain prefab? Saving a terrain? No? 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