CSS Flexbox not wrapping on iOS

April 24th, 2016

I had a problem where flexbox didn’t wrap on iOS but worked fine on all other browsers.
Thankfully it turned out to be a simple fix, on my flexbox child item I had to change

flex: 1

to

flex: 1 0 $minWidth; // Just 'flex: 1' will break wrapping on iOS.

where $minWidth is the minimum desired width of the flexbox child item.

Unity: Get UI element under mouse

March 30th, 2016

To check if the mouse is over any UI element you can use

EventSystem.current.IsPointerOverGameObject()

In case that function isn’t acting to your liking and you need to debug, or if you just want to know what object is under the mouse, you can use

PointerEventData pointerData = new PointerEventData(EventSystem.current) {
	position = Input.mousePosition
};

List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);

results.ForEach((result) => {
	Debug.Log(result);
});

Some Cygwin tips

October 8th, 2015

I’ve included here some first things I do after every fresh Cygwin installation.

Install Cygwin with Mintty, Zch, git, openssh, wget, curl.

Set Cygwin to use windows user home directory.
Open CygwinDir/etc/nsswitch.conf and add this line to it:

db_home: windows

Make Cygwin windows drive shortcuts shorter.
Type this into the Cygwin command line:

ln -s /cygdrive/c /c

Browse to C by typing “cd /c”. Do this for every drive you want.

Use Mintty as terminal and Zch as shell.
Edit your cygwin shortcut to look like this:

C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico /bin/zsh --login

Install Oh My Zsh in Cygwin
Just follow the normal instructions at this point:

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Remove Boo and UnityScript references from Unity projects.

October 6th, 2015

To stop Resharper in Visual Studio suggesting Boo.Lang when declaring List<>’s and other types common to both libraries, simply put this script in your editor folder.

Original source: https://gist.github.com/jbevain/a982cc580fb796c93e4e

using UnityEditor;
using SyntaxTree.VisualStudio.Unity.Bridge;
 
[InitializeOnLoad]
public class ReferenceRemovalProjectHook
{
	static ReferenceRemovalProjectHook()
	{
		const string references = "\r\n    <Reference Include=\"Boo.Lang\" />\r\n    <Reference Include=\"UnityScript.Lang\" />";

		ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
			content.Replace(references, "");
	}
}

Unity: Is mouse (or any coordinates) within UI element’s rect

March 30th, 2015

Here’s a quick way to check if the mouse or any other coordinates are within any UI object’s boundaries.

This method doesn’t use raycast so it ignores all overlapping objects, it works just like ActionScript’s object.HitTest(coords) function.

public bool AreCoordsWithinUiObject(Vector2 coords, GameObject gameObj)
{
	Vector2 localPos = gameObj.transform.InverseTransformPoint(coords);
	return ((RectTransform) gameObj.transform).rect.Contains(localPos);
}

// Example usage
bool isMouseOverIcon = AreCoordsWithinGameObject(Input.mousePosition, _myUiIcon);

Unity: Getting screen size in units (2D)

January 14th, 2015

When doing 2D games in Unity it is often handy to know where the borders of the screen are in units so one can for example spawn units on the edge of the screen or detect when something leaves it.

Fortunately it’s easy to figure out when using orthographic camera.

Camera.main.orthographicSize is always half of the total screen height, with the width changing with the aspect ratio. You can get the top or bottom coordinates by adding/subtracting it to/from the camera’s y position.

For the width you can then multiply the aspect ratio with the orthographic size,
camera.orthographicSize * Screen.width/Screen.height , to get half of the total screen width.

You can then get the x position of the screen’s left border like this:

float leftSideOfScreen = Camera.main.transform.position.x - Camera.main.orthographicSize * Screen.width / Screen.height;

Unity: Networking errors when changing scenes.

November 18th, 2014

