0
\$\begingroup\$

I have two scenes: Game and Loading.

In the Loading scene I have number of slots in this case 10 saved game slots and I can change the slots number each time.

I want to make that when I'm saving the game in the Game scene it will update the saved game slots in the Loading scene either if number of slots already exist or to make that when I'm saving the game it will create automatic a saved game slot in the Loading scene. I want to have this two options with already created slots then when saving the game each time add the date and time to slot 1 slot 2 slot 3....or when saving the game create a slot and add the date time to the created slot so again there will be slot 1 slot 2 slot 3...

This script I'm using the Game scene to save the current game progress:

using UnityEngine;
using System.Collections;
using System.IO;

public class SavingGame : MonoBehaviour
{
    public int resWidth = 1920;
    public int resHeight = 1080;
    public SaveLoad saveLoad;

    public static string ScreenShotName(int width, int height)
    {
        return string.Format("{0}/Saved Screenshots/SavedGameSlot_{1}x{2}_{3}/SavedGameSlot_{1}x{2}_{3}.png",
            Application.persistentDataPath,
            width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    }

    void Update()
    {
        if (Input.GetKeyDown("k"))
        {
            string filename = ScreenShotName(resWidth, resHeight);
            string directory = Path.GetDirectoryName(filename);
            Directory.CreateDirectory(directory);
            ScreenCapture.CaptureScreenshot(filename);
            StartCoroutine(saveLoad.SaveWithTime(directory,Path.GetFileNameWithoutExtension(filename) + ".savegame.txt"));
        }
    }
}

After pressing the k key I have a screenshot of the game view window and a saved game file for example :

The image type is png.

Saved image screenshot and saved game file on the hard disk

In the Loading scene I have this script that I use to create the saved games slots, in this case 10 slots, and I want to add to this script a bool variable to be able to decide whether to create a number of slots or if the flag bool is false to create the slots automatically when saving the game in the Game scene:

I typed manually the date and time to show the format I want it to be. Inside the circle's image UI I want to add the saved png screenshot file.

The variable LevelButtonText is where the date and time should be.

slots

This script in the Loading scene create the saved games slots:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// LevelsScrollViewController - generate scrollview items
/// handle all things those required for scrollview controller
/// </summary>
public class LevelsScrollViewController : MonoBehaviour
{

    [SerializeField] Text levelNumberText;
    [SerializeField] int numberOfLevels;
    [SerializeField] GameObject levelBtnPref;
    [SerializeField] Transform levelBtnParent;

    private void Start()
    {
        LoadLevelButtons();
    }

    // load level buttons on game start
    private void LoadLevelButtons()
    {
        for (int i = 0; i < numberOfLevels; i++)
        {
            GameObject levelBtnObj = Instantiate(levelBtnPref, levelBtnParent) as GameObject;
            levelBtnObj.GetComponent<LevelButtonItem>().levelIndex = i;
            levelBtnObj.GetComponent<LevelButtonItem>().levelsScrollViewController = this;
        }
    }

    // user defined public method to handle something when user press any level button
    // at present we are just changing level number, in future you can do anything that is required at here
    public void OnLevelButtonClick(int levelIndex)
    {
        
    }
}

I also have this prefab LevelBtn with the OnLevelButtonClick event and the script LevelButtonItem:

LevelBtn prefab

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// LevelButtonItem - attached to level button
/// handle specific button related actions
/// </summary>
public class LevelButtonItem : MonoBehaviour
{
    [HideInInspector] public int levelIndex;
    [HideInInspector] public LevelsScrollViewController levelsScrollViewController;

    [SerializeField] Text levelButtonText;

    Button btnenablestate;

    private void Start()
    {
        levelButtonText.text = (levelIndex + 1).ToString();

        btnenablestate = transform.GetComponent<Button>();

        if (levelButtonText.text == "")
        {
            btnenablestate.enabled = false;
        }
    }

    private void Update()
    {
        if (levelButtonText.text == "")
        {
            btnenablestate.enabled = false;
        }
    }

    // click event of level button
    public void OnLevelButtonClick()
    {
       // To load saved game here
    }
}

The problem is how to use the LevelBtn prefab or other ways to pass the formatted date and time and the png image to the Loading scene to the right saved slot each time the next saved slot if already created or if so to create a new one each time pressing the k key when saving the game?

\$\endgroup\$
1
  • \$\begingroup\$ This question is somewhat long and convoluted, involving many steps that seem like they could be broken down into smaller questions. For the question of how to share data between scenes, be sure to search for previous Q&A, like this one. \$\endgroup\$
    – DMGregory
    Jul 30 at 18:41

0

You must log in to answer this question.

Browse other questions tagged .