Virtual Reality is here. Since 2016 we can play high quality experiences.
In my previous article Finding Your Next Unity3D Job I overview what companies want in prospective candidates and outline where you can search for available positions. And in Get a Job In Game Development, I discuss the specifics of a programming career in the games industry
The games industry spans myriad platforms. Historically, PC (Commodore, Amiga, Dos, Windows, Mac, Linux) through the 70’s and 80’s led the medium. Consoles (Atari, Nintendo, Sega, Sony, Microsoft) and handheld (especially Nintendo) brought the first game-specific hardware to consumers. The 2007 release of the iPhone brought the critical masses to the device that would launch more games per year than any other platform before it. People who ‘never play games’ started to play, and continue to play today. The primary leader of advancement each year has been ‘better graphics’. Visuals are important to the marketing, promotion, and experience of playing video games. While the history of that technology spans decades, the number of other major technology advances is relatively small.
Its a short list. Here are the most significant paradigms in video games:
Virtual reality (VR) is a computer technology that uses software-generated realistic images, sounds and other sensations to replicate an a real environment or an imaginary setting, and simulates a user’s physical presence in this environment to enable the user to interact with this space. The essential hardware is a VR Headset.
Visuals
Visuals are at the essence of VR.
If all humans had just one eye, we would see still see robust visual information. But compare that to 2 eyes. Our 2 eyes look in the same general direction at the same objects. However, the offset position of our eyes (inches apart) and the unique angle (looking in slightly towards the nose) give us much more information than one eye could give. Through stereopsis we sense depth, perspective, and motion at a more profound level.
In traditional software development, visual content is designed for one screen of output (e.g. computer monitor or TV). This is essentially a one-eyed perception of the world within our games. For decades, we have adapted to see those worlds as realistic.
The perceived psychological distance between our ‘self’ and our ‘game’ collapses. The subtleties of where we are sitting, how we are sitting, and the angle of the screen are insignificant. In one-screen gaming, we are not ourselves, we are the screen.
However, the essential difference in VR is the simultaneous output of 2 high-resolution, high frame-rate screens. For the first time in the virtual gaming world we are gaining the same benefits of stereopsis in the physical world.
In VR gaming, we are no longer our game screen, we are ourself.
Input
Traditional gaming input devices include keyboard, mouse, controller (joystick/gamepad), and more recently gesture and voice. VR can and will embrace those devices too.
Primary Inputs
Headset – the position, angle, and acceleration of your head are input.
Motion Controller – The position, angle, and acceleration of your hand(s) are input.
Gamepad – Traditional console game controllers are also popular.
While there have been attempts at VR in the past, 2016 marks the relevancy of VR to the mainstream. The cost, quality, and distribution are finally here. Some are already released and before the end of the year, all the known devices will be released.
Apple has yet (October 2016) to announce specific plans for VR/AR, but the major game development platforms are embracing VR. Some more quickly and completely than others. Here are a few leading options.
Unity3D VR – Capable, cross-platform, game development. An early adopter of VR
Unity3D is a powerful suite of tools (Project IDE, Code IDE, run-time) for game development. In Unity3D Game Architectures I present six different techniques for setting up your game. Depending on the size and maturity of your team, you are probably using some form the archtectures presented there. I recommend checking that article out first, then read below.
A newer architecture called Entitas was presented at Unity’s Unite Conferences (2015 and 2016). I saw the most recent presentation and recently made time for a deeper dive to learn the basics.
I created a few projects. The full source is linked at the end of the article
Entitas Template – An ideal starting point for your next Entitas project
Entitas ‘Pong’ – I started with the template and created a simple, complete game
While making those projects, reading documentation, and dissecting other freely available Entitas projects, I learned a lot.
Entitas for C# / Unity3D
Entitas is a super fast Entity Component System Framework (ECS) with a version created specifically for C# and Unity3D.
As the creators explain — Entitas is open source. Internal caching and blazing fast component access makes it second to none. Several design decisions have been made to work optimal in a garbage collected environment and to go easy on the garbage collector. Entitas comes with an optional code generator which radically reduces the amount of code you have to write and lets you write code that is super fast, safe and screams its intent.
Groups – Hold groups of Entities (as a query optimization) – E.g. BulletGroup
Components – Hold public variables ( Has no methods) – E.g. VelocityComponent
Systems – Query entity/components ( Has methods to do logic ). Most of your code is here, typically acting on one or more groups. E.g. VelocitySystem
Controllers – Monobehavior that bridge the ‘unity world’ with the ‘ECS world’. E.g. InputController
Example:
The InputController (Monobehavior) listens for Unity.Input on Update. When the phone’s screen is tapped, the InputController creates an InputEntity, each with an InputComponent with data regarding the tap The InputSystem (ISystem) processes once, only when new InputEntities exist, and it updates the PlayerEntity‘s VelocityComponent. The concept of Velocity is processed separately to update the game properly, etc…
Note: Entitas Components are NOT Unity Components (aka Monobehaviors). Think of an Entitas Component as serving ANY (one) of these roles;
Simple data storage – e.g. myComponent.score
Events – myEntity.WillDestroy(true) which functions somethinglike myEntity.SendEvent (new DestroyMeEvent());
Visual things – e.g. myComponent.view.gameObject with some standard Unity renderers attached
Visual Debugging
Systems
Entities
Performance
Based on data provided by the creators we see impressive run-time performance.
Unity vs Entitas. 1000 objects with 2 components;
Memory: 9x (2.9 MB vs 0.32 MB)
CPU: 17x (105ms vs 6ms)
Entitas is MUCH faster due to its many 0ptimizations: Entitas…
Reuses Entities
Reuses Components
Caches Groups
Index Components
Compared to a typical Unity game architecture, ECS processes logic only when processing is necessary. The Entitas system architecture and query system allows me to mix ‘processing’ strategies. For example with 100 characters onscreen I can;
Move all characters every monobehavior.Update()
Move half one one frame and the rest on another frame
Move only those who have a changed position
Etc…
Evaluation
Pros
FAST performance
Data-binding is implicit (OnEnityAdded/Removed/Updated, OnComponentAdded/Removed/Replaced)
Querying is fast, efficient, and opens your mind to new ways to think about your game.
ECS embraces Single Responsibility Principle (SRP) ( link )
Testability*
Code sharing (use C# on client AND server)*
* These features are greatly enabled because the UnityEngine.* classes are separated by-design from the bulk of your Entitas game logic. Testing UnityEngine.* has historic challenges. Running UnityEngine.* on server is either undesirable or impossible depending on your technology stack.
Cons
Developing with Entitas is easy, but refactoring has challenges (see Growing Pains below)
Best to START your project with Entitas
Best to FULLY embrace your project with Entitas (Rather than use Entitas partially in your game)
Collaboration takes effort between Entitas and existing code (e.g. AssetStore code)
Neutral (Things to get used to)
With Entias you may have MANY more class files
Entitas uses code generation (its optional, but I always used it).
You feel like the bulk of your Entitas code is disconnected from Unity. I consider this a PRO, but it takes some time to get used to. Ex. Its standard practice to NOT store your character’s position on the gameObject.transform.
Everything can access everything. There is a more ‘global’ state. Ex. your enemy’s code scope can fully access your hero’s health. The creators see standard OOP-based gaming structure as ‘little boxes’ (encapsulation) that you must break with every major refactor and game feature added, so instead there is much less emphasis on these ‘little boxes’ in the Entitas paradigm.
Growing Pains
Fixing compilation errors
The (optional) Enttias code generator is based on runtime reflection. The project has to compile before you can generate. This is not an issue when you creating new components, however when it comes to changing or deleting components, your code might stop compiling. Here is a list of recipes how you can avoid bigger hassle while changing and deleting components.
Ex. I stored the position of a character as float x, y, z. Then later changed it to a custom Vector3 class implementation. In a project without code generation your IDE’s ‘Rename’ or ‘Find-Replace’ functionality makes this pretty straight-forward. However, not all of the previously generated code will respect your refactor and a bit (30-60 seconds) of manually changes will be needed. Then once the project compiles again (you can use the Entitas code generation menu option to clean up the code again. I don’t have a suggestion on how, but improving this workflow is highly desirable. For now we have some helpful workarounds.
Use this advice to speed the process when doing the following tasks;
I’ve been lucky to focus the past years on Unity and C#. Much of my demos, full-games, and tutorial are here RivelloMultimediaConsulting.com/unity/. If you have any comments or questions, I’d love to hear from you. Twitter.com/srivello.
Traditional Unity GUI Solutions
Unity is an amazing ecosystem for game development. For years, even big fans of the tool have been critical of the 2D/GUI/Text systems offered. Unity and the Unity community have launched various competing GUI systems (both immediate and retained) but the lack of features and lack of consistency leaves much to be desired. Luckily the amazing Asset Store (See my articles one, two, and three) of 3rd party tools and components has dutifully filled the needs of many devs.
Maintaining robust state (e.g. button) and handling adaptive layouts with Unity Legacy GUI was always very challenging. – Me
Older Options
Unity GUI (Immediate): This has been renamed to ‘Unity Legacy GUI’ to limit confusion with UI in Unity 4.6.x. The Legacy GUI will continue to be available and will remain as the recommended solution for Tools development (creating new windows and panels within Unity Editor). It requires developers to program the GUI in a loop that runs repeatedly.
NGUI (Retained): By FAR the most popular solution in recent years. There are alternatives in the asset store, but this is the most prevalent. Designed by a fantastic developer Michael Lyashenko with extensibility, flexibility, and power in mind — this project excels. It will still be popular for the future too because so many projects and teams are already at home with it. In 2013, Michael was hired by Unity to lead development on Unity UI for 4.6.x. He has since left the project, but his positive impact on the structure of the new solution is obvious.
Unity UI 4.6.x
This is the ‘New GUI’ for Unity. It was released publicly in 2014 after a long private and public beta period. I feel that Unity really wanted to get it right. And I think they have done a lot to guarantee adoption and success. Due to the complicated history of the GUI within Unity, the naming of the new system was/is debated. Perhaps some day in the future we’ll just call it ‘the’ UI of Unity and the other options will fade away. Before we’re sure of that, let’s take a look at the system. It is great.
I am pleased, and frankly shocked that Unity UI is available in the standard (free) version of Unity. – Me
Unity UI Features
Fast and flexible workflows: The workflow feels very at-home within Unity. Want to render text with a font? Drag a font from your OS into your project and *boom* its immediately available for run-time usage. No more need to ‘convert’ fonts to bitmap fonts. Its just as easy with images too. You can reskin any of the existing components too using your own graphics.
Low memory allocations and high performance: With batching, texture atlasing and the new canvas component, we’ve come up with the optimal solution to allow your UIs to execute quickly on GPUs. Draw calls are kept low and performance remains high across all supported Unity platforms and device resolutions.
Easy multiplatform deployment: Intelligence about the sizing (and resizing) of the app as well as layout groups (see below) helps greatly.
Unique animation capabilities: Use the existing (awesome) ‘Animation’ panel to animate ANY public property of your UI. Listen for UI events (e.g. button click) and react with transitions to new layouts or new scenes.
Intuitive Layout Tools: Laying out and resizing elements is easy with the new Unity UI. Design detailed layouts using the Rect Transform layout tools, and automate grids of UI elements with our built-in components.
Each element that can be laid out (text, image, etc…) is considered a component. The list of current components is finite, but any developer can create their own components from existing components or from scratch. We expect the list of built in components to grow — both from Unity and from the community.
Main UI Components (as of Unity 4.6.1)
UI Canvas: This is the required hierarchy root-parent to all UI elements. You can have multiple in the scene. It comes in 3 flavors; Overlay (flat HUD), Camera (tilted HUD), and World Space (speech bubble that follows your character through the scene). ( Docs, Video )
Take advantage of simple visual tools to anchor UI elements. The UI element maintains its anchored position regardless of changes to parent size or screen resolution. Want to anchor different elements relative to different positions on the canvas? No problem.
ADAPTIVE RESIZING
You can set UI elements to stretch along with the parent rectangle, or to maintain fixed margins inside it. In addition, each side of a UI element can be anchored individually, allowing you to set up sophisticated layouts without scripting.
ANIMATING UI
Animation plays a key part in your new UI workflow for creating dynamic layouts with slick transitions. Animate any part of your UI layouts, from bouncing buttons to animated material properties for detailed motion.
What’s Next?
With each minor and major update of Unity I expect vast bug-fixes and new features with the UI system. It is very stable and ready for use already. So start experimenting! I am already working on a few upcoming posts that will explore the system in more depth. If you want to see me cover something in particular, add a comment beneath this page.
New Unity UI (4.6.x): Unity UI Part 2 of 3: Demo – Basic UI (Source code + Video) [Coming Soon!]
New Unity UI (4.6.x): Unity UI Part 3 of 3: Demo – Combining UI w/ Frameworks/Architectures for scalability (Source code + Video) [Coming Soon!]
Unity3D is a powerful suite of tools (Project IDE, Code IDE, run-time) for game development. The Unity IDE is great for integrating your assets and code, setting up your scenes visually, and tweaking parameters through the powerful Inspector window. While Unity ships with a very capable code editor, MonoDevelop, serious developers prefer more powerful tools.
Here is a list of the must-have tools for Unity development.
Name: Visual Studio – Code Editor (Various Versions, Free to $499)
Details:Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It includes a code editor supporting IntelliSense as well as code refactoring. Built-in languages include Unity’s C# as well as C, C++, and more.
Pros:
Development Environment – Focus on creating value and accomplishing task quicker with a clean, fast and powerful development environment.
Semantic Code Analysis – Semantics (references, declarations, etc…) not just syntax are analyzed as you type. This allows for better refactoring.
Name: ReSharper ($185 for personal, $310 for a whole team)
Details: ReSharper is a renowned productivity tool that makes Microsoft Visual Studio a much better IDE. Thousands of developers worldwide wonder how they’ve ever lived without ReSharper’s code inspections, automated code refactorings, blazing fast navigation, and coding assistance.
The ultimate Agile tool is ReSharper. It is the one thing for C# developers that removes fear of change. Refactoring is just so damn easy that change isn’t scary. – Jaco Pretorius of ThoughtWorks
Pros:
Analyze code quality – On-the-fly code quality analysis in C#, VB.NET, XAML, ASP.NET, ASP.NET MVC, JavaScript, CSS, HTML, and XML. ReSharper tells you right away if your code contains errors or can be improved.
Instantly traverse your entire solution – Navigation features to instantly traverse your entire solution. You can jump to any file, type, or type member in no time, or navigate from a specific symbol to its usages, base and derived symbols, or implementations.
Eliminate errors and code smells – Instant fixes to eliminate errors and code smells. Not only does ReSharper warn you when there’s a problem in your code but it provides quick-fixes to solve them automatically.
Enjoy code editing helpers – Multiple code editing helpers including extended IntelliSense, hundreds of instant code transformations, auto-importing namespaces, rearranging code and displaying documentation.
Safely change your code base – Automated solution-wide code refactorings to safely change your code base. Whether you need to revitalize legacy code or put your project structure in order, you can lean on ReSharper.
Comply to coding standards – Code formatting and cleanup functionality is at your disposal to get rid of unused code and ensure compliance to coding standards.
Name: UnityVS ($99 for personal, $249 for a whole team)
Details: UnityVS is the missing ‘glue’ between the Unity IDE and the Visual Studio code editor.
Pros:
Connect Visual Studio’s debugger to Unity to debug your scripts. Put breakpoints, inspect and modify variables and arguments and evaluate complex expressions to fix bugs promptly. Without UnityVS, Visual Studio is just a powerful yet decoupled code editor.
UnityVS is packed with productivity features: code snippets, wizards, tool windows such as the Unity project explorer. UnityVS sends the Unity console directly to Visual Studio.
Play Unity and use breakpoints. At the breakpoint you can inspect AND EDIT the live values of any variable. If that doesn’t convince you to purchase UnityVS, you are insane. – Sam Rivello, RMC
Unity3D is a powerful suite of tools (Project IDE, Code IDE, run-time) for game development. A fantastic way to accelerate Unity & C# learning, empower development, and even make some money using the Unity Asset Store.
1. In my previous article “Introduction To the Asset Store” we see a complete Intro to the Unity Asset Store including how you can Fund your Indie Game Development by creating assets. Now let’s hear from some key developers who have published successful content.
For a complete introduction to Unity see my “Intro To Unity” Article. For a quick review: games in Unity are made up of multiple GameObjects that contain meshes, scripts, sounds, or other graphical elements like Lights. The Inspector displays detailed information about your currently selected GameObject, including all attached Components and their properties. Here, you modify the functionality of GameObjects in your scene. Any public field (publicly accessible variable) that is displayed in the Inspector can be directly modified while in edit mode and also while in play mode (the game is playing live). That is great.
The Inspector is a vital part of the Unity toolset with many powerful features. However, here I’ll focus on the specific programming-centric benefits of using the inspector.
Reasons
Gives us an (editable) view to all public fields on a game object.
Very powerful for experimentation, prototyping, and debugging.
Improves our interface to the code. E.g. instead of setting a color in code as #ff0000, you can have a GUI color picker in the inspector.
Example
[actionscript3]
public float myNumberField_float = 10f;
[/actionscript3]
NOTE: Only public fields are shown in the inspector.
NOTE: Adding the attribute [HideInInspector] before a public field name will allow it to be public (for coding) but not be shown in the inspector. This is outside the scope of this article, but it is quite useful sometimes.
Why Use Properties?
As we learn in C# In Depth, for every type you write, you should consider its interface to the rest of the world (including classes within the same assembly). This is its description of what it makes available, its outward persona. Implementation shouldn’t be part of that description, any more than it absolutely has to be. (That’s why I prefer composition to inheritance, where the choice makes sense – inheritance generally exposes or limits possible implementations.)
A property communicates the idea of “I will make a value available to you, or accept a value from you.” It’s not an implementation concept, it’s an interface concept. A field, on the other hand, communicates the implementation – it says “this type represents a value in this very specific way”. There’s no encapsulation, it’s the bare storage format. This is part of the reason fields aren’t part of interfaces – they don’t belong there, as they talk about how something is achieved rather than what is achieved.
I quite agree that a lot of the time, fields could actually be used with no issues in the lifetime of the application. It’s just not clear beforehand which those times are, and it still violates the design principle of not exposing implementation.
Reasons
Through encapsulation you now have a publicly accessible ‘thing’ just like a field, but here you can more completely control it. For example, within the set {} you could check the incoming ‘value’ and ensure it is between 1 and 10 before accepting the change.
Want to break into the debugger whenever the value changes? Just add a breakpoint in the setter.
Want to log all access? Just add logging to the getter.
Properties can be used for more advanced language features (like binding) too
Example
[actionscript3]
private float myNumberProperty_float = 10f;
public float myNumberProperty
{
set {
myNumberProperty_float = value;
}
get {
return myNumberProperty_float;
}
}
[/actionscript3]
Using Properties Via Inspector
By default, while Unity allows the inspector to shows with fields (variables), it does not render properties (getter/setters).
However, now you can do it all. I created the uExposeProperties (uEP) project. It is an asset store project for free. I’m hoping the community finds great uses for it. With uEP you get the best of both worlds; benefits of the inspector as well as properties.
Reasons
You get the benefits of the inspector
You get the benefits of properties
Your code stays very clean (only one simple attribute is added per property)
Example
NOTE: You must download the uEP package to use this functionality. It is not included in Unity.
[actionscript3]
private float myNumberProperty_float = 10f;
[ExposeProperty]
public float myNumberProperty
{
set {
myNumberProperty_float = value;
}
get {
return myNumberProperty_float;
}
}
[/actionscript3]
Drawbacks
There is one major drawback: theoretical performance. The current implementation depends on the OnInspectorGUI() event which is far more expensive than not using it.
Any ideas on how to improve that?
Next Steps
Download the source-code (See ‘Member Resources’). Available Now.
Get uEP on the asset-store (Coming Soon!)
Member Resources
[private_Free member]Enjoy this members-only content!
Here is a recap. In article 1 we used an RX approach for the following (academic) exercise;
Exercise: Dispatch a double click event based on custom timing parameters.
Reactive Extensions (RX) Solution
You can see the code is easy to read and powerful. While this algorithm requires state management, we don’t have to handle it directly. The internals of RX do it for us. You can imagine with a more complex feature, the code would be dramatically more than the RX solution. Below we break-down the code and explain each step.
NOTE: The TLP Library used in this article is a work in progress. It features only limited functionality and does not conform to common RX standards in the naming, signature, and structure of major elements. These differences are why the syntax and marble diagram names below are not 100% coincidental, but the concepts are similar.The TLP team invites other developers to fork the library and contribute to it. Still it is a fantastic learning tool and quite useful as-is.
Complete Code For RX Solution
[actionscript3 collapse=”true”]
//————————————–
// Methods
//————————————–
///<summary>
/// Use this for initialization
///</summary>
void Start ()
{
// VARIABLES
float maxTimeAllowedBetweenSingleCLicks_float = 1;
float delayBetweenAllowingADoubleClick_float = 5;
int clicksRequiredForADoubleClick_int = 2;
// CREATE OBSERVABLE
//(ORDER IS NOT IMPORTANT, BUT SUBSCRIBE MUST BE LAST)
var mouseDoubleClickObservable = Observable
//RUN IT EVERY FRAME (INTERNALLY THAT MEANS Update()
.everyFrame
//FILTER RESULTS OF THE FRAME
//WE CARE ONLY ‘DID USER CLICK MOUSE BUTTON?’
.filter (
_ =>
Input.GetMouseButtonDown (0)
)
//DID WE FIND X RESULTS WITHIN Y SECONDS?
.withinTimeframe (clicksRequiredForADoubleClick_int, maxTimeAllowedBetweenSingleCLicks_float)
//REQUIRE SOME ‘COOL-DOWN-TIME’ BETWEEN SUCCESSES
.onceEvery (delayBetweenAllowingADoubleClick_float);
//FOR EVERY EVENT THAT MEETS THOSE CRITERIA, CALL A METHOD
var subscription = mouseDoubleClickObservable
.subscribe (
_ =>
_onMouseEvent (MouseEventType.DoubleClick)
Marble diagrams are frequently in videos and blog entries from the Rx team explaining how certain operators in Rx work. From top to bottom there are; source stream(s), operators, and then the outgoing stream. The RXJava wiki has an especially pretty set of these images.
Let’s take a closer look at the syntax we used.
1. everyFrame: We observe an event stream on every frame (aka Update) in Unity…
2. filter: …where the mouse button is down…
3. withinTimeFrame: … X occurrences within Y seconds, which we call success…
4. onceEvery: …and success can only happen every Z seconds…
5. subscribe: …then call the desired method…
1. Operator: everyFrame (Similar to fromEvent)
From RXJava, we see that “the free-threaded nature of Rx is key to the performance characteristics of the event processing pipeline. However, in places where we bridge with the external world, this sometimes has negative effects due to thread-affine operations involved. The FromEvent [Pattern] bridges are one such place where we reach out to add and remove operations on events”.
Our specific use of everyFrame(), in pseudocode, would be like fromEvent (myMonoBehavior.Update).
2. Operator: filter
You can filter an Observable, discarding any items that do not meet some test, by passing a filtering function into the filter( ) method. For example, the following code filters a list of integers, emitting only those that are even (that is, where the remainder from dividing the number by two is zero):
3. Operator: withinTimeFrame (Similar to buffer)
The buffer( ) method periodically gathers items emitted by a source Observable into bundles, and emits these bundles as its own emissions. There are a number of ways with which you can regulate how buffer( ) gathers items from the source Observable into bundles:
4. Operator: onceEvery (Similar to sample)
5. Operator: subscribe
Subscribe is not an operator per se, it is something higher level in importance — When an observer subscribes to an observable sequence, the sequence begins and runs till completion. It is in the subscribe that we answer “What should ultimately happen with the outgoing event stream?”. In our example, we call a local method _onMouseEvent and we pass an appropriate MouseEventType.
NEXT STEPS
Our exploration into RX has just begun. Everyone interested is invited to download the source-code from my first article “Unity3D Reactive Extensions 1: An Introduction” and learn more. I continue to seek to find the best RX library for Unity development and have already begun to add RX into my game projects.`
Obviously fantastic software can be created without RX. We work without it every day. So why consider it? Well, essentially because asynchonous programming is hard to do, brittle, and is a challenge to scale. Well as the Reactive Manifesto explains;
The Need To Go Reactive – Each day applications are getting bigger, users expect more to happen in less time, and our projects are deployed to increasingly diverse set of (smaller) devices. To compete, old dogs need new tricks.
Reactive Applications – They react to events; loading data, user gestures, system failures.
Event-Driven – Event driven systems feature loose coupling between components and subsystems; a prerequisite for scalability and resilience.
Scalable – With RX our data runs ‘as if’ it is synchronous. Even the currently synchronous stuff. So as complexity comes and delays ensue, it can flex to be asynchronous. Secondly, the decoupled nature allows for location transparency. Benefits include multi-threading (where possible).
Resilient – In a reactive application, resilience is not an afterthought but part of the design from the beginning (see ‘onError’). Making failure a first class construct in the programming model provides the means to react to and manage it, which leads to applications that are highly tolerant to failure by being able to heal and repair themselves at run-time.
Responsive – The philosophies and practices of RX programming seek minimal latency to the user experience.
Conclusion – We expect that a rapidly increasing number of systems will follow this blueprint in the years ahead.
NOTE: While RX is designed to work with asynchronous data streams, it really doesn’t care. RX blindly accepts synchronous data within the same data flow.
Games
My first exposure to RX was regarding both UI behaviors (e.g. mouse events) and asynchronous data (e.g. loading from server). Gaming is inherently an asynchronous experience and the UI behaviors (e.g. user input) alone warrant a deep look at RX.
What is RX?
Iterables You are probably familiar with the concept of Iterables. In Unity we are used to using a List of values and looping through them and doing something synchronously. Observables In RX, it is observables that are a similar concept except the list may or may not be populated yet (i.e. it could start out empty) and may or may not be fully populated yet (i.e. ‘complete’). Still you iterate through the values, however asynchronously. Consider the following table;
Same Concept, Opposite Direction (Pull vs Push of Data)
Iterable (Pull)
Observable (Push)
getDataFromLocalMemory()
.skip(10)
.take(5)
.map({ s -> return s + " changed" })
.forEach({ println "next => " + it })
getDataFromNetwork()
.skip(10)
.take(5)
.map({ s -> return s + " changed" })
.subscribe({ println "onNext => " + it })
Reactive applications use observable models, event streams and stateful clients.
Reactive extensions (RX) is the library designed for Functional Reactive Programming (FRP). It is “a library for composing asynchronous and event-based programs by using observable sequences.” What is FRP? The name comes from the language features used and the philosophy of the data exchange (See Figure 1).
According to Haskell.org, Functional Reactive Programming (FRP) integrates time flow and compositional events into functional programming. This provides an elegant way to express computation in domains such as interactive animations, robotics, computer vision, user interfaces, and simulation. The major parts of RX;
Observables – Source of event stream & Subscriber – Observer of event stream.
Linq – Query the event stream (e.g. filter, map, zip)
Schedulers – Define the timing (inc. frequency) of concurrency.
As a quick primer, consider the following Observer->Subscriber event stream (See Figure 2).
Here the Observable emits an integer, it goes through an operation that adds 2 to it, and is finally received by the Subscriber (Observer). All steps are asynchronous, which means the subscriber does not actually know that the original value had been changed.
Operators
Between the Observable and the Subscriber the event stream can be operated upon. Here is a partial list of operators;
Transforming
map( ) — transform the items emitted by an Observable by applying a function to each of them
scan( ) — apply a function to each item emitted by an Observable, sequentially, and emit each successive value
Filtering
filter( ) — filter items emitted by an Observable
takeLast( ) — only emit the last n items emitted by an Observable
skip( ) — ignore the first n items emitted by an Observable
takeFirst( ) — emit only the first item emitted by an Observable, or the first item that meets some condition
Combining
startWith( ) — emit a specified sequence of items before beginning to emit the items from the Observable
merge( ) — combine multiple Observables into one
zip( ) — combine sets of items emitted by two or more Observables together via a specified function and emit items based on the results of this function
How to do RX?
Let’s consider a very typical operation; using mouse input to perform a simple operation. Compare the non-RX and RX approaches to the following (academic) exercise;
Exercise: Dispatch a double click event based on custom timing parameters.
Reactive Extensions (RX) Solution
You can see the code is easy to read and powerful. While this algorithm requires state management, we don’t have to handle it directly. The internals of RX do it for us. You can imagine with a more complex feature, the code would be dramatically more than the RX solution. You can read an in depth analysis of this code in my next article “Unity3D Reactive Extensions 2“.
[actionscript3 collapse=”true”]
//————————————–
// Methods
//————————————–
///<summary>
/// Use this for initialization
///</summary>
void Start ()
{
// VARIABLES
float maxTimeAllowedBetweenSingleCLicks_float = 1;
float delayBetweenAllowingADoubleClick_float = 5;
int clicksRequiredForADoubleClick_int = 2;
// CREATE OBSERVABLE
//(ORDER IS NOT IMPORTANT, BUT SUBSCRIBE MUST BE LAST)
var mouseDoubleClickObservable = Observable
//RUN IT EVERY FRAME (INTERNALLY THAT MEANS Update()
.everyFrame
//FILTER RESULTS OF THE FRAME
//WE CARE ONLY ‘DID USER CLICK MOUSE BUTTON?’
.filter (
_ =>
Input.GetMouseButtonDown (0)
)
//DID WE FIND X RESULTS WITHIN Y SECONDS?
.withinTimeframe (clicksRequiredForADoubleClick_int, maxTimeAllowedBetweenSingleCLicks_float)
//REQUIRE SOME ‘COOL-DOWN-TIME’ BETWEEN SUCCESSES
.onceEvery (delayBetweenAllowingADoubleClick_float);
//FOR EVERY EVENT THAT MEETS THOSE CRITERIA, CALL A METHOD
var subscription = mouseDoubleClickObservable
.subscribe (
_ =>
_onMouseEvent (MouseEventType.DoubleClick)
This code requires us to ‘manually’ maintain state. You can imagine with a more complex feature, the code here would be even more long-winded compared to the RX equivalent. Thankfully we can use Coroutines, otherwise there would be even more state-specific variable setup to write and maintain.
[actionscript3 collapse=”true”]
/// <summary>
/// KEEPING STATE: Timing information
/// </summary>
private int _state_clicksInLastXSeconds_int = 0;
NOTE: The Unity source-code for this article is available (See ‘Member Resources’ below).
RX & Unity
RX is a library that can be theoretically ported to any language. It currently exists in many languages; prominently Java and JavaScript. There are C# ports, but unfortunately not all are Unity-compatible. Unity’s (latest version 4.3.4) C# is compatible with an OLD version of Mono – Mono v2.65 (versus the latest available v3.2.7). Mono is the open source version of .NET which powers the C# language features of Unity. So, while RX libraries in C# are plentiful, there is not yet a Unity-compatible library which contains all the features we would like to see. All examples covered int his article are from the amazing people at Tiny Lab Production (TLP). Their RX library is available for free download (See ‘Member Resources’ below). The TLP team actively encourage use of and contribution to their library
RX Integration
Uses of RX
My first experimentation includes creating mostly non-RX games with few distinct RX elements (particularly mouse/key/gesture input scenarios). RX can be peppered into your project selectively. With more experience and awareness I expect the community to embrace more systemic usage of RX in Unity games. Ideas for RX In Gaming;
User Input (Mouse/Keyboard/Touch)
Constraints (keep player character onscreen)
Loading Assets
Loading From Backend Services
Multiplayer game state synchronization (e.g. ‘Did every player in this game room click start?’)
The RX library you choose must match your platform of choice. For me that is Unity & C#. Due to the “RX & Unity” issues above I can’t simply choose any C# library. The library chosen for this article was created by the fine developers at Tiny Lab Productions (TLP). I continue to search for a Unity-friendly library that has the scope and syntax exactly matching Netflix’s fantastic RxJava implementation.
NOTE: The TLP Library used in this article is a work in progress. It features only limited functionality and does not conform to common RX standards in the naming, signature, and structure of major elements. The TLP team invites other developers to fork the library and contribute to it. Still it is a fantastic learning tool and quite useful as-is.
Resources
While RX is new to the Unity Community, there are tons of non-Unity resources and tutorials available. Its important to note that until a robust Unity-compatible RX library is available all of the functionality may not be available. Here are some resources;
RxJava Documentation (A Must Read First) Functional Reactive Programming on the JVM (by the Netflix team)
The RX example above uses combinators to empower the event stream filtration and modification. Deeper explanation is available in my next article “Unity3D Reactive Extensions 2“.
Member Resources
[private_Free member]Enjoy this members-only content!
Unity3D is a powerful suite of tools (Project IDE, Code IDE, run-time) for game development. A fantastic way to accelerate Unity & C# learning, empower development, and even make some money using the Unity Asset Store.
In my previous article “Introduction To the Asset Store” we see a complete Intro to the Unity Asset Store including how you can Fund your Indie Game Development by creating assets. Now let’s hear from some key developers who have published successful content.
With 4 years experience in Unity, the team at Lumo Dev first created game examples in c#. Due to demand the product is also available in Java. Lumo found that new developers may not be sure where to start when planning a complete game, so their game templates help accelerate the learning process. The team confides that they too learn greatly from others’ work too.
Throw your product out there! Don’t wait until you created the best product before getting it out. Later, you can always go back and redo stuff, but the important thing is to get experience. – Lumo Dev Team
Jasper Stocker’s most popular product is Camera Path, but since its success he shifted focus. He now specializes in creating procedurally generated content. He has 5-6 years of experience so far with Unity.
I offer tools in a single (programming) language so I didn’t have to maintain two codebases. C# is dominant. I use every time. It’s a bit of a no brainer.
Q: Advice for Unity Game Developers? A: Learn to extend the editor. It’s pretty simple and you can make some really powerful tools. You can get rid of a lot of boring work by automating it. You can also make things designers might use the make levels more dynamic.
Q: Tips for Asset Store Devs? A: Work hard to make something robust and test the hell out of it. Try to break it by acting like a bored 5 year old.
Fabio has 3 years experience with Unity. He creates collections of materials/textures/shaders. He estimates he has spent 100-150 hours learning Unity, doing R&D for his assets, and creating them.
Mixed Dimensions has 5+ years experience with Unity development for many platforms (Web/IOS/Android/Windows/Mac). Lead, Muhannad confirms that his popular plugins have been downloaded thousands of times and his team continues to update the leading products every month. His tools depend on (and include) existing libraries such as LZMA, ClipperLib, and Poly2Tri (all Managed .NET).
Focus on mobile games and create something that has a need. – Muhannad from Mixed Dimensions.
Many Thanks
I am grateful to these developers for sharing some of their experiences.