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!