- Home /
Question by
Radical_boy · Mar 24, 2021 at 08:45 AM ·
c#uiplayerprefssave
How to make a Saved/Favorites system in unity?
I have no idea where to start with this or how to search for more info on this topic so i am coming here for advice. I want to make a Saved system where the player can press a Button and get the Object to appear on a Favorite list(list of buttons that take you to the Game scene shown on them).
I have managed to make this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class CustomSave : MonoBehaviour
{
public Button Custom;
public Button Save;
public Button Save1;
int a = 0, b = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (a>b)
{
if (PlayerPrefs.GetInt("Place") == 1 && PlayerPrefs.GetInt("1") == 0)
{
RectTransform rt = Custom.GetComponent<RectTransform>();
rt.anchoredPosition = new Vector2(-285, 435);
PlayerPrefs.SetInt("1", 1);
PlayerPrefs.SetInt("2", 0);
}
if (PlayerPrefs.GetInt("Place") == 2 && PlayerPrefs.GetInt("2") == 0)
{
RectTransform rt = Custom.GetComponent<RectTransform>();
rt.anchoredPosition = new Vector2(245, 435);
PlayerPrefs.SetInt("2", 1);
}
if (PlayerPrefs.GetInt("0") == 0)
{
RectTransform rt = Custom.GetComponent<RectTransform>();
rt.anchoredPosition = new Vector2(-2000, 700);
PlayerPrefs.SetInt("1", 0);
PlayerPrefs.SetInt("0", 1);
}
b++;
}
}
public void CustomSaved()
{
a++;
PlayerPrefs.SetInt("CustomSaved", PlayerPrefs.GetInt("CustomSaved") +1);
if (PlayerPrefs.GetInt("CustomSaved") % 2 != 0)
{
var colors0 = Save.GetComponent<Button>().colors;
colors0.normalColor = Color.red;
Save.GetComponent<Button>().colors = colors0;
var colors1 = Save1.GetComponent<Button>().colors;
colors1.normalColor = Color.red;
Save1.GetComponent<Button>().colors = colors1;
PlayerPrefs.SetInt("Place", PlayerPrefs.GetInt("Place")+1);
}
if (PlayerPrefs.GetInt("CustomSaved") % 2 == 0 && PlayerPrefs.GetInt("CustomSaved") !=0)
{
var colors = Save.GetComponent<Button>().colors;
colors.normalColor = Color.white;
Save.GetComponent<Button>().colors = colors;
var colors2 = Save1.GetComponent<Button>().colors;
colors2.normalColor = Color.white;
Save1.GetComponent<Button>().colors = colors2;
PlayerPrefs.SetInt("Place", PlayerPrefs.GetInt("Place") - 1);
PlayerPrefs.SetInt("0", 0);
}
}
}
The void CustomSave() is assigned to the buttons Save1 and Save. This works only when one of the Save/Save1 buttons are presed as else it would trigger scripts from multiple objects and it would be all glitchy. It took me a while but i am sure there are better ways for it to work but i ust cant figure it out...
Comment