Examples

Usage Since 1.4.0

The examples have been included in the package and are imported when the Rest Server package is installed. They can be found in the package directory (usually de.bearo.restserver) in the subdirectory Samples.

Usage Deprecated since 1.4.0

The basic usage example can be downloaded here. How to run:

  • Download the zip file and extract it.
  • Open the restserver-basicusage directory with Unity.
  • “Enter Safe Mode?” - Ignore the compilation errors and open the project.
  • Import the “RestServer” package via the package manager.
  • All compilation errors should be fixed.
  • Open the SampleScene.
  • Read the “README.md” file.

Example Description

Note: In all examples the Http Methods are not chosen according to the REST standard or best practices, just for ease of use for the user.

Simple REST Server Example Since 1.4.0

Simple REST Server usage. This example has the following endpoints

  • GET /position:
    • Returns the current position of the Cube (rest controlled) game object: {"x":0, "y":0, "z":0}
    • If a query parameter is given, like http://localhost:8080/position?x=3&y=-2, then the Game object is moved to that position
  • POST /position:
    • Accepts json in the form {"x":0, "y":0, "z":0} and sets the position of the Cube (rest controlled) game object to the given one. Missing coordinates will be interpreted as zero.
  • GET /triggeranimation:
    • Moves the game object Cube (rest controlled) on the positive x-axis over 10 seconds.

Stream Deck Since 1.4.0

This shows how Unity can be easily controlled with a Stream Deck (or similar). The Stream Deck has a built-in method to call GET endpoints (System -> Webpage, check “Get request in background”). This example has the following endpoint:


Twitch Overlay Since 1.4.0

This shows how a Twitch Overlay for OBS (or similar apps) can be done. The background is a uniform color, that can be removed in OBS. The scene could easily be controlled via a Stream Deck. This example has the following endpoint:

  • GET /new_follower:
    • Shows the text “New Follower!” for 5 seconds

Simple VS Example (Visual Scripting) Since 1.4.0

This shows how visual scripting can be used. The background is a uniform color, that can be removed in OBS. The scene could easily be controlled via a Stream Deck. This example has been created with visual scripting and exposes the following endpoint:

  • GET /move:
    • Changes the material of the game object and moves it around randomly.

Animation Interlock Example Since 1.4.0

Shows the usage of the new SimpleInterlock class, which helps implementations where repeated requests against the same endpoint do not restart a animation while it’s running. With Disable Interlock the effects without the Interlock class can be experienced and how the animation can be made look broken.

  • GET /move:
    • Changes the material of the game object and moves it around randomly.

Binary Example Since 1.4.0

Shows the implementation of an endpoint which let the user download an image

  • GET /image:
    • Displays the image of a Nyan cat

Static Content Example New in 1.5.0

The static content example shows a simple angular application that is served using the rest server (compressed in the sample-app.zip). The app allows to show/hide a cube.

  • GET / (and more):
    • Retrieves the example javascript app from the sample-app.zip. To aid navigation, every path that can’t be found will be redirected to the main /index.html.
  • GET /static/:
    • Returns a simple text file served also as static content.

Reverse Proxy Example New in 1.5.0

This example implements a reverse proxy. In this case it reverse proxies this documentation pages.

  • GET /.*:
    • Redirects the request to this documentation site.