Skip to content

Instantly share code, notes, and snippets.

@codehoose
Last active December 11, 2018 08:50
Show Gist options
  • Save codehoose/1d0de5fdc159c59b30d870b01f9da926 to your computer and use it in GitHub Desktop.
Save codehoose/1d0de5fdc159c59b30d870b01f9da926 to your computer and use it in GitHub Desktop.
public static class CloudFactory
{
public static ICloudService Create()
{
if (Application.platform == RuntimePlatform.WindowsPlayer)
return new SteamCloudService();
}
}
public class GameSystems : MonoBehaviour
{
private static GameSystems _instance;
public static GameSystems Instance
{
get
{
if (_instance == null)
{
_instance = GameObject.FindObjectOfType<GameSystems>();
if (_instance == null)
{
var go = new GameObject("__GAMESYSTEMS__");
_instance = go.AddComponent<GameSystems>();
_instance.Cloud = CloudFactory.Create();
_instance.FileIO = FileSystemFactory.Create();
}
}
}
}
public ICloudService Cloud { get; private set; }
public IFileSystem FileIO { get; private set; }
}
public interface ICloudService
{
void RequestLeaderboard(int userId, Action<ILeaderboardData> callback);
}
public class SteamCloudService : ICloudService
{
public void RequestLeaderboard(int userId, Action<ILeaderboardData> callback)
{
// Example code, not complete
SteamInterface.Leaderboards.Fetch(OUR_LEADERBOARD, (boardData) => {
var converted = ToGameFormat(boardData);
callback(converted);
});
}
private ILeaderboardData ToGameFormat(ISteamLeaderboard leaderboard)
{
// TODO: Convert from Steam format to in-game format
return data;
}
}
@codehoose
Copy link
Author

GameSystems is the singleton for the game that gives the rest of the classes access to the game's systems. In this example, I have a singleton that has two systems; a cloud service and a file service. I implemented basic versions of the cloud service to illustrate the point, file system would be similar.

An example call to get the leaderboard data and populate the UI might look like this:

GameSystems.Instance.Cloud.RequestLeaderboard(userId, boardData => {
    PopulateUI(boardData);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment