• 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 /
  • Help Room /
avatar image
2
Question by WalterCC · Dec 11, 2016 at 03:52 AM · editor-scripting

Read Write ScriptableObject in ProjectSettings Folder.

I need read/write ScriptableObject in ProjectSettings Folder to save my editor extension's per-project settings like unity official project settings did.

I've tried :

 settings = ScriptableObject.CreateInstance<MyPlugin>();
 AssetDatabase.LoadAssetAtPath<MyPlugin>("ProjectSettings/MyPlugin.asset");
 AssetDatabase.CreateAsset(settings, "ProjectSettings/MyPlugin.asset"); 
 
 AssetDatabase.LoadAssetAtPath<MyPlugin>("<OtherFolder>/MyPlugin.asset");
 AssetDatabase.CreateAsset(settings, "<OtherFolder>/MyPlugin.asset"); 

It doesn't work with error: UnityEngine.UnityException: Creating asset at path ProjectSettings/MyPlugin.asset failed. at (wrapper managed-to-native) UnityEditor.AssetDatabase:CreateAsset (UnityEngine.Object,string) at MyPlugin..cctor () [0x00033] in ... UnityEditor.EditorAssemblies:SetLoadedEditorAssemblies(Assembly[])

It seems that all apis in AssetDatabase can only read / write "Assets" folder. If so, why keep "Assets\" in Path? If not so, how to read write folder in project folder other than "Assets" folder?

Please help. Thanks.

Comment
Add comment · Show 1
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 WalterCC · Dec 13, 2016 at 02:10 AM 0
Share

Is there any body have this requirement?

1 Reply

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

Answer by Adam-Mechtley · Dec 13, 2016 at 08:58 PM

Yes unfortunately those API points only work with files in the Assets folder, though I can't comment as to why they therefore still require specification of the Assets folder in the supplied path.

If you want to read/write files in the Project Settings folder, I would suggest you consider .NET standard file I/O in combination with Unity's JsonUtility. That said, you'd have to do some engineering if you need to detect external changes to the file and so on.

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 WalterCC · Dec 14, 2016 at 04:48 AM 0
Share

Hey, thanks for the reply. The file I/O is ok. But, the problem is we can't instance ScriptableObject saved in folder other than "Assets" folder.

avatar image Adam-Mechtley WalterCC · Dec 14, 2016 at 09:45 AM 2
Share

You should be able to do something like this:

  1. Add a readonly singleton property (i.e. getter) to your ScriptableObject class

  2. Inside your singleton property, if the backing field for the singleton is null, create a new instance of your object that will be your singleton

  3. When you create the new singleton to store in the backing field, read the JSON data you wrote to the Project Settings and call JSONUtility.FromJSONOverwrite (), specifying your singleton instance as the object to write the JSON data to

  4. Add appropriate callbacks to the API points on your ScriptableObject to serialize its data back to the JSON file when something is changed

[2]: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnValidate.html

avatar image WalterCC Adam-Mechtley · Dec 16, 2016 at 03:55 PM 0
Share

Thanks for the solution. I think this is the bast way to do that now, unless unity make some change on that.

avatar image dCalle · Jun 11, 2017 at 07:32 PM 0
Share

Exactly, what I was looking for. Thanks ;-)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

90 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Process Terrain and Water in Editor? 0 Answers

why can't we set lighting parameter by script 0 Answers

I really need some help and it's frustrating 1 Answer

GUI.Window. Wanting to allow clickthrough 0 Answers

Working out which object the mouse is over in the editor 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