Category Archives: Unity3d

Non-breaking space in Unity3D/NGUI

I was messing with some text line wrapping an an app today, and needed to stop NGUI inserting a line break between a number and a label in a long string.  Using the escape sequence ‘\u00A0’ did the trick.

return category + "\u00A0#" + indexInCategory;

Now the last word in the string category and the number preceded with a # stay on the same line!

Note: I read that pressing ALT-Space on the Mac will insert this character.  It may do that, but MonoDevelop/NGUI don’t seem to care.  The escape literal worked, the ALT-Space just acted like a regular space.

Note: To insert a non-breaking space in a XML document without using Document Entities, use the following:  

Updating the Unity Splashscreen

Looks like they didnt expose the splashscreen property, which is a pain… I have a script that allows me to switch between a Paid version and Free version of an App inside Unity.  Makes developing the App a lot simpler… just flip a toggle and presto, new icon, no ads and no ‘remove ad’ button!

I wanted to swap the splashscreen between the paid and free versions with this toggle, but the fine folks at Unity didn’t think of that, so after some searching, I ended up copying the correct screen over a generic splashscreen, but I had to use File.Copy as AssetDatabase.CopyAsset would reset the splashscreen in the build settings, which defeated the purpose!  I call to Refresh() when the copy was done updated the build setting and asset database.

	string srcPath = AssetDatabase.GetAssetPath(splashScreen);
	Debug.Log("srcPath: " + srcPath);
	string dstPath = Path.GetDirectoryName(srcPath);
	dstPath+="/splashScreen" + Path.GetExtension(srcPath);
	Debug.Log("Duplicating " + srcPath + " into " + dstPath);
	File.Copy(srcPath,dstPath,true);
	//AssetDatabase.CopyAsset(srcPath,dstPath); // doesnt work correctly
	AssetDatabase.Refresh();

Hope that helps someone else till they fix it.

Unity3D Application.persistentDataPath is null on Android

I’ve no idea why this is happening all of a sudden, but the static property Application.persistentDataPath is returning a zero length string and not the path to the storage folder on my Android devices.  From googling, I can see I’m not the only one with this problem.  It’s happening whether I build directly from Unity, or download it from the beta tab on Google Play.

I added the following method to my Android plugin JAR so at least I could move on and not get stuck:

    public static String PBgetFilePath(final Activity activity)
    {
        File path = activity.getApplicationContext().getFilesDir();
        Log.i(LOGTAG,"Internal FilesDir: " + path.getPath());
        return path.getPath();
    }

This returns the correct string and when I use it, I can read and write files as expected.  If persistentDataPath returns a null or a zero length string, then I call my java code and use the result.

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.