I had a really simple project set up that had two scenes, “Init” which I use to connect to server, and “Main”, where all my synced objects using networkViews are.
When the player had connected to the server I’d change to “Main” where I wanted all the objects already on the server to automatically show up.
However every time I ran the project I got these three errors:

ms_IDToPointer->find (obj->GetInstanceID ()) == ms_IDToPointer->end ()
View ID AllocatedID: 1 not found during lookup. Strange behaviour may occur
Received state update for view id' AllocatedID: 1' but the NetworkView doesn't exist

What was happening was that the server was sending us the objects before we had a chance to load “Main”, which was weird because I was using “Network.isMessageQueueRunning = false;” before connecting to the server and turning it back on when “Main” was loaded.

Fortunately this has a simple fix, apparently you have to set isMessageQueueRunning AFTER calling Network.Connect(..). I moved it to “void OnConnectedToServer()” and everything worked fine.

Init.unity

void Start()
{
	// Don't set isMessageQueueRunning here
	Network.Connect(hostData);
}

void OnConnectedToServer()
{
	Network.isMessageQueueRunning = false;
	Application.LoadLevel("Main");
}

Main.unity

void Start()
{
	Network.isMessageQueueRunning = true;
}

Managing your playerglobals and SDKs

March 28th, 2013

If you have multiple versions of the Flex/Air SDK on your computer it can sometimes become a hassle to make sure that every SDK has all the playerglobal.swc files for all the versions that you need to compile for.

What I recommend is creating a /flex directory in your preferred location (mine is on the root of C:) where you have all your SDKs, and put a folder there called /playerglobals where you have all your versions of playerglobal that you will need.
Then delete the FlexSDK_x.x.x/frameworks/libs/player folder from every SDK and replace it with a Directory Junction (hard) link, which you can do with the following command (Windows):

mklink /J C:\flex\FlexSDK_x.x.x\frameworks\libs\player c:\flex\playerglobals

Now you have only one place to worry about your playerglobals.

Switch-case for object’s class type

August 31st, 2012

Quick tip; instead of using a bunch of “if (x is y) else if (x is z) else” conditionals, you can simply get the class of any object using the “constructor” property. It’s not exposed trough strict typing though, so you have to typecast your instance to Object to access it.

This allows you to have a switch-case for an object’s class:

function addShape(shape:Shape):void {
	switch (Object(shape).constructor) {
		case Circle:
			trace("It's a circle");
			break;
		case Square:
			trace("It's a square");
			break;
		default:
			trace("Unknown shape");
	}
}

Mouse scroll wheel in flash not working in Chrome

August 14th, 2012

Chrome’s latest update to version 21 introduced a new type of flash player using the PPAPI (pepper) plugin architecture.
I noticed that this version of flash started breaking mouse scroll wheel support for some flash content.

Which flash files break and which don’t depend on one thing; if you are using JavaScript to prevent the default behavior of the mouse scroll wheel or not.

The reason for preventing default mouse scroll wheel behavior is for when you don’t want the page to scroll when you scroll over the flash content.
To prevent that, developers write JavaScript code (or use libraries like SwfWheel.js) that add an event listener for the “mousewheel” or “DOMMouseScroll” event, and in the handler call “evt.preventDefault()” and/or “evt.returnValue = false” to cancel the scroll.

Therein lies the problem, the old NPAPI Flash Player would receive the mouse scroll event even if we prevented the default behavior. Maybe even incorrectly so, since the event was cancelled.
The new PPAPI Flash Player will not receive the event if the default is prevented.

If one were to remove the preventDefault() function, the Flash file would start working correctly again, but the whole page would again start scrolling, which is out of the question.

You will have to not only prevent the default scroll behavior, but also call an external interface function in your flash file to let it know when the user scrolls the mouse wheel.
In my case I was using SwfWheel and only needed a small modification to the library file: in the SWFWheel.getState() function add this code before the “if (br.mac)” line:

// The new pepper plugin requires chrome to always be hacked.
if (br.chrome)
{
	return STATE_HACKED;
}