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 inkspot · Jan 02, 2014 at 07:25 PM · prefabassetsave

Saving usermade content

Hi all!, i made an editor script which allows players to snap objects together and creating a new object all together Now i want them to allow to save their creation in a folder as a loadable item like a prefab. What is the best way of doing so. I believe LoadAssetAtPath and PrefabUtility can only be used within unity's editor is there another way of doing this?

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
0

Answer by HappyMoo · Jan 02, 2014 at 09:06 PM

Sae your data in an object you derive from ScriptableObject.

http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.html

You can save your data with:

 AssetDatabase.CreateAsset(asset, "Assets/MyData.asset");
 AssetDatabase.SaveAssets();

And if you click on your asset file, you can see the Editor for it in the Inspector... which you can customize of course like for other Types.

Then you can drag those assets directly onto a

 public YourClass data;

field or you can load it from the AssedDatabase like other Assets or from Resourcs-Folders during runtime.

Comment
Add comment · Show 4 · 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 inkspot · Jan 02, 2014 at 10:07 PM 0
Share

Thank you for your answer, i've tried using your sample code as following:

 creation = GameObject.FindWithTag ("rootelement");
     AssetDatabase.CreateAsset(creation, "Assets/Resources/user_vehicles/"+creationname+".prefab");
             AssetDatabase.SaveAssets();

And getting a bunch of errors thrown at me, like - Persistent object inconsistency - Can't destroy Transform component. If you want to destroy the game object please call 'Destroy' on the game object ins$$anonymous$$d. Destroying the transform component is not allowed. - Some objects were not cleaned up when closing the scene

Am i missing something ?

avatar image HappyMoo · Jan 02, 2014 at 10:24 PM 0
Share

yes, you shouldn't save a whole gameObject, but have a custom class that saves your data and save that... that's the power of ScriptableObject.. that it actually doesn't need a gameObject to exist.

But maybe I missunderstood what you want. If you want to create a simple prefab like if someone would drag a gameObject from the hierarchy into the project, you can do that like this:

 Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/myname.prefab");
 PrefabUtility.ReplacePrefab(creation.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
avatar image inkspot · Jan 02, 2014 at 10:28 PM 0
Share

I tried that before the problem is 'editorutility' i think, it implies that it doesn't work in a standalone build? and only in the editor ?

avatar image HappyMoo · Jan 03, 2014 at 01:21 AM 0
Share

Yes, but the same is true for any editor script. If you need to save stuff in a custom format as a game feature, you're on your own... i.e. you can use mono/.net to write the stuff out however you want... You can create a Text file: http://msdn.microsoft.com/en-us/library/aa735748(VS.71).aspx

or write an xml file etc.

avatar image
0

Answer by jamesflowerdew · Jan 03, 2014 at 02:03 AM

The following may not be the best answer, but it is one answer.

This kind of stuff can usually done by sending raw data (although it is more work). You'd save out the data then use this to rebuild the assets on load. Obviously you could access "PlayerPrefs" to within limits, and in standalone mode possibly System.IO stuff:

 public static bool WriteTextFile(string nData, string nAddress){
     System.IO.File.WriteAllText(Application.dataPath + nAddress, nData);
     AssetDatabase.ImportAsset (nAddress);
     return(true);
 }//nData is the string data, nAddress is the address of the file

I'm not sure how well this System.IO stuff works in exes/apps etc though. I know it flunks in web players for what I guess is security reasons (Web apps that write files are frowned upon), and it seems unreliable in the editor at this end (saved assets need manually re-imported). On the web, PHP would probably be your friend (you'd post your data and retrieve it from a server).

I hope that this is helpful, and would love to know if you found a more elegant solution.

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 inkspot · Jan 03, 2014 at 01:11 PM 0
Share

Yeah, Right now i'm serializing the objects loading/saving them through large chunks of data written away to the playerprefs for now, it seems to be the best solution.

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

20 People are following this question.

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

Related Questions

Prefab not saving changes bug 1 Answer

How to modify prefab permanently via script. 0 Answers

Getting a prefab with javascript FindAsset (Solved) 0 Answers

How to save/load references to components and prefabs 1 Answer

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


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