Category Archives: Unity3d

Unity Tutorials

I’ve started working on a series of Unity tutorials. Initially I’m going to focus on some of the low level stuff required to build iOS and Android products with Unity, specifically creating plugins that will allow Unity to access features that are specific to that platform.

You can see the tutorials at my youtube channel along with some other review videos I’ve posted to Amazon.

I’ll be posting the code from the tutorials on my github page – – check back for updates.

Unity 5.6 and Android overlays – Updated

Unity just released 5.6.2f1 which rolls back the change they made to their Android player.  While the method described below works, it’s no longer needed.  There was also some performance issues using the PopupWindow, so it’s better just to go back to using a view added to the Unity view.

I updated the Unity version I’ve been using to 5.6 when it came out of beta, but when I built my android app, I noticed a major problem – the ad banners no longer appeared!  So glad I made sure to test my app before updating in to GPlay 😀

Turns out 5.6 uses a feature in Android to make their view sit on top of all other views for that activity, and when my plugin would create a view to hold both my AdMob view and Amazon adView, it wouldn’t display them on top of the Unity view as before.

I tried a few different methods before seeing that the folks who maintain the AdMob plugin for Android on Unity had  updated their method to use a PopupWindow.  I did some research on this method, and got it to work for both AdMob and Amazon ads.

The one thing that had me stuck for a bit was that you cannot show a PopupWindow right after creating it in the application onCreate method, you need to wait for a bit.  This was also true when I created the PopupWindow during my plugin initialization.

I used the post() method on the rootView to get the delay I needed.

    if (mPopupWindow!=null)
        activity.getWindow().getDecorView().getRootView().post(new Runnable() {
        public void run() {
            if (isDebug)
                Log.i(LOGTAG,"Showing POPUP window...");
                            Gravity.TOP, 0, 0);

At some point, I’ll update my plugin to allow the PopupWindow to be either at the top or bottom of the display, right now it’s hard-coded to appear at the top.

Custom Inspector: Mixing custom drawer and default property drawers

I’ve been playing around with ScriptableObjects and using them to define parameters for my Dialog display gameobject.  In previous instances, I’d just use constant strings and pass them to the function that creates and displays the dialog.  After reading about ScriptableObjects, I decided to give them a shot to build strings for the dialogs.

Everything was great, except for the inspector displaying long strings.  They just get truncated of the right side of the display.  The behavior exhibited by the NGUI UILabel inspector is much better, as any text to long to be displayed in the inspector is wrapped and the field is extended vertically.

I found the NGUI code in UILabelInspector that handles the displaying of strings across multiple lines, and took a copy to use in my own inspector.  Then I realized, with a custom inspector I have to display all the properties myself, and I didn’t want to (a) write a custom display for each property type, and (b) remember to extend the inspector anytime I modified my ScriptableObject class.

The second problem was solved by iterating over the SerializedProperties contained in the SerializedObject, while the first problem was solved by calling EditorGui.PropertyField on SerializedProperties I didn’t want to display myself.

I then added an exception for the ‘Script’ property, to make it non-editable.

This resulted in the code below:

public override void OnInspectorGUI()
	SerializedProperty sp = serializedObject.GetIterator();
	if (!sp.NextVisible(true))
		sp = null;
	while (sp!=null)
		if (sp.type == "string")
			Rect position = EditorGUILayout.GetControlRect(GUILayout.Height(EditorGUI.GetPropertyHeight(sp)));
			if ( == "m_Script")
				EditorGUI.PropertyField(position, sp, new GUIContent(sp.displayName), true);
				EditorGUI.PropertyField(position, sp, new GUIContent(sp.displayName), true);
		if (!sp.NextVisible(true))
			sp = null;

The call to DrawMultiLineString uses code directly from NGUI’s UILabelInspector, and is not available unless you have NGUI for Unity.

Unity3D and OnValidate

So, I came across MonoBehaviour method earlier today that I’d previously missed:

void OnValidate()

This is called when a script is loaded, or (editor only) when a parameter is changed via the inspector.  I’m using this to allow me to set a property that has a getter/setter via the inspector by using an extra variable.   I set the extra copy in the inspector, and then use code in OnValidate to copy it to the setter method.

I’m also going to use it instead of the Update method for my editor scripts – Update is called every time something changes in the editor, OnValidate, only when something on that script changes – much more effective.

Unity3D IAPs

I’ve been upgrading my Unity3D system to use the new Unity3D IAPS.  I’d been using a mixture of my own stuff on iOS and Unibill for Android, and wanted to clean up my code and simplify the cross-platform process.

Upgrading my code to reference UnityEngine.Purchasing system wasn’t too hard, but there was one thing I came across that cost me a lot of wasted time, the selected Store on Android.

For an Android build, you have a selection of store fronts you can support – Google Play, Amazon & Samsung.  (They just added a ‘Select Store at Runtime’ option too).

You make this selection using the Editor, but there is nothing to indicate what this selection is.  I had selected Amazon while messing around in the project, and promptly forgot I’d done that.  So when I finally called the code to initialize the purchase tokens, they would never authorize and always return ‘product not found’ error.  Initially I blamed this on the fact it can take Google Play 24 hours to activate a new purchase token, so I took a break…

The next day, the same problem, but I thought maybe the 24 hours wasn’t technically up yet, or maybe Google was taking longer than normal.  I pushed on with some other tasks, and checked the products by running the App from time to time on an Android device and watching ADB for the results – still nothing.

That’s when I remembered the store selector! I selected Google Play, rebuilt and presto, it worked! However, it would have been nice if either (or both)

(a) The Editor menu had a check mark beside the selected store

(b) The system spat out an ADB message with the name of the store it was checking

It also turns out you can select the store via a script, which I decided was the better option for me.



public AndroidStore androidStore;

is a property on a MonoBehaviour which creates a nice drop down in the inspector.