Giter VIP home page Giter VIP logo

unity-project-prefs's Introduction

Per-Project Preference Utility for Unity Tools Development

by Ming-Lun "Allen" Chou / AllenChou.net / @TheAllenChou / Patreon

This is a per-project preference utility useful for Unity tools development. It's similar to Unity's EditorPrefs, except that it stores key-value record pairs as a scriptable object asset in the current project rather than modifying the machine's registry like EditorPrefs. This utility is editor-only and should live in an Editor folder.

The default scriptable object UI is extended to further provide utilities for reordering, sorting, and record type enforcement. Supported record types include: boolean, int, float, string, and string set (unique strings joined by semicolons).

turbulent-rainbow-cubes

As an example, your tool might need a start screen that contains a check box for specifying when to automatically launch the start screen, e.g. never, on newer version, etc. This requires a way to store and access persistent data specific to this project, which can be done using this utility. Here's some sample code for this example:

using namespace LongBunnyLabs;

enum LaunchMode
{
  Never, 
  OnNewerVersion, 
};

string LaunchModeKey = "LaunchModeKey";
string LastLaunchedVersionKey = "LastLaunchedVersion";

// find int value for the "LaunchModeKey" key
LaunchMode launchMode = (LaunceMode) ProjectPrefs.GetInt(LaunchModeKey, (int) LaunchMode.OnNewerVersion);

// find int value for the "LastLaunchedVersion"
int LastLaunchedVersion = ProjectPrefs.GetInt(LastLaunchedVersionKey, -1);

bool launch = false;
switch (launchMode)
{
  case LaunchMode.Never:
    break;
    
  case LaunchMode.OnNewerVersion:
    launch = (CurrentVersion > LastLaunchedVersion);
    break;
}

if (launch)
{
  LaunchStartScreen();
  ProjectPrefs.SetInt(LastLaunchedVersionKey, CurrentVersion);
}

// ...somewhere in the start screen's scripts
LaunchMode selectedLaunchMode = (LaunchModeEnum) EditorGUILayout.EnumPopup(currentLaunchMode);
ProjectPrefs.SetInt(LaunchModeKey, (int) selectedLaunchMode);

Usage

Upon any interaction with the utility, if the preference asset file doesn't exist, it will be created. The default file location is Assets/ProjectPrefs.asset. This can be changed by modifying ProjectPrefs.InstancePath.

In the ProjectPrefs class, several functions are provided for getting a value corresponding to a given key. If the preference asset file doesn't exist or if such key cannot be found, the default values passed in will be returned.

bool GetBool(string key, bool defaultValue);
int GetInt(string key, int defaultValue);
float GetFloat(string key, float defaultValue);
string GetString(string key, int defaultValue);
string[] GetSet(string key, int defaultValue);

There are also functions for setting the value for a given key.

void SetBool(string key, bool value);
void SetInt(string key, int value);
void SetFloat(string key, float value);
void SetString(string key, string value);

String sets are a bit special. A string set is a single string consisted of a set of unique strings joined by semicolons. This can be useful for representing a series of flags. There are a couple functions for string set queries and set operations.

bool SetContains(string key, string value);
void AddToSet(string key, string value);
void RemoveFromSet(string key, string value);

Functionally, a string set is equivalent to a collection of booleans. Which one to use is up to personal preference. The following two setups are functionally equivalent:

// using a string set
ProjectPrefs.AddToSet("MySet", "A");
ProjectPrefs.AddToSet("MySet", "D");
ProjectPrefs.AddToSet("MySet", "E");

// using booleans
ProjectPrefs.SetBool("Myset:A", true);
ProjectPrefs.SetBool("Myset:B", false);
ProjectPrefs.SetBool("Myset:C", false);
ProjectPrefs.SetBool("Myset:D", true);
ProjectPrefs.SetBool("Myset:E", true);

unity-project-prefs's People

Contributors

theallenchou avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.