Skip to content

TeachMeInc/SWAG-API-Unity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SWAG API - Unity Plugin & Example

This example shows how to use the SWAG API with Unity.

See SwagAPI.cs for a list of available plugin methods.

See this PDF for more comprehensive documentation. (subject to change)

An end-to-end example can be found in this repository in the SampleScene scene.

Setting up the scene using the SWAG template

  1. Add a SwagObj to your scene. Make sure the SwagAPI script is attached.
  2. Navigate to the WebGL player settings (Edit -> Project Settings -> Player -> WebGL settings) and choose the SWAGAPI template.
  3. Navigate to the project build settings (File -> Build Settings) and choose the WebGL platform.
  4. Build the project!

Setting up the scene using a custom template

  1. Add a SwagObj to your scene. Make sure the SwagAPI script is attached.
  2. Add the following snippets of HTML to your template:

Just before the closing </head> tag:

<script type="text/javascript" src="https://swagapi.shockwave.com/dist/swag-api.js"></script>   
<link rel="stylesheet" type="text/css" href="https://swagapi.shockwave.com/dist/swag-api.css"></link>

Just after the opening <body> tag (replacing the existing <canvas> tag):

<div id="webgl" class="webgl-content">
  <div id="swag">
    <canvas id="unity-canvas" width={{{ WIDTH }}} height={{{ HEIGHT }}} style="width: {{{ WIDTH }}}px; height: {{{ HEIGHT }}}px; background: {{{ BACKGROUND_FILENAME ? 'url(\'Build/' + BACKGROUND_FILENAME.replace(/'/g, '%27') + '\') center / cover' : BACKGROUND_COLOR }}}"></canvas>
    <div class="footer">
      <div class="webgl-logo"></div>
      <div class="fullscreen" onclick="unityInstance && unityInstance.SetFullscreen(1)"></div>
      <div class="title">{{{ PRODUCT_NAME }}}</div>
    </div>
  </div>
</div>

Update your createUnityInstance call to initialize the SWAG API when ready:

createUnityInstance(document.querySelector("#unity-canvas"), {
  ...
})
.then((unityInstance) => {
  window.unityInstance = unityInstance;

  document.swagBrandingShown = 0;
  SWAGAPI.showBrandingAnimation('webgl', function (){
    document.swagBrandingShown = 1;
  });
})
  1. Navigate to the project build settings (File -> Build Settings) and choose the WebGL platform.
  2. Build the project!

Developing and Using Locally

For now you will need to edit your etc\hosts file.

  • On Windows you can find it at c:windows\system32\drivers\etc\host
  • On MacOS you can find it at /etc/host

and add the following line

127.0.0.1 local.shockwave.com

Install http-server

npm install -g http-server

Launch a local web server from your /build directory, where your index.html is

http-server -p 8888

launch your game with http://local.shockwave.com:8888

Other Notes

  1. The SWAG API is provided via a JSLIB external plugin. There is currently a quirk in Unity which prevents the plugin from working in editor mode, so you will need to build your project to test SWAG API features in your game.
  2. SWAG API calls are secured via CORS and as such will only work properly in dev mode when coming from local.addictinggames.com or local.shockwave.com on port 8888. You will need to add these to your HOSTS file and run your game using an external web server to get passed CORS restrictions (ie Unity's "Build and Run" won't work for testing SWAG API features).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •