Skip to content

Instantly share code, notes, and snippets.

@Eugeny
Created April 14, 2012 17:15
Show Gist options
  • Save Eugeny/2386021 to your computer and use it in GitHub Desktop.
Save Eugeny/2386021 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
using System;
using System.Threading;
public class GamePub : MonoBehaviour
{
public const string BASE_URI = "http://pub.mobilwerx.com/";
public const string GAME_ID = "1";
public GUISkin Skin;
private string DeviceID;
public void Request (string uri, string prms, Action<object> callback, object cba)
{
new Thread (new ThreadStart (delegate {
WebRequest req = HttpWebRequest.Create (CreateURI (uri) + prms);
WebResponse resp = req.GetResponse ();
/*Stream s = resp.GetResponseStream ();
StreamReader sr = new StreamReader (s);
return sr.ReadToEnd ();*/
if (callback != null)
callback (cba);
})).Start ();
}
string CreateURI (string uri)
{
return BASE_URI + uri + "?devid=" + GetDeviceID () + "&gameid=" + GAME_ID;
}
public void Navigate (string uri)
{
Application.OpenURL (CreateURI (uri));
}
public string GetDeviceID ()
{
return DeviceID;
}
public void SubmitScore (int id, string score)
{
Request (String.Format ("api/score/submit/{0}/{1}", id, Uri.EscapeUriString (score)), "", ShowMessage, "Highscore submitted");
}
public void ShowMessage (object msg)
{
messageTime = 5;
message = msg.ToString ();
}
private float messageTime = 0;
private SmoothFloat messageAlpha = new SmoothFloat ();
private string message = null;
void OnGUI ()
{
GUI.skin = Skin;
if (message != null) {
GUI.color = new Color (1, 1, 1, messageAlpha.Value);
GUI.Box (new Rect (10, Screen.height - 70, 200, 60), "", "Bar");
GUI.Label (new Rect (15, Screen.height - 65, 190, 50), message);
}
}
void Update ()
{
messageTime -= Time.deltaTime;
messageAlpha.Update (messageTime > 0 ? 1 : 0);
}
public static GamePub instance;
public void Awake ()
{
instance = this;
DontDestroyOnLoad(gameObject );
DeviceID = SystemInfo.deviceName + " " + SystemInfo.deviceUniqueIdentifier;
useGUILayout = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment