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 uprashanth · Sep 04, 2014 at 04:03 AM · sceneplayerprefssavegameobjectsvalues

How to save gameobject values with respect to scene?

I am making a custom tool for my game, In my editor I have a IntSlider which has some value like 10. I want to save that value to a selected gameobject with respect to scene name.

Eg: I have Gameobjects named: Button1, Button2 IntSlider Value: MaxUse Scenes: Test1.unity, Test2.unity

Practical Senario: In Test1.unity scene I selected Button1 gameobject and its IntSlider value i.e MaxUse will show 2. But, if I Open Test2.unity scene and Select Button1 gameobject which is also in Test2.unity and its IntSlider value will be 2.

So I want to save IntSlider value of a selected gameobject based on the scene.

Here is my Code!

 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 
 public class DataStoreTest : EditorWindow
 {
 
     public int shrinkvalue;
     public string SceneName;
 
     void OnEnable()
     {
         string sceneName = EditorApplication.currentScene;
         string[] lines = Regex.Split(sceneName, "/");
         SceneName = lines[lines.Length - 1];
 
         if(Selection.activeObject != null)
         {
             PlayerPrefs.GetInt(Selection.activeObject.name.ToString(), shrinkvalue);
         }
     }
 
 
     [MenuItem("Temple Mystery/ Test")]
     static void Init()
     {
         DataStoreTest TestWindow = (DataStoreTest)EditorWindow.GetWindow(typeof(DataStoreTest));
         TestWindow.title = "Test Window";
     }
 
     void OnInspectorUpdate()
     {
         Repaint();
     }
 
     void OnGUI()
     {
         if(Selection.activeGameObject != null)
         {
             shrinkvalue = EditorGUILayout.IntSlider("Shrink", shrinkvalue, 1, 10);
             SaveValue();
         }
     }
     
     void OnSelectionChange()
     {
         if (Selection.activeGameObject != null)
         {
             shrinkvalue = PlayerPrefs.GetInt(Selection.activeObject.name.ToString());
             
         }
     }
 
     void OnLostFocus()
     {
         SaveValue();
     }
 
     void OnDisable()
     {
         SaveValue();
     }
 
     void OnDestroy()
     {
         SaveValue();
     }
 
     void SaveValue()
     {
         if(Selection.activeGameObject != null)
         {
             PlayerPrefs.SetInt(Selection.activeObject.name.ToString(), shrinkvalue);
             Debug.Log(PlayerPrefs.GetInt(Selection.activeObject.name.ToString()));            
         }
     }
 }

Plz reply ASAP!!! Thanks in Advance!

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
0

Answer by Redeemer86 · Sep 04, 2014 at 04:35 AM

You can try this roundabout way, if I get what you are saying:

  1. Make an empty game object in Test1.scene

  2. Attach a script to hold slider value on this game object

  3. Use Object.DontDestroyOnLoad when Sitching from Test1.scene to Test2.scene

  4. Extract the stored slider value from empty game object loaded in scene to your slider script.

Hope this is what you are looking for ...

Comment
Add comment · Show 2 · 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 uprashanth · Sep 04, 2014 at 04:47 AM 0
Share

Thank You my friend!! Your solution helped me! It was working with the code I've written immediately after I asked the question here....

Here is the working code!

 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 
 
 public class DataStoreTest : EditorWindow
 {
 
     public int shrinkvalue;
 
 
 
     public string SceneName;
     public string previousScene;
     void OnEnable()
     {
         string sceneName = EditorApplication.currentScene;
         string[] lines = Regex.Split(sceneName, "/");
         SceneName = lines[lines.Length - 1];
         previousScene = SceneName;
 
         if(Selection.activeObject != null)
         {
             shrinkvalue = int.Parse(PlayerPrefs.GetString(SceneName + Selection.activeObject.name));
         }
     }
 
 
     [$$anonymous$$enuItem("Temple $$anonymous$$ystery/ Test")]
     static void Init()
     {
         DataStoreTest TestWindow = (DataStoreTest)EditorWindow.GetWindow(typeof(DataStoreTest));
         TestWindow.title = "Test Window";
     }
 
     void OnInspectorUpdate()
     {
         Repaint();
     }
 
     void OnGUI()
     {
         if(Selection.activeGameObject != null)
         {
             shrinkvalue = EditorGUILayout.IntSlider("Shrink", shrinkvalue, 1, 10);
             //Test.$$anonymous$$axUse = shrinkvalue;
             SaveValue();
         }
         if(Selection.gameObjects.Length > 1)
         {
 
         }
     }
     
     void OnSelectionChange()
     {
         if (Selection.activeGameObject != null)
         {
             shrinkvalue = int.Parse(PlayerPrefs.GetString(SceneName + Selection.activeObject.name)); 
         }
     }
 
     void OnLostFocus()
     {
         if(previousScene != SceneName)
         {
             return;
         }
         else
             SaveValue();
     }
 
     void OnDisable()
     {
         SaveValue();
     }
 
     void OnDestroy()
     {
         SaveValue();
     }
 
     void SaveValue()
     {
         if(Selection.activeGameObject != null)
         {          
             PlayerPrefs.SetString(SceneName + Selection.activeObject.name, shrinkvalue.ToString());
             Debug.Log(PlayerPrefs.GetString(SceneName + Selection.activeObject.name));
         }
     }
 }

 


avatar image Redeemer86 · Sep 04, 2014 at 04:52 AM 0
Share

Good to hear that it worked out for you ... Please set this as Solved ...

Have a nice day ...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to save progress in the game? 1 Answer

Saving for a Level Editor 1 Answer

Updating main menu using PlayerPrefs when collecting an object in different scene (level) 1 Answer

PlayerPrefs performing function even when it doesn't have that key 2 Answers

How to save different values with same script? 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