Some help with Data Save/Load would be most appreciated
Let me preface my question with the fact that I am a graphic designer and not a programmer, but I am desperately trying to learn.
I am building an app with a login screen, the user would type in their first and last name, then tap the login button. The app will run on a standalone device, but more than one person may pick up that device and log into it.
I have built the UI using the Unity Editor, and laid it all out nice a pretty, exactly the way I'd like it to look. Now, how do I make it function? Okay, through my searching, I have managed to cobble together these two scripts:
This one SAVES the data:
using UnityEngine;
using System.Collections;
public class SaveData : MonoBehaviour {
string firstName = "";
string lastName = "";
void OnGUI(){
firstName = GUI.TextField (new Rect (420, 300, 346, 50), firstName);
lastName = GUI.TextField (new Rect (420, 365, 346, 50), lastName);
if (GUI.Button (new Rect (420,430,346,50), "Login")){
PlayerPrefs.SetString ("name", firstName);
}
}
}
And this one LOADS the data:
using UnityEngine;
using System.Collections;
public class LoadData : MonoBehaviour {
string firstName = "";
// Use this for initialization
void Start () {
firstName = PlayerPrefs.GetString ("name");
}
void OnGUI(){
GUI.Label (new Rect (88, 62, 300, 50), firstName);
}
}
Here's my problem: While this does indeed FUNCTION, the scripting actually creates the input fields and login button... and man, they are ugly... they do not "go" with my design at all.
Now, I did look a bit at GUISkins, but that seems a bit complex - but if that's my only option I'll take on the challenge.
I know that with other scripts, I've created lines like:
public gameObject LittleMan;
Which then placed an "entry box" into which I could drag my LittleMan game object from the hierarchy into it, and thus attach it to the script.
My question is this: Is there a way that my scripts can be re-written to produce little "entry boxes" in the inspector, into which I can link items from the hierarchy to, so that the script will function, but my design will remain intact?
For reference, here are some screenshots as to what I'm talking about with regards to the design aspect:
If someone would be kind enough to hold my hand a bit, I'd appreciate your time and effort. Respectfully, Kimber
Answer by TBruce · Apr 09, 2016 at 08:25 PM
@cre8or Your UI dialog looks like it does because you are creating it in the OnGui() method.
void OnGUI(){
firstName = GUI.TextField (new Rect (420, 300, 346, 50), firstName); // creates the first blank text field using the default font
lastName = GUI.TextField (new Rect (420, 365, 346, 50), lastName); // creates the second blank text field using the default font
if (GUI.Button (new Rect (420,430,346,50), "Login")){ // creates the Login button using the default font
PlayerPrefs.SetString ("name", firstName);
}
void OnGUI(){
GUI.Label (new Rect (88, 62, 300, 50), firstName); // creates the label using the default font
}
To get WYSIWYG you need to design the dialog UI in the scene before runtime.
You do not really need two scripts to accomplish what you want. I changed the PlayerPrefs key name to make it represent what you are saving.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Logon : MonoBehaviour
{
public InputField firstName;
public InputField lastName;
public Text userLoggedOn;
// Use this for initialization
void Start ()
{
LoadData();
}
// Update is called once per frame
void Update ()
{
}
public void SaveData()
{
if (firstName != null)
{
PlayerPrefs.SetString("FirstName", firstName.text);
}
if (lastName != null)
{
PlayerPrefs.SetString("LastName", lastName.text);
}
}
public void LoadData()
{
if ((firstName != null) && (PlayerPrefs.HasKey("FirstName")))
{
firstName.text = PlayerPrefs.GetString ("FirstName");
if (userLoggedOn != null)
{
userLoggedOn.text = firstName.text;
}
}
if ((lastName != null) && (PlayerPrefs.HasKey("LastName")))
{
lastName.text = PlayerPrefs.GetString ("LastName");
}
}
}
I have attached a project with a working logon screen as well as a screen shot. Good luck!
@$$anonymous$$avina thank you kindly, for your help - and thank you as well, for posting that small working project. It does help me a lot, to be able to see things in context. I'm going to try implementing this, and I'll let you know how things turn out. I appreciate your time, thank you!
You are very welcome. I tried to make it simple but flexible especially to allow an artist to be able to make easy modifications. If you have any questions feel free to ask.
I certainly will ask... thank you very much! If I may ask: Are you a programmer / coder?
Answer by Happy-Zomby · Apr 09, 2016 at 08:13 AM
Hi, to save the data this should work
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SaveData : MonoBehaviour
{
public Text yourFirstNameUIItem;
public Text yourLastNameUIItem;
//you should see these in the inspector and can drag and drop the UI gameobjects where you have the text component linked to first and last name
public void Save()
{
PlayerPrefs.SetString("firstName", yourFirstNameUIItem.text);
PlayerPrefs.SetString("lastName", yourLastNameUIItem.text);
}
}
this script should be placed on your log in button game object then on your log in button you need to look at the button component. And look for the onclick () box change the gameobject to the button gameobject then use the pull down to find the save script and then the save function. When you click the button it you saved the data
to load data - you can attached the below script to an object of your scene to which you want to load the data
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class loadData : MonoBehaviour {
public Text theUIItemToShowTheName;
// Use this for initialization
void Start ()
{
theUIItemToShowTheName.text = PlayerPrefs.GetString("firstName");
}
}
I didn't test this so you may need to fix some issues but it should work. Please do note this is just to store and displays the name you enter - it is note going to be able to save different users and so on. The next time you type a name it will cover over the previous one. Savings users and related data is a much much more complicated item and you'll need to look into tutorials and so on. if you want to learn about unity coding there are a lot of good tutorials out there. hope this helps,
@HappyZomby thank you kindly, for your help. I just saw this post, and I'm going to work on sorting things out. I really appreciate your time. Thank you!
Answer by cre8or · Apr 10, 2016 at 04:42 AM
@Mavina Sorry to bother you with this, but I'm a wee bit stuck with the code/sample you provided.
I have implemented the code into my project. I'm not sure if I was clear in my original posting, but the login screen and the screen in which the name appears next to the little avatar icon are different unity "scenes".
When I implement your script, and I play the login scene in the editor, them click the login button (which has 2 functions attached to it 1. is the login script save 2. is the menu manager go to the next scene.) I am taken to the next scene, but the name is not populated in the place in which it should be. It's empty.
However, it's partially working, because when I close the player, and restart the login scene, my name is pre-populated, without having to retype it.
Now, since this script controls events on 2 different scenes, would I need two scripts to make it work: a save for the first one and a load for the second?
Or... maybe a better question might be: How do I trigger the LOAD portion of the script to populate the name in the text area beside the icon...
Of course my little project was just an example implementing what you were looking to achieve but only in a single scene.
But to answer the question about how to trigger the load you would do something like this:
// allow this to be modified in the inspector
public string sceneToLoadOnLogin = "sceneToLoadOnLogin";
public void Save()
{
// method logic here
// very last line
if (sceneToLoadOnLogin != "")
{
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene(sceneToLoadOnLogin);
// the above line can also be done like so
// uses UnityEngine.Scene$$anonymous$$anagement;
// Scene$$anonymous$$anager.LoadScene(sceneToLoadOnLogin);
}
}
And yes this could be done in two scripts but it could also be done in a single script if you code it properly. There would of course be other factors to take into account (which could include company policies or scripting standards).
Thank you very kindly for your help, $$anonymous$$avina. I really appreciate your time. Sorry I haven't replied earlier - I'm just able to get back to this now.
I hope that I was able to help you enough to get you through your problem. Please pardon me for asking but if you feel that I was able to answer your original question then would you please be so kind as to reward me for answering your question and Upvote the answer if you don't $$anonymous$$d? Thank yo!