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 Leslie-Young · Sep 04, 2013 at 11:43 AM · unityeditorassetscriptableobject

Why is Unity updating these values in asset file?

I am trying to create a custom asset which is stored in the Resources folder. Editing is fine and all but my problem comes in when testing. I run my little sample, change the values in the runtime GUI, and then stop the player.

When I click on the asset to look at it in the inspector (with debug on) I can see that not only the public vars but also the property and private one where saved into my asset file. In the custom asset's editor window I can also see the values where updated for the two public fields.

Is Unity suppose to work like this and how can I prevent runtime changes being saved?

 public class TheAsset : ScriptableObject
 {
     public string data1;
     public string data2;
 
     public string PropData { get; set; }
     private string privateData;
 
     public string GetPrivateData()
     {
         return privateData;
     }
 
     public void SetPrivateData(string s)
     {
         privateData = s;
     }
 }
 
 public class AssetEditor: EditorWindow
 {
     private static TheAsset asset = null;
 
     [MenuItem("Window/Custom Asset Test")]
     public static void ShowDiaQEditor()
     {
         AssetEditor window = EditorWindow.GetWindow<AssetEditor>("TEST");
         window.LoadAsset();
     }
 
     public void LoadAsset()
     {
         asset = (TheAsset)Resources.LoadAssetAtPath("Assets/Resources/testdata.asset", typeof(TheAsset));
         if (asset == null)
         {
             asset = ScriptableObject.CreateInstance<TheAsset>();
             AssetDatabase.CreateAsset(asset, "Assets/Resources/testdata.asset");
             AssetDatabase.Refresh();
         }
     }
 
     public void OnFocus()
     {
         if (asset == null) LoadAsset();
     }
 
     public void OnGUI()
     {
         asset.data1 = EditorGUILayout.TextField("Data 1", asset.data1);
         asset.data2 = EditorGUILayout.TextField("Data 2", asset.data2);
 
         if (GUI.changed)
         {
             EditorUtility.SetDirty(asset);
         }
     }
 }
 
 public class Test : MonoBehaviour
 {
     private TheAsset asset;
 
     void Start()
     {
         asset = (TheAsset)Resources.Load("testdata");
     }
 
     void OnGUI()
     { 
         if (asset == null) return;
         GUI.Window(0, new Rect(Screen.width * 0.5f - 100, Screen.height * 0.5f - 125, 200, 250), Window, "Test");
     }
 
     void Window(int id)
     {
         GUILayout.Label("Data 1");
         asset.data1 = GUILayout.TextField(asset.data1);
         GUILayout.Label("Data 2");
         asset.data2 = GUILayout.TextField(asset.data2);
         GUILayout.Label("PropData");
         asset.PropData = GUILayout.TextField(asset.PropData);
         GUILayout.Label("PrivData");
         asset.SetPrivateData(GUILayout.TextField(asset.GetPrivateData()));
     }
 }
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 Leslie-Young · Sep 04, 2013 at 11:50 AM 0
Share

I should add, when I close and open Unity I see, in the asset's inspector, that the values where restored to what they where when I last edited it with the asset's custom editor window. So the runtime changes are not saved, but also not "forgotten" when I stop play testing.

1 Reply

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

Answer by MrDude · Sep 04, 2013 at 11:59 AM

You need to instantiate your object after loading it. Anything you do to the asset before you instantiate it is applied to the source asset,not the instance and as such it will persist.

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 Leslie-Young · Sep 04, 2013 at 12:16 PM 0
Share

Thanks, this made me realise how stupid I were :p

I should have done ... asset = (TheAsset)Instantiate(Resources.Load("testdata"));

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

17 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

Related Questions

How to load ScriptableObject from asset file immediately? 1 Answer

No solution in Monodevelop 5 Answers

Scriptable object keeps referencing the objects? 1 Answer

Using ScriptableObjects as Types/Enums 2 Answers

Override asset on import 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