Skip to content

Instantly share code, notes, and snippets.

@troy-lamerton
Last active April 20, 2017 12:30
Show Gist options
  • Save troy-lamerton/28148856df9a4f2eac66279b197fa257 to your computer and use it in GitHub Desktop.
Save troy-lamerton/28148856df9a4f2eac66279b197fa257 to your computer and use it in GitHub Desktop.

Common code snippets

void RestartGame() {
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
void PauseGame () {
    Time.timeScale = 0;
    // ShowPauseMenu();
    // also stop accepting input, to move the player etc
    // you can do this by setting a global static variable
    // -- how? see "Data between scenes" section of this doc
}
void ResumeGame () {
    Time.timeScale = 1;
    // HidePauseMenu();
    // allow user input
}

Canvas UI

Move UI element to cursor location

void PositionAtCursor(Transform transform) {
    var screenPoint = Input.mousePosition;
    // TODO: use rectTransform component
    transform.position = screenPoint;
    // works on any object using a rect transform
}

Button - Adding OnClick in the inspector is limited to methods with only one simple parameter

Button click - Call any method with any arguments

Movement

Click to move there

Top-down movement

Data between scenes

Three major ways:

  1. Save info into file using Unity's PlayerPrefs
  2. Static class data
  3. DontDestroyOnLoad with a Singleton object

Example code from tutorial: https://gist.github.com/troy-lamerton/f895b856d2972e9b9f1bb69133a32574
Implemented GlobalState class: https://gist.github.com/troy-lamerton/6701c62763fb29723a750f78bab22163
Youtube source: https://www.youtube.com/watch?v=WchH-JCwVI8 (1hr 10min)

Miscellaneous

Mine a fixed rock, earning 1 point every second

GetGoop example script

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