Skip to content

Instantly share code, notes, and snippets.

@onevcat
Last active August 30, 2023 08:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save onevcat/9666d61d481843c41d67caf08467def1 to your computer and use it in GitHub Desktop.
Save onevcat/9666d61d481843c41d67caf08467def1 to your computer and use it in GitHub Desktop.
Final script for UniWebView "Working with Code" sample
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
UniWebView webView;
// Use this for initialization
void Start () {
// Create a game object to hold UniWebView and add component.
var webViewGameObject = new GameObject("UniWebView");
webView = webViewGameObject.AddComponent<UniWebView>();
// More to add...
webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
webView.Load("https://docs.uniwebview.com/game.html");
webView.Show();
webView.OnPageFinished += (view, statusCode, url) => {
webView.EvaluateJavaScript("startGame();", (payload)=>{
if (payload.resultCode.Equals("0")) {
Debug.Log("Game Started!");
} else {
Debug.Log("Something goes wrong: " + payload.data);
}
});
};
webView.OnShouldClose += (view) => {
Debug.Log("miao");
webView = null;
return true;
};
webView.OnMessageReceived += (view, message) => {
if (message.Path.Equals("game-over")) {
var score = message.Args["score"];
Debug.Log("Your final score is: " + score);
webView.EvaluateJavaScript("getStars(" + score + ");", (payload)=>{
Debug.Log("You got " + payload.data + " stars!");
});
Invoke("Restart", 3.0f);
}
if (message.Path.Equals("close")) {
Destroy(webView);
webView = null;
}
};
}
void Restart() {
if (webView != null) {
webView.Reload();
}
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment