Unity3D for iOS: Storing a New High Score in Parse via UniParse while Adding a Value to a _ptr Field

So far this is the only way I’ve been able to successfully do this. I wonder if I’m missing something & there’s actually a better way to do this using only 1 call to Parse. Anyone? Feel free to prove me wrong. Please.

...
// localStorage is a PlayerPrefs instance that lets us grab values stored on the device that's running the app
...			 

// MyGameUsers is a Parse Class that stores unique user ID's as the parse Class' objectId's
// change MyGameUsers to your own Class name where appropriate.
var usersTablePointer = new ParseClass("/classes/MyGameUsers");

//"f2H4saD8" is an arbitrary value for demo purposes, a typical objectId from a Parse table  
string myQuery = "where={\"objectId\": \""+ "f2H4saD8" + "\" }";             				
			
ParseInstanceCollection prseList = gamerTablePointer.List(myQuery);		

//wait to get the Gamer object w/ our objectID specified in myQuery 
while(!prseList.isDone) yield return null;
			
ParseInstance[] items = prseList.items;		
				
// post a new record to the Score table in Parse:
// change MyGameScores to your own Class name where appropriate.
var myGameScoresClass = new ParseClass("/classes/MyGameScores");

//create a new record in the db table (represented by the ParseClass)
var myNewScore = myGameScoresClass.New();

//1034 is just an arbitrary score value; you can grab it from your own variable value			
myNewScore.Set( "Score", 1034 ); 	
myNewScore.Set( "MyGameUsers_ptr", items[0] as object );

//create the new record in the server side Parse Class (table) called "MyGameScores" (or your own class name)
myNewScore.Create();
				
//Wait until it's finished the save in Parse.
while(!myNewScore.isDone) yield return null;						

Debug.Log("myNewScore is done; check it in the MyGameScores table in the relevant Parse.com account.");		 
Advertisements

Unity3D for iOS: ParseInstance.GET(string) throws “InvalidCastException: Cannot cast from source type to destination type” for Date objects from Parse

The Error

//UniParse boiler plate:
var pObj = new ParseClass("/classes/Tournaments");			
string query = "...your query here..."; 	
ParseInstanceCollection MyParseList = pObj.List( query );			
while(!MyParseList.isDone) yield return null;					
ParseInstance[] items = MyParseList.items; 		

//get custom Date from a Parse.com Class (database table)
string MyDateFromParse = items[0].Get<string>("MyCustomDate"); 

The last line above throws this error because MyCustomDate is of type “date” in the Parse class:

/*
InvalidCastException: Cannot cast from source type to destination type.
ParseInstance.Get[String] (System.String key) (at Assets/Plugins/ParseClass.cs:238)
MyManager+<getMyCustomDateFromParse>c__Iterator15.MoveNext () (at Assets/Scripts/MyManager.cs:9)				
*/

The Solution

Read up on Data Types in Parse.com’s documentation.

Hashtable d = (Hashtable)items[0].Get<object>("MyCustomDate");
string MyDateFromParse = d["iso"];

Unity3D for iOS and Android: UniWeb and UniParse plugins for HTTP

If you’ve ever had to work with a REST API from within a Unity project or use web services and back end data in general, you’re probably seen the limitation of Unity’s built in WWW class. Lucky for us, the developers at Different Methods released an awesome plugin that provides full HTTP functionality for Unity3D. It’s called UniWeb. UniParse is a free plugin, still in beta, that works with UniWeb and a backend service called Parse.

I owe a special thank you to Simon Wittber of Different Methods for super speedy and excellent customer service help with resolving a Unity Asset Store download issue.

Here’re a few deeper UniParse examples:

My buddy Steve, went through the following basic steps to get UniWeb up and running on an iPhone 4S and and several Android models. The Android phones included:

  • Motorola DroidX (Android 2.3.4)
  • Samsung Galaxy (Android 2.1)
  • Samsung Galaxy Nexus (Android 4.0.4, Ice Cream Sandwich)

Testing UniWeb

What is UniWeb? It’s an “HTTP glue” plugin for Unity3D. It allows for much more functionality than Unity’s built in WWW class. For example, Parse’s REST API sometimes requires the use of the PUT method instead of GET or POST. WWW only works with GET & POST. UniWeb handles PUT just fine. While I haven’t tried all of UniWeb’s functionality, it has classes to handle stuff like JSON parsing & Socket connections.

Here’s a basic test we did over HTTP:

  1. Installed the UniWeb .unitypacakge
  2. Opened up the basic UniWebExample scene that comes with UniWeb
  3. At first, we got a minor error telling us the socket connection failed because the socket server doesn’t exist. This was easily fixed – we removed the Socket script from the test GameObject.
  4. The UniWebExample image loading script worked perfectly
  5. Next, we ran into a cross compilation error when we tried to put the test app on an iPhone. Steve fixed this by switching the Api Compatibility Level from “.NET 2.0 Subset” to .”NET 2.0″ in Build Settings > Player Settings (under iOS platform settings, Other Settings > Optimization)
  6. Shortly after Steve sent an APK of this test app to several folks with Android devices and the app worked well for them. The images loaded & cycled through once, just like on the iPhone version.

Testing UniParse with UniWeb

In layman’s terms Parse is a back end service for mobile developers. It frees mobile developers up from having to code their own databases & middleware language logic to manage data between those databases & mobile applications. UniParse is Different Methods’ plugin for talking to Parse from inside a game or mobile app built with Unity3D.

The UniParse plugin uses an Update() method that does an HTTP ‘PUT’. There’s also Delete, which does HTTP ‘Delete’.

Testing UniWeb over HTTPS

coming soon…