<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ruben Rizzi on Medium]]></title>
        <description><![CDATA[Stories by Ruben Rizzi on Medium]]></description>
        <link>https://medium.com/@raynor85?source=rss-a4f8486b6b55------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*4du4QjDWqJ7WS7E_.</url>
            <title>Stories by Ruben Rizzi on Medium</title>
            <link>https://medium.com/@raynor85?source=rss-a4f8486b6b55------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 16:46:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raynor85/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[New Unity UIElements Runtime system, how to use it now!]]></title>
            <link>https://medium.com/@raynor85/new-unity-uielements-runtime-system-how-to-use-it-now-f560c4cb22d2?source=rss-a4f8486b6b55------2</link>
            <guid isPermaLink="false">https://medium.com/p/f560c4cb22d2</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[runtime]]></category>
            <category><![CDATA[editor]]></category>
            <category><![CDATA[ui]]></category>
            <dc:creator><![CDATA[Ruben Rizzi]]></dc:creator>
            <pubDate>Wed, 01 Apr 2020 20:29:39 GMT</pubDate>
            <atom:updated>2020-04-01T20:38:42.395Z</atom:updated>
            <content:encoded><![CDATA[<p>Many of you probably heard of the new system to build UI elements to replace the existing two. This screenshot taken from the <a href="https://docs.unity3d.com/Manual/UIToolkits.html">official doc</a> visualises it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1TSExDaLoimMAeljsKF5LQ.png" /></figure><p>As you can see, the <strong>UIElements</strong> system is already available in <strong>Unity Editor</strong> and you can start creating custom UI editors right now, but after spending one whole day trying to figure out how to actually use the same system at runtime for my own game, I decided to put together a little practical guide to do so.</p><p>I was able to make it work for version <strong>2019.3 (stable)</strong> and for <strong>2020.1 (beta)</strong>, so be sure you are not at any previous versions.</p><ul><li>Open the <strong>Package Manager</strong>, tick <strong>Show preview packages</strong> from the accordion, and download <strong>UI Builder </strong>(the one I used is the latest one at the time I wrote this guide: <em>0.10.2</em>)</li><li>Download the <strong>UI Runtime</strong> package from <a href="https://unity.bintray.com/unity/com.unity.ui.runtime/-/">https://unity.bintray.com/unity/com.unity.ui.runtime/-/</a> (the one I used is the latest one at the time I wrote this guide: <em>0.0.4</em>)</li><li>Similarly as before, through the <strong>Package Manager</strong>, add the package you just download to Unity</li></ul><p>And that’s it! Now just be sure that it all works adding a <strong>Panel Renderer </strong>Component to any GameObject in the scene from the inspector (Unity will add a <strong>Panel Scaler</strong> automatically as well), then select an <strong>Uxml</strong> asset in the <strong>Panel Renderer</strong> from the existing ones coming within the <strong>UI Runtime</strong> package (I have personally used <strong>TanksStatsBox</strong>). Here is the screenshot to show the <strong>UI Builder</strong> on the left and the <strong>Panel Renderer </strong>which renders in the game view <strong>TanksStatsBox</strong>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H9eQNXdoDQzwm9RcXUmyYQ.png" /></figure><p>Just to clarify, the <strong>UI Renderer</strong> is still under development and it might change in the future, but if like me you can’t wait to try out and familiarise with it before it becomes the new standard for building UI both in the Editor and in the Scene, please leave a comment! Last but not least, the <strong>UI Runtime</strong> package is based on the amazing presentation <a href="https://www.youtube.com/watch?v=t4tfgI1XvGs">Building UI for games with the new UI Builder — Unite Copenhagen</a> and the source code of the project of the presentation can be found on GitHub <a href="https://github.com/Unity-Technologies/UIElementsUniteCPH2019RuntimeDemo">here</a>, so it makes sense to watch it if you haven’t done it already. Happy styling!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f560c4cb22d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A different strategy to share data between scenes in Unity]]></title>
            <link>https://medium.com/@raynor85/a-different-strategy-to-share-data-between-scenes-in-unity-c1111ab38c12?source=rss-a4f8486b6b55------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1111ab38c12</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[scriptableobject]]></category>
            <category><![CDATA[scenes]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[singleton]]></category>
            <dc:creator><![CDATA[Ruben Rizzi]]></dc:creator>
            <pubDate>Sun, 22 Mar 2020 02:19:20 GMT</pubDate>
            <atom:updated>2020-03-22T02:40:30.198Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0tYD1UoUCKCliHdCDxj50A.jpeg" /></figure><p>Every Unity game developers encountered the need to share data between scenes. From my experience, the most common method to achieve this is using the Singleton pattern, and there are two main variation of them: one is applying the Singleton pattern to a MonoBehaviour class and the second is applying it to a ScriptableObject.</p><p>According to the official Unity <a href="https://wiki.unity3d.com/index.php/Singleton">wiki</a>, there are a couple of alternatives to the Singleton pattern, which are using Scriptable Objects and the Toolbox pattern. I won’t go into details in the Toolbox pattern, which looks like an enhanced version of the Singleton Pattern and somewhat related to the Service Locator Pattern, but I will be focussing on Scriptable Objects instead.</p><p>From my experience, Scriptable Objects alone can’t replace Singletons, especially in the needs of passing data between scenes. The official wiki claims otherwise, citing the amazing presentation by Ryan Hipple from Schell Games at Unite Austin 2017 titled <a href="https://youtu.be/raQ3iHhE_Kk">Game Architecture with Scriptable Objects</a>. If you haven’t watched it yet, please do yourself a favour and watch it, or alternatively just jump <a href="https://youtu.be/raQ3iHhE_Kk?t=1401">here</a> and watch the first demo example, in which Ryan explains how data driven components using Scriptable Objects leads to cleaner and more modular code.</p><p>If you watched the demo, you noticed that every time the Sphere of Pain component hits the Player, the Scriptable Object containing the PlayerHP decreases, and the HealthUI component and the AudioSystem react to this change of data. A direct assumption I made watching this is, since Scriptable Objects are data containers, they exist throughout the whole lifecycle of the game, and because of it any components in every scenes can access their data. This means that, if a component in scene A has a reference to a Scriptable Object, it can access its data, and modify it. Doing so, another component in another scene can access the same Scriptable Object and read the modified data. From the look of it, we just solved the problem of relying on the Singleton Pattern to share data between scenes!</p><p>Unfortunately, it is not really like it. We indeed achieved the goal, but I see some major drawbacks in modifying Scriptable Objects from code:</p><ul><li>The modification persists after play mode, so in Ryan example the game designer / developer must remember to reset the PlayerHP to its original value before committing new code and before entering play mode again.</li><li>Scriptable Objects are basically files, in fact they are persistent even between play modes and when closing and entering the Unity Editor. Using them to store and modify data in this way at runtime is not very efficient; variables in memory should be used for this, not files saved in the hard disk drive.</li></ul><p>So Scriptable Objects should really be used to keep data that can be edited by game designers, but they shouldn’t be changed directly by code. So what is the advantage of them, you might wonder? Scriptable Objects helps to reduce your project’s memory usage by avoiding copies of values, as described in the <a href="https://docs.unity3d.com/Manual/class-ScriptableObject.html">ScriptableObject Unity Manual</a>. That is also why I suggest to always use the get property accessors for each value, to enforce data immutability:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e2ff5dcd53450858a5465ddda154da3/href">https://medium.com/media/1e2ff5dcd53450858a5465ddda154da3/href</a></iframe><p>So, if we can’t use Scriptable Object to mutate data, what do we do?</p><p>Before diving into the implementation of the solution I came up with, I’d like to describe why I am not happy with the Singleton Pattern:</p><ul><li>Break Single-responsibility principle. It is very tempting to create a gigantic GameManager Singleton and put inside all the global variables we need, ending up breaking the Single-responsibility principle.</li><li>Break Modularity. You call the singleton directly, so many entities depend on it. Ideally you should pass its capability as dependency.</li><li>Hard to test. Direct consequence of above.</li><li>Hard to refactor. Direct consequence of above.</li><li>Global scope. They are very powerful and easy to abuse.</li></ul><p>I really love Ryan’s approach to use Scriptable Object data to drive component behaviours in the scene, and at the same time use the data as their controller, so all components are independent between one another. This approach solves all the problems mentioned before, but we can’t really use it. So how can we combine Ryan’s approach without mutating the Scriptable Object data directly? Well, we can use Scriptable Objects in the same way he did, but instead of allowing components to mutate their data directly, we wrap inside the Scriptable Object a nested Singleton to host the editable data, and initialise it with the Scriptable Object immutable data, so the other components can access to the Singleton to get and set the data through the Scriptable Object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*cpNugWoSV_6f5Oue2MjzRA.jpeg" /></figure><p>I know, it sounds a little bit confusing, so it is probably time to see some code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/438ea697e57efddac1820614242cf018/href">https://medium.com/media/438ea697e57efddac1820614242cf018/href</a></iframe><p>We can immediately see the advantages:</p><ul><li>No more global scope: the Singleton <strong>LifeManager</strong> cannot be access directly, so components in the scene must reference <strong>PlayerLives.asset</strong> to access its data.</li><li>Modularity: same reason above.</li><li>Easy to test: now that there is not a hard reference between the Singleton and the components which use it, we can easily mock it.</li><li>Single-responsibility principle: if you follow this architecture you will probably end up creating a small Singleton which only cares about the data of its parent Scriptable Object, so by design it is quite difficult to break the Single-responsibility principle.</li></ul><h3>Conclusion</h3><p>I am using this approach in the game I am actually developing, and I was able to remove the need of a global Singleton to share data between scenes like remaining lives, scores and player progression. I hope you find it useful and I can’t wait to hear your thoughts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1111ab38c12" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>