Category Archives: Programming

UIButton and isEnabled color change

As part of my hidden UIWidget stuff, the widget has a few buttons on it that I set the isEnabled flag to false when appropriate.  This had the effect of the button changing over time to a dark gray color when it appears on screen.

I didn’t want the color to transition, I wanted it to start gray when it appeared.  So I added the following lines:

float oldDur = button.duration;
button.duration = 0;
button.isEnabled = false;
button.duration = oldDur;

That did the trick – button appeared gray and didn’t transition.

NGUI and inactive widgets in Unity3D

I have a UITexture that I resize before making it visible in the game view.  The problem I ran into is that when you resize a UIWidget and that widget is currently inactive, then NGUI will not force an update to any collider you have attached to it.

I got around this by adding a call to ResizeCollider() after I enabled the UIWidget.  I’ll keep watching to see if there are other properties not being updated while the UIWidget is inactive.

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…)