Blank Game View in Unity3D Editor

Since a recent Unity update (pretty sure it’s the one that added the ability to scale the gameview) I’ve been seeing an intermittent issue where the game view will just go blank.  This happens in both Edit & Play mode, but only on my Mac.  Initially, I had to quit and reload Unity to resolve it, but discovered, reselecting my window layout from the ‘Layout’ drop down restored the game view.  So it became annoying and not as aggravating :D.

I’ve been following a thread on the Unity forums where this is being discussed:

http://forum.unity3d.com/threads/blank-game-view.421817/#post-2772116

I tried the suggested start-up flag and the problem went away, though I noticed a performance drop in the editor, but nothing serious.  I used the following command line from a terminal window to start Unity with the flag set.  (Note, I have my copy of Unity in a folder that includes the version name, you might need to change that):

/Applications/Unity5.4.0/Unity.app/Contents/MacOS/Unity -force-gfx-direct

I hope they find & fix this one soon.

NGUI EventDelegate

I have a button whose function changes depending on which panel is currently being displayed.  When switching panels, this button stays on screen (think a NavBar).  I ran into this unexpected behavior today, that slowed me down.

If you change the contents of an EventDelegate list during a method called from that  EventDelegate list then its probable that your new method will get called right way when control is passed back to the Execute method of EventDelegate.  This looked like the OnClick method of a UIButton was not waiting for a release, so I went down that rabbit hole for a bit.

I ended up adding a short delay to the code that switches the current OnClick action to the new OnClick action, which solved the problem.

	button.onClick.Clear();
	//delay this by a frame, so it happens outside the EventDelegate processing
	callAfterDelay(0.01f,()=>{
		button.onClick.Add(action);
	});

OSX Find command and patterns

I’ve been working on learning to create Joomla modules and plugins, and needed to do some bulk renaming on files, folders and the contents of those files (some of which are .ini, some are .xml and lots are .php).

To rename the files, I ended up using Automator on the mac – this is a nice visualization tool to do bulk rename of folders and files.

For the file content, I used the following command:

find . -type f -name '*.xml' -exec sed -i '' s/OLDNAME/NEWNAME/ {} +

This finds all the .xml files in your current path, passes them to sed, which then replaces the text OLDNAME with NEWNAME.  Because I also had some case variations, I used three other commands:

find . -type f -name '*.xml' -exec sed -i '' s/OLDName/NEWName/ {} +
find . -type f -name '*.xml' -exec sed -i '' s/oldname/newname/ {} +
find . -type f -name '*.xml' -exec sed -i '' s/Old Name/New Name/ {} +

Combine this with all 3 file types and it’s a lot of typing.  A read of the manual file for find says that -name uses the standard ‘pattern’ system, and then after some googling, I discovered you can use [ ] to have it match one character from the list, so I split up the file types and used the following:

find . -type f -name '*.[xpi][mhn][lpi]' -exec sed -i '' s/Old Name/New Name/ {} +

That worked as expected, and I was able to hunt for all 3 extensions with one find command.

Now I’m sure someone out there can probably point out how to use regex to do it correctly, but hey, my method worked, and that’s all I cared about…

I just discovered, I’ve a fourth file type that needed to be updated… ‘.sql’.  So I interleaved that in my line and voila!

find . -type f -name '*.[xpis][mhnq][lpil]' -exec sed -i '' s/Old Name/New Name/ {} +

(I probably don’t need that last ‘l’ but for completeness I left it in…)

WordPress multi-site

I’ve been working on setting up a new set of WP sites on my servers and ran into a funky problem I wasn’t able to solve (initially) or find any other documented solutions out there.

I’m using a plugin called ‘WordPress MU Domain Mapping‘ and I’d followed the install instructions.  However, when I would visit the ‘Domain Mapping Configuration’ page in my Setting tab I’d always see a message at the top of the page ‘Domain mapping database table created.’.  This message appeared every time I went to that page.

Now some googling did find other folks who were having that message always appear, and it seemed that it was because the table wasn’t actually being created.  Things such as permissions and access were pointed out and I’d checked all that and every thing seemed in order.  I could even create a new table using phpMyAdmin with the same user WP used, so I knew the user was setup correctly.

Checking out the tables with phpMyAdmin, I could see that the ‘domain_mapping_logins’ table had been created, but the ‘domain_mapping’ table didn’t exist, and this was obviously needed and the root of the error.  I turned on WP debugging, hoping I’d see an error message about the table not being created, but no such luck.

In the end, I solved it by exporting a working table from another WP install I had, and then imported it via phpMyAdmin. This created the table for me, and all was well with the world!

I’d like to know why the plugin was unable to create the required table, but that’s a project for another day I think.  The table should have been created by the following code:

if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtable}'") != $wpdb->dmtable ) {
			$wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtable}` (
				`id` bigint(20) NOT NULL auto_increment,
				`blog_id` bigint(20) NOT NULL,
				`domain` varchar(255) NOT NULL,
				`active` tinyint(4) default '1',
				PRIMARY KEY  (`id`),
				KEY `blog_id` (`blog_id`,`domain`,`active`)
			);" );
			$created = 1;
		}

Which is similar, but with obvious differences from the code that creates the ‘domain_mapping_logins’ table:

if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtablelogins}'") != $wpdb->dmtablelogins ) {
			$wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtablelogins}` (
				`id` varchar(32) NOT NULL,
				`user_id` bigint(20) NOT NULL,
				`blog_id` bigint(20) NOT NULL,
				`t` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
				PRIMARY KEY  (`id`)
			);" );
			$created = 1;
		}

Update: Figured out what was going on:  The version of mysql running on my webserver is 32bit and I dug into the actual error.  It turns out that the ‘domain’ field exceeded the maximum length of a key (1,000 bytes).  This is because UTF8 is used as the default encoding, which allows basically 9 bytes per character, so the domain can be 2,295 bytes long.  To remedy this, I added the following to the SQL statement:

ENGINE=InnoDB DEFAULT CHARSET=latin1;

Which keeps the field length under the 1,000 byte limit and allows the field to be created.

Javascript and new way of thinking!

It’s been quite some time since I did any Javascript programming, and it’s hard to start thinking in an unstructured way again.  One of JS’s peculiar nuances is the ability to declare a property on an object by just assigning it! After working with C++/Obj C/C# and having to declare everything up front this takes a new way of thinking.

For instance, with the Cocos2D-X-JS the pattern of their script declaration didn’t make it obvious on where or how to declare variables that will persist between function calls.  Being used to pre-declaring everything, I stuck a few ‘var’ statements at the top of the file, before the ‘extends’ stuff starts.  This works, but looks a bit messy.

var self,canvas;

cc.Class({
extends: cc.Component,
...

Of course, with JS, there’s no need to declare variables, just assign them as a property and they’re created for you!  Of course, you’ll not get the benefit of type checking and even checking if the var you’re using is spelt the same as the one you declared, but you can’t have everything!