- Home /
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!
Answer by Redeemer86 · Sep 04, 2014 at 04:35 AM
You can try this roundabout way, if I get what you are saying:
Make an empty game object in Test1.scene
Attach a script to hold slider value on this game object
Use Object.DontDestroyOnLoad when Sitching from Test1.scene to Test2.scene
Extract the stored slider value from empty game object loaded in scene to your slider script.
Hope this is what you are looking for ...
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));
}
}
}
Good to hear that it worked out for you ... Please set this as Solved ...
Have a nice day ...