Photon UNITY c# script for PUN asset update notification

MT3
MT3
edited October 2015 in Tutorials and Code Sharing
Greetings!

I felt I wanted an update feature for PUN similar to the UNITY "update available" message. I would like to share it in case anyone else might like this feature. It uses concepts that could serve a number of purposes, like grabbing a scoreboard from the internet once a day.

It is implemented in a single script that should be added to an object in your games "lobby scene" so that PUN is active.

It uses the WWW object to read the current release from the PUN thread on the UNITY web site.

It employs PlayerPrefs to facilitate a "once a day" notification via a legacy GUI window (for backward compatibility).

The script only works while running your app in the Unity editor and will not function (or malfunction) in compiled builds.

I'm an old school programmer and probably didn't structure this script or comment as well as could have been done... so I'm hoping someone might find it entertaining to improve on this (especially my brute force parsing) and enlighten us all to a better way. I also hope I've not committed a transgression with the way I've posted the script :s

Hope you enjoy.

using UnityEngine;
using System.Collections;
using System.IO;

public class PunVersionChecker : MonoBehaviour 
{
	private bool showPunMessage = false;
	private bool closeNow = false;
	private string textFromWWW = "";
	private string message = "";
	private Rect WindowSIZE = new Rect((Screen.width * .5f)-300,(Screen.height * .5f)-150,600,300);
	private string url = "http://forum.unity3d.com/threads/photon-unity-networking.101734/";// the official PUN thread at Unity forums
	//private string url = "https://www.assetstore.unity3d.com/en/#!/content/1786";// the PUN package in Unity Asset Store

	void Start ()
	{
		//lines to remove these keys if you want to remove them
		//PlayerPrefs.DeleteKey("PMonth");
		//PlayerPrefs.DeleteKey("PDay");

		//this keeps message open as opposed to showing once a day
		//PlayerPrefs.SetString("PMonth","1");
		//PlayerPrefs.SetString("PDay","1");
		//PlayerPrefs.Save();

		CheckIfDisplayNow ();
		if (showPunMessage == false) {
			this.enabled = false;
		} else {
			#if UNITY_EDITOR
				StartCoroutine (GetTextFromWWW ());
			#else
				this.enabled = false;
			#endif
		}
	}
	
	void OnGUI()
	{
		if (showPunMessage && message != "")WindowSIZE = GUI.Window(0, WindowSIZE, MyWindow,"New PUN version available.");
		
		/*
		//This routine writes the file to pc hard drive and shows path to file in debug window. Use to examine returned www.text.
		if(GUI.Button(new Rect(0,0,200,100),"write file"))
	    {
		StreamWriter fileWriter = null;
		string fileName = Application.persistentDataPath + "/PUN.txt";
		Debug.LogError (fileName);
		fileWriter = File.CreateText(fileName);
		fileWriter.WriteLine(textFromWWW);
		fileWriter.Close();
		}
		*/
	}

	void Update(){
		if (textFromWWW != "")ParseWebPage ();
		if(closeNow)this.enabled = false;
	}

	private void MyWindow(int id){
		GUI.skin = null;
		GUIStyle myStyle = new GUIStyle();
		myStyle.fontSize = 17;
		myStyle.normal.textColor = Color.yellow;
		myStyle.alignment  = TextAnchor.MiddleCenter;
		
		GUIStyle mButStyle = new GUIStyle(GUI.skin.button);
		mButStyle.fontSize = 12;
		mButStyle.normal.textColor = Color.white;
		mButStyle.alignment  = TextAnchor.MiddleCenter;
	
		if (message != "")
		{
			GUILayout.Box(message, myStyle);
		}
		if (GUI.Button (new Rect (250, 180, 100, 50), "Close", mButStyle)) {
			closeNow = true;
		}
		GUI.DragWindow();
	}

	void CheckIfDisplayNow(){
		string theMonth = System.DateTime.Now.Month.ToString();
		string theDay = System.DateTime.Now.Day.ToString();
		string oldMonth = PlayerPrefs.GetString("PMonth");
		string oldDay = PlayerPrefs.GetString("PDay");
		if (oldMonth == "" || oldMonth == null)	oldMonth = "0";
		if (oldDay == "" || oldDay == null)	oldDay = "0";
		if (int.Parse(theMonth) > int.Parse(oldMonth) || int.Parse(theDay) > int.Parse(oldDay))showPunMessage = true;
		PlayerPrefs.SetString("PMonth",theMonth);
		PlayerPrefs.SetString("PDay",theDay);
		PlayerPrefs.Save();
	}

	void ParseWebPage(){
		if(textFromWWW == "")return;
		string NewVersion = "";
		textFromWWW = textFromWWW.Replace("\r","");
		textFromWWW = textFromWWW.Replace("\n","");
		textFromWWW = textFromWWW.Remove(0, textFromWWW.IndexOf("cloud-banner.jpg")+16);//nothing above this banner is needed
		float newValue=0;
		float oldValue=0;
		if(textFromWWW.IndexOf("</div><br /><b><br />v") > 0){
			textFromWWW = textFromWWW.Remove(0, textFromWWW.IndexOf("</div><br /><b><br />v")+21);//leave 'v' as first character
			int tempNumber = textFromWWW.IndexOf(")");//version data ends on this character
			NewVersion = textFromWWW.Remove(tempNumber+1, textFromWWW.Length-(tempNumber+1));
			tempNumber = NewVersion.IndexOf("(");
			string ss = NewVersion.Remove(tempNumber, NewVersion.Length-tempNumber );
			ss = ss.Replace("v", "");
			ss = ss.Trim();
			newValue = float.Parse(ss);
			oldValue = float.Parse(PhotonNetwork.versionPUN);
			textFromWWW = "";
		}
		 if(newValue > oldValue )message = "\n\n\nPUN new version available: " + NewVersion + "\n\nPUN installed version: " + PhotonNetwork.versionPUN;
	}
	
	IEnumerator GetTextFromWWW ()
	{
		WWW www = new WWW(url);
		yield return www;
		if (www.error != null)
		{
			Debug.LogError("Something went wrong trying to read the web page..." + www.error);
		}
		else
		{
			textFromWWW = www.text;
		}
	}
}

Comments

  • Nice!
    Do you mind fixing the formatting? Wrap the script in "code" tags or upload the .cs file as attachment?
  • Sorry I just don't see any way to upload the script :(

    Usually, the script will self-format if pasted into a script while in Monodevelop... not much but maybe it'll help?