Unity
We also suggest that you read the following Unity WebGL export tips. They may help you to reduce the final build size and increase the performance of your game, thus increasing your revenue. Furthermore, you can also have a look at the WebGL Optimizer utility that the CrazySDK provides by default.
Info
If you are updating the SDK, we recommend that you delete the folders CrazySDK
and CrazyOptimizer
(if present) to avoid possible conflicts in case of folder structure change.
Introduction
The SDK contains a sitelock functionality. You will find more information about sitelocking, including whitelisting your domain, in the sitelock section.
The SDK also contains the following parts:
CrazyAds
that contains the ad management system.CrazyEvents
that contains all our other features.
Make sure that you also import the Plugins and WebGLTemplates folders at the root of your project.
CrazyGames supports only compressed builds (either Brotli or Gzip). This means that the development builds won't run in the QA tool, since they are not compressed.
Template
The SDK contains a WebGL template that lets your game run using the same setup as it would on crazygames.com. In order to use it, make sure you have imported the template (/WebGLTemplates/Crazy_5_6+ and /WebGLTemplates/Crazy_2020 folders should be present in your project). Then, in your Build Settings (File/Build Settings...), in the Player Settings, under Resolution and Presentation, among the WebGL Templates, select Crazy_5_6+ if you are using a Unity version prior to 2020 or Crazy_2020 for the 2020 version and after. Now when will build and run your project, it will have all the SDK capabilities as it would on the CrazyGame website.
By default, the SDK is not initialized on your domain, and all the calls to the SDK will produce nothing. However, if you upload the game on your domain using the above-mentioned templates, the SDK will start in demo mode and you will be able to see demo ads/banners and console logs.
Sitelock
Sitelocking is done automatically on game start. It allows your game to run only on CrazyGames.com and our affiliated sites. This ensures that your game cannot be stolen and hosted on other domains, so you do not lose any revenue.
By default, the sitelock will also block the game on your domain. To avoid this, you should add your domain to the list of whitelisted domains in the CrazySDK settings. You can find the settings in CrazySDK/Resources/CrazyGamesSettings
.
Info
When reimporting the SDK, Unity resets these settings.
CrazyAds
Guidelines for Advertisements
Please be sure to read our advertisement guidelines, since your game will be rejected without any feedback if it doesn't follow them.
The CrazyAds
folder exposes a CrazyAds
class that is available anywhere in your code. Import it with using CrazyGames;
or prefix the usage with CrazyGames
.
Playing an Advertisement
Displaying an advertisement is done with a single line of code:
using CrazyGames;
CrazyAds.Instance.beginAdBreak();
// OR: CrazyGames.CrazyAds.Instance.beginAdBreak();
Info
Your game will automatically be paused when the ad is being requested, and be continued when the ad finishes playing.
You can pass functions as arguments that will be called when the ad is completed or if an error happened:
using CrazyGames;
void FixedUpdate()
{
transform.Translate(pushForce * Time.fixedDeltaTime);
if (transform.position.y < -20)
{
print("Player Died! Starting Ad Break!");
// Start playing an ad
CrazyAds.Instance.beginAdBreak(Respawn, AdError);
}
}
void Respawn()
{
print("Ad Finished! Respawning player!");
transform.position = startPos;
rb.velocity = Vector3.zero;
}
void AdError()
{
print("Ad has not been displayed");
Respawn();
}
The AdError
callback is also triggered if the ad is not filled.
Ad Types
We support two different types of advertisements: midgame and rewarded.
Midgame advertisements can happen when a user died, a level has been completed, etc.
Rewarded advertisements can be requested by the user in exchange for a reward (An additional life, a retry when the user died, a bonus starting item, extra starting health, etc.). Rewarded ads should be shown when users explicitly consent to watch an advertisement.
By default beginAdBreak()
shows a midgame advertisement. If you want to display a rewarded ad you should call beginAdBreakRewarded()
on the same principle.
using CrazyGames;
CrazyAds.Instance.beginAdBreak(successCallback, errorCallback) //Midgame ad
CrazyAds.Instance.beginAdBreakRewarded(successCallback, errorCallback) //Rewarded ad
Info
If you don't specify an errorCallback the successCallback will be called even if an error happened.
Demo
The CrazyAds
folder has a CrazyAdsDemo
subfolder that contains the CrazyAdsDemo
scene and its assets.
This scene moves a player across a cube, falls off and dies, which calls an ad.
In the editor, ads will not actually run so you will see a placeholder simulation for 3 seconds.
You can explore the CrazyAdsDemoPlayer.cs
code example that is managing player movements and ad displaying.
You can also change between midgame and rewarded ad types in the inspector when selecting the CrazyAdsDemoPlayer
game object.
CrazyBanners
Size
There are 5 banner sizes available:
- Leaderboard (728x90)
- Medium (300x250)
- Mobile (320x50)
- Main (468x60)
- Large Mobile (320x100)
Getting started
Here is a simple video that will teach you how to add banners to your Unity game.
Steps
- Add the latest CrazyGames Unity SDK package to your project.
- Drag and drop the CrazySDK/CrazyAds/Ressources/CrazyBanner prefab to your scene.
- Set banners' size and position.
- Test with CrazyGames standard resolution (922x487) and a 16:9 ratio, make sure that the banners are not going outside of the screen and that they are not overlapping with any UI elements.
- Adjust banner anchor and positioning if necessary.
- Make sure that the banners are activated when the game starts.
- Trigger the banner display when you want to.
- Remove the banner explicitly when needed. For example, loading a new new scene will remove the banner in Unity’s editor, but it will not be removed on CrazyGames.
- That’s it! You can try your game using the CrazyGames Unity template (included in the package). Make sure to use Crazy_2020 if you are on Unity 2020 and Crazy_5_6+ for all the previous versions.
If you are managing more than one banner at the same time, make sure to call updateBannersDisplay()
only once to update all your banners.
leftBanner.MarkVisible(true);
rightBanner.MarkVisible(true);
CrazyAds.Instance.updateBannersDisplay();
Limitations
- You can display a maximum of 2 banners of the same size at the same time.
- The same banner can be re-displayed only 60 seconds after the last display.
- The banner has to be fully inside the game window.
- If a banner does not follow any of these rules, it will not display. We refer to the advanced features section bellow to detect these cases programmatically.
Advanced Features
Callbacks
Our SDK provides several callbacks to detect the success or failure of displaying a banner ad. These callbacks can be used as follows:
using CrazyGames;
public class MyController : MonoBehaviour
{
public CrazyBanner bannerPrefab;
void Awake()
{
CrazyAds.Instance.listenToBannerError(BannerError);
CrazyAds.Instance.listenToBannerRendered(BannerRendered);
}
private void BannerError(string id, string error)
{
Debug.Log("Banner error for id " + id + ": " + error);
}
private void BannerRendered(string id)
{
Debug.Log("Banner rendered for id " + id);
}
}
Refreshing
You can refresh a visible banner every 60 seconds, with a maximum of 60 refreshes per session. You’ll need to call this code every time you want the banner to be refreshed.
CrazyEvents
The CrazyEvents
folder exposes a CrazyEvents
class that is available anywhere in your code. Import it with using CrazyGames;
or prefix the usage with CrazyGames
A demo is available in the CrazyEventsDemo
folder, you can run the scene and look at the outputs in your console. You can also see the SDK usage in the DemoAssets/ButtonActions.cs
file.
Invite Link
This feature lets you share the Crazygames version of your game to the players and invite them to join a multiplayer game. You can call InviteLink
with parameters that correspond to your game or game room.
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("roomId", "1234");
string inviteLink = CrazyEvents.Instance.InviteLink(parameters);
We provide a helper if you want to automatically copy the invite link to the clipboard.
You can check if the user is currently playing through an invite link with IsInviteLink
.
Finally, you can retrieve parameters passed through the invite link with GetInviteLinkParameter
.
Here is a full example of this feature.
using CrazyGames;
if (CrazyEvents.Instance.IsInviteLink()) {
string roomId = CrazyEvents.Instance.GetInviteLinkParameter("roomId");
if (roomId != null && roomId != "") {
Debug.Log(string.Format("Correct invite url, the roomId is {0}", roomId));
} else {
Debug.Log("Cannot find 'roomId' parameter");
}
} else {
Debug.Log("User is not playing through an invite link");
}
Gameplay
We provide functions that enable us to track when and how users are playing your games. These can be used to ensure our site does not perform resource intensive actions while a user is playing.
The GameplayStart
function has to be called whenever the player starts playing or resumes playing after a break (menu/loading/achievement screen, game paused, etc.).
The GameplayStop
function has to be called on every game break (entering a menu, switching level, pausing the game, ...) don't forget to call GameplayStart
when the gameplay resumes.
using CrazyGames;
// Main menu, user clicks start
CrazyEvents.Instance.GameplayStart();
// Level is over, displaying switching level screen
CrazyEvents.Instance.GameplayStop();
// Next level starts
CrazyEvents.Instance.GameplayStart();
// The player is pausing the game by looking into the menu
CrazyEvents.Instance.GameplayStop();
// The player closes the menu and gets back to the game
CrazyEvents.Instance.GameplayStart();
Happy time
You can call the HappyTime
function on player achievements, it will possibly adapt the website to celebrate!
Info
Use this feature sparingly, the celebration should remain a special moment.
// Player beats a boss, reaches a high score, etc.
CrazyEvents.Instance.HappyTime();
// Player finishes a level, gets an item, etc.
// No need to celebrate
System info
We provide some information about the system, that is contained in the SystemInfo
object. Refer to the following code to know more about fields and format.
GetSystemInfo
receives a callback which is called after the SDK is initialized. If the SDK is initialized, the callback is called instantly.
using CrazyGames;
CrazySDK.Instance.GetSystemInfo(systemInfo =>
{
Debug.Log(systemInfo.countryCode); // US
// For browser and os, format is the same as https://github.com/faisalman/ua-parser-js
Debug.Log(systemInfo.browser.name); // Chrome
Debug.Log(systemInfo.browser.version); // 99.0.2534.75
Debug.Log(systemInfo.os.name); // Windows
Debug.Log(systemInfo.os.version); // 10
Debug.Log(systemInfo.device.type); // possible values: "desktop", "tablet", "mobile"
});