Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
October 20, 2014
arrowPress Releases
October 20, 2014
PR Newswire
View All
View All     Submit Event





If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 
Interesting things in Unity 4.5 you probably didn't know about
by Valentin Simonov on 06/03/14 05:10:00 pm   Featured Blogs

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

Well, as you all know Unity team has recently released Unity version 4.5 which brought us (according to Unity's web site):

  • Over 450 bug fixes,
  • OpenGL ES 3.0,
  • Shader workflow boost,
  • Smooth and natural 2D physics and
  • Sparse Textures.

While 450 bugs fixed sounds amazing, shaders compilation was indeed terribly slow, I don't really care about OpenGL ES 3.0 (since I got an old iPhone 4s) and Sparse Textures is a good feature to explore on modern desktop GPUs, what I want to tell you about are those features and bug fixes buried in the massive Release Notes which you have probably missed but they are indeed very important. 

In no particular order.

Hierarchy Window sorting

New Hierarchy Window sorting - sorting of elements is now based on transform order instead of name.

This change alone breaks the naming convention of millions of developers. I myself used to name the empty object with controllers $ to make sure that it's always on the top of Hierarchy Panel. Now you can just drag GameObjects up and down manually.

SelectionBase attribute

Editor: Created SelectionBase attribute. When applied to a script, GameObjects with that script on will work the same as prefab roots for Scene View picking.

If you have an hierarchy of objects with geometry attached usually when you click one of them it gets selected.

If you remember Prefabs behave differently. If you click an object in prefab's hierarchy the root of this prefab is be selected instead. And now you can manually force this behavior using SelectionBase attribute.

[SelectionBase]
public class TheBase : MonoBehaviour {}


This may be useful when you think of an hierarchy of objects as single object and don't want to control its child objects. For example you have an object and a plane inside and it's important to move this root object instead of its child plane. This feature might help you always select the right object.

DecoratorDrawers

Editor: PropertyDrawers are now supplemented with DecoratorDrawers, which can be used for decorative elements in the Inspector such as spacing, headers, or notes.  
Editor: Improved inspector customisation with [Tooltip] and [ContextMenuItem] script attributes.

PropertyDrawers are used to make your fields more pretty in the Editor. They are extremely easy to use. For example using just one annotation you can convert a float field into a smart range property.

[Range(0, 100)]
public float MyValue;

While PropertyDrawers modify the appearence of fields in the Editor, new DecoratorDrawers augment them with visual elements. Check out this code:

[Header("Hi there!")]
public string TheHeader = "Header!";
[Tooltip("This is THE VALUE!")]
[ContextMenuItem("Reset", "resetTheValue")]
public float TheValue = 42.0f;
[Space(50)]
public string TheString = "THE STRING";

private void resetTheValue()
{
    TheValue = 42;
}


And the animation where you can see what these annotations do.

Just a little trick to make your Editor interfaces better.

Scripts in DLLs

Scripting: Improved resolution of types that inherit from types in different user assemblies.

This is probably one of my favorites.

You see, prior to 4.5 if you had MyBaseClass : MonoBehaviour in MyBaseClass.dll and MyDerivedClass : MyBaseClass in MyDerivedClass.dll you couldn't use MyDerivedClass in your projects because Unity was unable to follow this MASSIVE inheritance chain and figure out that MyDerivedClass was a MonoBehaviour.

Thanks God this is now fixed.

Default parameters

Scripting: Fixed namespace detection for MonoBehaviors containing methods with default parameters.

This bug was one of those bugs stopping people from using specific language features. Now we can breath freely.

Saving to JPEG

Graphics: Added Texture2D.EncodeToJPG.

Many developers have been asking for a function to save to JPEG. Yes we had Texture2D.EncodeToPNG for a long time, but you know...

Anyway, the new method is also terribly slow. Encoding a 2048x2048 texture:

  • Encoded to PNG in 0.4529677 seconds,
  • Encoded to JPG in 0.1349338 seconds.

Now we know if we have Pro license

Scripting: Added UNITY_PRO_LICENSE define.

This will be useful for fellow plugin developers. Now we have an easy way to be sure that code we are writing targets right Editor version.

Serialization

Scripting: Add UnityEngine.ISerializationCallbackReceiver interface, to get a callback right before serialisation and right after deserialization.  
Scripting: Structs with System.Serializable attribute can now be serialized. Also, fields of AnimationCurve[] and double[] now get serialised.

People have been asking for years for the ability to serialize structs. Now this code works and shows a list of structs:

public List list = new List();

[Serializable]
public struct MyStruct
{
    public int Value;
}


Being able to serialize arrays of AnimationCurve sometimes may be handy too.

But what really interesting is ISerializationCallbackReceiver interface. The docs say:

Interface to receive callbacks upon serialization and deserialization.

It seems that instead of wasting man-years on figuring out how to serialize Dictionaries they gave us an ability to manually specify what and how we want to be serialized. Example in the docs shows just that.

GameObject.GetComponentsInParent()

Scripting: Added GetComponentsInParent function.

It's good to know that now we have this. I haven't tested it yet but it must be a lot faster than enumerating manually through the parent chain.

Debugging DirectX 11 shaders

Documentation: Added docs on how to debug DirectX11 shaders with Visual Studio, look for 'SL-Debugging DirectX 11 Shaders with Visual Studio'.

The method described is not new but now we got a decent tutorial on how to debug dx11 shaders in Unity.

Windows Phone apps

I understand that nobody gives a fuck about WP apps but I had to develop a couple simple ones for Nokia Lumia and I'm glad that Unity is evolving and I hope that it will be able to make this platform better.

There are a lot of added features and fixed bugs for Windows Phone target and it's good to know that I helped fixing some of them:

Start, Update and similar methods in scripts derived from generic base class are now properly invoked.  
WP: Resolution will now be correctly detected on phones than have larger screen than 768x1280.  
WP: Screen.dpi now returns physical device DPI, rather than 0.

o.O

Active scenes count properly displayed when more than 100 scenes are added to Build Settings window.

Damn, someone has huge projects...

Nice lists

And the last one is not even in the Release Notes. It's a little Editor class hiding in UnityEditorInternal namespace. It's called ReorderableList and it makes drawing lists in custom inspectors such an easy task. If you ever tried to make a pretty inspector for a List<> or Array field in Unity you know what a massive pain it is.

Here's a demo of ReorderableList class:

This is the code from the custom inspector used:

private ReorderableList list;

list = new ReorderableList(property.serializedObject, property.FindPropertyRelative("tagList"), true, true, true, true);
list.drawHeaderCallback += rect => GUI.Label(rect, label);
list.drawElementCallback += (rect, index, active, focused) =>
{
    rect.height = 16;
    rect.y += 2;
    EditorGUI.PropertyField(rect, 
        list.serializedProperty.GetArrayElementAtIndex(index), 
        GUIContent.none);
};
list.DoList(position);


This is the full example: TouchManagerEditor.cs

Interesting fact: there was already practically identical list implementation called ReorderableList which I've been using from time to time.

***

These are the features I found particularly interesting while browsing through Unity 4.5 Release Notes. It's always like a treasure hunt... you  never know what you will find.

If for some reason you think that some other features/fixes deserve more attention feel free to post them in comments.

---

This text is a repost from my blog at va.lent.in.
If you are a Unity developer check out my open source multitouch library for Unity — TouchScript.


Related Jobs

Cloud Imperium Games
Cloud Imperium Games — Austin, Texas, United States
[10.20.14]

DevOps Engineer
Harmonix Music Systems
Harmonix Music Systems — Cambridge, Massachusetts, United States
[10.20.14]

Client-Server Network Engineer
Harmonix Music Systems
Harmonix Music Systems — Cambridge, Massachusetts, United States
[10.20.14]

Software Engineer - Gameplay
Cloud Imperium Games
Cloud Imperium Games — Santa Monica, California, United States
[10.20.14]

Gameplay Programmer






Comments


Ian Morrison
profile image
Thanks for digging for this stuff! I also went through the 4.5 changelog but I still missed a ton of it.

Guillermo Aguilera
profile image
"Hierarchy Window sorting" this is soo sad.

Chris Masterton
profile image
yes yes yes to Default Parameters. thanks for the dig.

Bryson Whiteman
profile image
Great summary Valentin!

I was wondering though, if for some reason someone was still on 4.4 or 4.3, what would happen for some of the newer editor features that you mentioned. Like the ReorderableList or DecoratorDrawers, would they simply fail or is there a reasonable fallback. Just wondering, for instance, in case if you included these features in a plugin and a user had an earlier version.

Oliver Eberlei
profile image
Hey Bryson.

If you are developing for the Unity Asset Store, the current version you are using is automatically used as the minimal required version to download the asset. Developers with lower version would get a text "Version 4.5 required" instead of the regular buy/download button.

If you are distributing the plugin yourself, then the script will simply fail because it won't know the class names you are using since they're only introduced in 4.5. There is no fallback, just a bunch of errors in the console.

You can develop your plugin version specific by using the proper #define's in your code to encapsulate code that only runs in 4.5

Read this for more info: http://docs.unity3d.com/Manual/PlatformDependentCompilation.html

Alejandro Martinez
profile image
On the asset store there is a gem called FullInspector.
It brought these inspector/editor serialization goodness and a lot more:
- Dictionaries,
- List,
- Structs,
- Even Interfaces and Abstract classes are serializable to a concrete implementation.
- Fields annotations
- Easily make some buttons appear and do some actions.
- Delegates,
- ...and more.

Not free but it's still a bargain.

Valentin Simonov
profile image
This one?
https://www.assetstore.unity3d.com/en/#!/content/14913
Interesting.

Lance McKee
profile image
Yes, thank you for all the great information! It's a simple little thing, but I've found myself wanting a GetComponentInParents many times over the years.

Olivier Besson
profile image
is the 2D Gui system still ... hem...(how to stay polite?)... the most-badly-designed-in-the-world?

Valentin Simonov
profile image
IMGUI (aka OnGUI) is fine for editor tweaks.
The new GUI for games should come with 4.6.
It looks good so far.

Amir Ebrahimi
profile image
Thanks for the write up!

Stefan Sigl
profile image
Thanks! I read the changelog (fast) and i missed some cool stuff!

Revolter Nameless
profile image
Hi, you wrote the following:

"Anyway, the new method is also terribly slow. Encoding a 2048x2048 texture:

Encoded to PNG in 0.4529677 seconds,
Encoded to JPG in 0.1349338 seconds."


Even though the information shows that the new method is almost 4 times faster than encoding to PNG.


none
 
Comment: