Category Archives: Development

Missing RectTransform after creating a UI component in script

Sometimes you may create a new UI element in script and want to set it’s position etc.

I wrote a quick extension method to allow me to stretch a newly created component to that of it’s parent:

public static class Extensions
{
	public static void StretchRectTransform(this Transform value)
	{
	    RectTransform rt = value as RectTransform;
	    if (rt == null) return;
	    rt.anchorMin = Vector2.zero;
	    rt.anchorMax = Vector2.one;
	    rt.sizeDelta = Vector2.zero;
	    rt.localScale = Vector3.one;
	}
}

Which I used as follows:

   
public static void Add(GameObject sel)
{
    GameObject go = new GameObject(sel.name+"_HL");
    go.transform.SetParent(sel.transform);
    go.transform.StretchRectTransform();
    image = go.AddComponent<Image>();
}

However I quickly found that the cast of a Transform to a RectTransform did not work in this scenario, as it turns out the RectTransform does not yet exist. I’m not sure when it begins to exist however a quick look at the docs and I noticed it’s a Component so modified my function to add it if it did not exist:

public static void StretchRectTransform(this Transform value)
{
	RectTransform rt = value as RectTransform;
	if (rt == null)
	{
	    rt = value.gameObject.AddComponent<RectTransform>(); // Add a RectTransform if it does not exist
	}
	rt.anchorMin = Vector2.zero;
	rt.anchorMax = Vector2.one;
	rt.sizeDelta = Vector2.zero;
	rt.localScale = Vector3.one;
}

This now works as expected. I assume adding the RectTransform replaces the original Transform, I’ve not found any docs on this so if anyone knows better please share!

Custom popups matching EditorGUILayout.Popup style

So I’m just tidying up one of my editor interfaces and I wanted the button used to pop up a generic menu to match the style used by Unity for it’s drop down menus in the inspector. i.e with an up and down arrow to the right.

I’m not using EditorGUILayout.Popup because I don’t want to add a delay whenever the inspector is shown which would occur if I created the array of options in OnEnable or OnInspectorGUI. Instead I wish to populate the menu when/if the user clicks the drop down button, at which point in my example I scan the project’s path for scene files.

To use call EdDelayedPopup.Popup and pass it the string or GUIContent you wish to display in the button and a function to call when the button is pressed. When the function is called you can query EdDelayedPopup.ButtonRect to get the rectangle of the button and GenericMenu.DropDown to open a menu there or even use your own custom window.
Continue reading Custom popups matching EditorGUILayout.Popup style

Working with multiple versions of Unity3D installed

As the number of projects you work on increases or you start to use betas such as the current Unity 4.6 betas you may find it useful to have multiple versions of unity installed on your machine at once. Fortunately this is very easy to do. You can even run multiple versions at once.

Installation

Mac: On the Mac Unity always installs into the Applications/Unity folder. Once installed you can just rename the Unity directory to reflect the version installed. Or as I do when I install a new version I rename my existing Unity directory to reflect it’s version.

Continue reading Working with multiple versions of Unity3D installed

Fixing a Macbook that’s got slower over time.

The following article details how, under load, my MacBook (Pro Retina Mid 2012 2.6GHz Intel i7 with 16Gb) was less than 1/4 of the speed it used to be and how I fixed it.

Over the last few months I started to notice that things were becoming less responsive in my dev environment, that’s an understatement, it was beginning to crawl. Something needed to be done or I needed a new machine!

Continue reading Fixing a Macbook that’s got slower over time.

High Resolution Cropped Facebook Profile Images

When writing our original Facebook integration we searched high and low to try to work out how to get the square profile image at a resolution higher than 50×50 pixels to no avail. The only options we could find were to get the picture, from which your profile picture is taken, at a higher resolution. The problem with this is that it’s not always square and often the user may have setup their profile image to be a cropped area of it.

Well today I was fed up with looking at low resolution images so searched again and managed to find a solution. Using the graph api you can request facebookid/profile?fields=pic_crop

eg http://graph.facebook.com/tactilefusion/profile?fields=pic_crop returns:

{
   "data": [
      {
         "pic_crop": {
            "uri": "http://profile.ak.fbcdn.net/hprofile-ak-prn1/t1/p320x320/10992_567316339955054_1439725697_n.jpg",
            "width": 320,
            "height": 320,
            "left": 0,
            "top": 0,
            "right": 1,
            "bottom": 1
         },
         "id": "447312341955455"
      }
   ]
}

Note that the left, top, right, bottom fields will always be the above they are there for backwards compatibility as it now always returns a cropped image.

So at last we have full res profile images in our new games in production:

Leaderboard