<?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 Altaf Navalur on Medium]]></title>
        <description><![CDATA[Stories by Altaf Navalur on Medium]]></description>
        <link>https://medium.com/@altaf.navalur?source=rss-d459f7c3c2c1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Q-NmzYBNJRwS3glIXmcSYQ.jpeg</url>
            <title>Stories by Altaf Navalur on Medium</title>
            <link>https://medium.com/@altaf.navalur?source=rss-d459f7c3c2c1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 15:27:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@altaf.navalur/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[Serialize & Deserialize Color objects in Unity]]></title>
            <link>https://medium.com/@altaf.navalur/serialize-deserialize-color-objects-in-unity-1731e580af94?source=rss-d459f7c3c2c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/1731e580af94</guid>
            <dc:creator><![CDATA[Altaf Navalur]]></dc:creator>
            <pubDate>Mon, 27 Feb 2023 02:13:09 GMT</pubDate>
            <atom:updated>2023-02-27T02:13:09.172Z</atom:updated>
            <content:encoded><![CDATA[<p>Serialization and deserialization are two important concepts in computer science. Serialization is the process of converting an object into a stream of bytes, while deserialization is the process of reconstructing an object from a stream of bytes.</p><p>In this article we will talk about how we can handle Color serialization in Unity using <a href="https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@2.0/manual/index.html">Newtonsoft Json package</a></p><p>Lets consider an example, We have a PlayerData class as shown below.</p><pre>public class PlayerData<br>{<br>    public int health { set; get; }<br>    public string name { set; get; }<br>}</pre><p>Now, Lets build an object of PlayerData class &amp; serialize it.</p><pre>        PlayerData playerData = new PlayerData();<br>        playerData.health = 33;<br>        playerData.name = &quot;Avatar&quot;;<br><br>        string data = JsonConvert.SerializeObject(playerData);<br>        Debug.Log($&quot;Serialized: {data}&quot;);</pre><p>Serialized data of PlayerData looks like this,<br><em>Serialized: {“health”:33,”name”:”Avatar”}</em></p><p>Now, Let`s add a new member variable of type Color to PlayerData class &amp; try to serialize the object,</p><pre>public class PlayerData<br>{<br>    public int health { set; get; }<br>    public string name { set; get; }<br>    public Color color { get; set; }<br>}</pre><p>Try to serialize the object,</p><pre>        PlayerData playerData = new PlayerData();<br>        playerData.health = 33;<br>        playerData.name = &quot;Avatar&quot;;<br>        playerData.color = Color.gray;<br><br>        string data = JsonConvert.SerializeObject(playerData);<br>        Debug.Log($&quot;Serialized: {data}&quot;);</pre><p>When we execute the code above we get the following error,</p><p><em>JsonSerializationException: Self referencing loop detected for property ‘linear’ with type ‘UnityEngine.Color’. Path ‘color.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear.linear’.</em><br>…</p><p>To fix this error, we need to inform the serialization &amp; deserialization engine how to handle this data.</p><p>For this we need to create a new class that can handle serialization &amp; deseriliaztion of Color objects.</p><p>Lets create a new class &amp; name it ColorHandler.cs</p><pre>using System;<br>using Newtonsoft.Json.Converters;<br>using Newtonsoft.Json;<br>using UnityEngine;<br><br>public class ColorHandler : JsonConverter<br>{<br>    public ColorHandler()<br>    {<br>    }<br><br>    public override bool CanConvert(Type objectType)<br>    {<br>        return true;<br>    }<br><br>    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)<br>    {<br>        try<br>        {<br>            ColorUtility.TryParseHtmlString(&quot;#&quot; + reader.Value, out Color loadedColor);<br>            return loadedColor;<br>        }<br>        catch (Exception ex)<br>        {<br>            Debug.LogError($&quot;Failed to parse color {objectType} : {ex.Message}&quot;);<br>            return null;<br>        }<br>    }<br><br>    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)<br>    {<br>        string val = ColorUtility.ToHtmlStringRGB((Color)value);<br>        writer.WriteValue(val);<br>    }<br>}</pre><p>After we create ColorHandler class, we have to inform serialization &amp; deserialization engine that we need to use ColorHandler class to handle Color objects. We do this by adding an attribute to Color object declaration.<br><em>[JsonConverter(typeof(ColorHandler))]</em></p><p>The complete code looks like this,</p><pre>public class PlayerData<br>{<br>    public int health { set; get; }<br>    public string name { set; get; }<br>    [JsonConverter(typeof(ColorHandler))] public Color color { get; set; }<br>}</pre><p>Now, If we execute the code, we get the following output,</p><p><em>Serialized: {“health”:33,”name”:”Avatar”,”color”:”808080&quot;}</em></p><p>Simple, Isn&#39;t it :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1731e580af94" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Masked logging system in Unity]]></title>
            <link>https://medium.com/@altaf.navalur/masked-logging-system-in-unity-2c04a78c3140?source=rss-d459f7c3c2c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c04a78c3140</guid>
            <category><![CDATA[masked-logging]]></category>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Altaf Navalur]]></dc:creator>
            <pubDate>Sun, 13 Feb 2022 08:01:59 GMT</pubDate>
            <atom:updated>2022-02-13T08:13:57.783Z</atom:updated>
            <content:encoded><![CDATA[<p>Logging is one great way of debugging code. However, Adding too many logs will hinder the performance of your game. Not only that, logging critical data in production builds is unsafe. The conventional way of logging in Unity is to use one of the following functions in <a href="https://docs.unity3d.com/ScriptReference/Debug.html">Debug</a> class,</p><p><strong>Debug.Log</strong></p><p><strong>Debug.LogWarning</strong></p><p><strong>Debug.LogError</strong></p><p>These functions are quite handy &amp; serve the purpose of spitting out logs. However, For productions builds we have to remove them. There are several ways to strip out these logs. I prefer the masking system. Advantages are,</p><ol><li>We can add multiple masks. This gives us the flexibility to enable/disable logs by one or more masks.</li><li>We can add remove masks dynamically. This is the best part of it. I can make a build by disabling all masks &amp; then enable one or more dynamically on need basis.</li><li>Disable all masks in production build by just one line of code.</li></ol><p>I have been using masked logging system in all of my games for many years and it has been very useful to me.</p><p>Lets get started with the implementation.</p><p>For the sake of simplicity, I will discuss only Debug.Log, you can apply the same logic to `Debug.LogWarning` &amp; `Debug.LogError`.</p><p>Lets first define our masks,</p><pre>[Flags]<br>        public enum Mask<br>        {<br>            //No logs<br>            None = 0,<br>            //All logs related to asset loading<br>            Resources = 1,<br>            //All logs related to FB<br>            Facebook = 1 &lt;&lt; 3,<br>            //All logs related to API/WebService calls<br>            WebService = 1 &lt;&lt; 4,<br>            //All logs related to sound/audio<br>            Sound = 1 &lt;&lt; 5,<br>            //All logs related to in-app purchasing<br>            Purchasing = 1 &lt;&lt; 6,<br>            //All logs related to game play<br>            Game = 1 &lt;&lt; 7,              <br>        }</pre><p>We created an enum for our masks. Notice I have <strong>Flags</strong> attribute to the enum.</p><p>We have defined few masks. These masks are flags and can be set or unset as we need.</p><p>Lets write our logging functionality. As this is advanced debugging class, lets create a class and name it XDebug. Inside the class we declare a variable to hold our mask status.</p><p>Note: I have made the class static as I do not want anyone to create instances of it.</p><pre>public static class XDebug<br>{<br>  private static Mask mDebugMask = Mask.None; <br>}</pre><p>Next lets add a function to add the mask</p><pre>public static void AddMask(Mask inMask)<br>        {<br>            mDebugMask |= inMask;<br>        }</pre><p>Now we have a function to add a mask, We also need a function to remove the mask. Lets add that function,</p><pre>public static void RemoveMask(Mask inMask)<br>        {<br>            mDebugMask &amp;= ~inMask;<br>        }</pre><p>With all the core functionality written, next we write a function to check if the given mask is set or unset. Lets call this function <strong>CanLog.</strong></p><pre>public static bool CanLog(Mask inMask)<br>        {<br>            if ((mDebugMask &amp; inMask) != 0)<br>                return true;<br>            else<br>                return false;<br>        }</pre><p>This function checks if the given mask is set or unset. Returns true if set else returns false. We will use this function to determine if we should write this log or not.</p><p>Now lets write our function that we call for the purpose of logging.</p><pre>public static void Log(object log, Mask inMask = Mask.None)<br>        {<br>if (CanLog(inMask))<br>                UnityEngine.Debug.Log(log);<br>        }</pre><p>In the function above, we first check if the mask is set. If set, we log the data else ignore. That`s all it is for the masked logging system.</p><p>The complete code is as follows,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/859bf33fa1284dde4522e579ee28aafb/href">https://medium.com/media/859bf33fa1284dde4522e579ee28aafb/href</a></iframe><p>Now lets test it out. Create a new script and name it <strong>MaskingTest.cs </strong>Attach this file to an empty GameObject in scene and save the scene. Write the following code in the script,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e3591f1e7fcaa08a1a576cfbab2df20/href">https://medium.com/media/0e3591f1e7fcaa08a1a576cfbab2df20/href</a></iframe><p>The first log will not be written to console as there is no mask enabled. However, the second log will be written to console as we have enabled masking. We can set multiple masks in one function by ORing the masks.</p><p>Apply the same login for Debug.LogWarning &amp; Debug.LogError.</p><p>Hope it helps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c04a78c3140" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EditorPrefs in Unity]]></title>
            <link>https://medium.com/@altaf.navalur/editorprefs-in-unity-22a0bf39732b?source=rss-d459f7c3c2c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/22a0bf39732b</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[editorprefs]]></category>
            <category><![CDATA[script-editor]]></category>
            <category><![CDATA[unity3d]]></category>
            <dc:creator><![CDATA[Altaf Navalur]]></dc:creator>
            <pubDate>Sat, 17 Jul 2021 19:01:59 GMT</pubDate>
            <atom:updated>2021-07-17T19:01:59.307Z</atom:updated>
            <content:encoded><![CDATA[<p>Save <a href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</a> per project in Unity</p><p>If you have been using Unity, you already know that there are two kinds of preferences storage available viz <a href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</a> and <a href="https://docs.unity3d.com/ScriptReference/PlayerPrefs.html">PlayerPrefs</a>. While PlayerPrefs are used to save/load data for the game, EditorPrefs are used to save editor specific preferences.</p><p>Unity`s built in EditorPrefs API is good enough, the only problem is, the preferences are shared across projects. Example, If you working of multiple projects &amp; save a key <strong>ParseAssets</strong> to true using <a href="https://docs.unity3d.com/ScriptReference/EditorPrefs.SetBool.html"><strong>SetBool</strong></a>, all projects will have the same value. In some cases one might need it to be in such a way, but in most cases we wouldn&#39;t want that.</p><p>We can write our own API to help us out in such cases. Instead of directly calling EditorPrefs API`s we can wrap those API`s in our own custom class.</p><p>We can create a unique key by using projects path as part of the key. For example, if the key we are trying to save/load is <strong>ParseAssets, </strong>we can use <strong>UniqueKeyPerProject-ParseAssets,</strong> This way all our preferences will be saved uniquely to each project.</p><blockquote>Lets get to coding now…</blockquote><p>First step is to generate a unique key by using project path. To achieve this we will generate <a href="https://en.wikipedia.org/wiki/MD5">MD5</a> of our project path. Once we have our unique key, all keys will be prepended with this unique. For example,</p><pre>EditorPrefs.SetString(&quot;OurUniqueKey&quot; + key, value);</pre><p>Create a new script file <strong>XGEditorPrefs.cs</strong> under Editor folder. If you don&#39;t have editor folder, please create one under Assets folder.</p><p>Declare a static string variable, this variable will hold our unique key for the current project.</p><pre>private static string mAppKey = null;</pre><p>Write a function to generate MD5 hash from the given string. In our case, we will pass path of our project.</p><pre>private static string GetMd5Hash(string input)<br>        {<br>            MD5 md5 = MD5.Create();<br>            byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));<br>            StringBuilder sb = new StringBuilder();</pre><pre>for (int i = 0; i &lt; data.Length; i++)<br>                sb.Append(data[i].ToString(&quot;x2&quot;));</pre><pre>return sb.ToString();<br>        }</pre><p>Now define a static constructor to initialize our static variable. Call <strong>GetMd5Hash </strong>and pass path to our project.</p><p>static XGEditorPrefs()<br>{<br> mAppKey = GetMd5Hash(Application.dataPath) + “-”;<br>}</p><p>Now we have a unique key for our project, next we write all API`s needed to Get &amp; Set preferences. Ex SetString, GetString, SetInt, GetInt etc by prepending our unique key to each key passed to it.</p><pre>public static void SetString(string key, string value)<br>{<br>    EditorPrefs.SetString(mAppKey + key, value);<br>}</pre><pre>public static string GetString(string key, string defaultValue)<br>{<br>    return EditorPrefs.GetString(mAppKey + key, defaultValue);<br>}</pre><p>Full source code below,</p><pre>using System.Security.Cryptography;<br>using System.Text;<br>using UnityEditor;<br>using UnityEngine;</pre><pre>namespace XcelerateGames.Editor<br>{<br>    /// &lt;summary&gt;<br>    /// EditorPrefs data is common across projects, that means settings from one project will reflect in other, This wrapper class fixes that issue<br>    /// by generating a unique key by project path.<br>    /// &lt;/summary&gt;<br>    public class XGEditorPrefs<br>    {<br>        /// &lt;summary&gt;<br>        /// Return MD5 hash of the text<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;input&quot;&gt;text&lt;/param&gt;<br>        /// &lt;returns&gt;&lt;/returns&gt;<br>        private static string GetMd5Hash(string input)<br>        {<br>            MD5 md5 = MD5.Create();<br>            byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));<br>            StringBuilder sb = new StringBuilder();</pre><pre>for (int i = 0; i &lt; data.Length; i++)<br>                sb.Append(data[i].ToString(&quot;x2&quot;));</pre><pre>return sb.ToString();<br>        }</pre><pre>/**Unique id by project path, This id will be pre-pended to each key*/<br>        private static string mAppKey = null;</pre><pre>/// &lt;summary&gt;<br>        /// Static constructor to set unique kay based on project path<br>        /// &lt;/summary&gt;<br>        static XGEditorPrefs()<br>        {<br>            mAppKey = GetMd5Hash(Application.dataPath) + &quot;-&quot;;<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Checks if the given key exists<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;Key to check&lt;/param&gt;<br>        /// &lt;returns&gt;true if exists, else false&lt;/returns&gt;<br>        public static bool HasKey(string key)<br>        {<br>            return EditorPrefs.HasKey(mAppKey + key);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;returns&gt;value of the given key&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetString(string,string)<br>        public static string GetString(string key)<br>        {<br>            return EditorPrefs.GetString(mAppKey + key);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key if it exists, else returns default value<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;returns&gt;value of the given key&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetString(string)<br>        public static string GetString(string key, string defaultValue)<br>        {<br>            return EditorPrefs.GetString(mAppKey + key, defaultValue);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Sets the value of the preference identified by key. Note that EditorPrefs does not support null strings and will store an empty string instead.<br>        /// &lt;param name=&quot;key&quot;&gt;key&lt;/param&gt;<br>        /// &lt;param name=&quot;value&quot;&gt;value to store&lt;/param&gt;<br>        public static void SetString(string key, string value)<br>        {<br>            EditorPrefs.SetString(mAppKey + key, value);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key&lt;/param&gt;<br>        /// &lt;returns&gt;coresponding value&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetBool(string,bool)<br>        public static bool GetBool(string key)<br>        {<br>            return EditorPrefs.GetBool(mAppKey + key);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key if it exists, else default value<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key&lt;/param&gt;<br>        /// &lt;returns&gt;coresponding value&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetBool(string)<br>        public static bool GetBool(string key, bool defaultValue)<br>        {<br>            if (HasKey(key))<br>                return EditorPrefs.GetBool(mAppKey + key);<br>            return defaultValue;<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Sets the value of the preference identified by key.<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;param name=&quot;value&quot;&gt;value to save&lt;/param&gt;<br>        public static void SetBool(string key, bool value)<br>        {<br>            EditorPrefs.SetBool(mAppKey + key, value);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key in the preference file if it exists.<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;returns&gt;corresponding value&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetInt(string, int)<br>        public static int GetInt(string key)<br>        {<br>            return EditorPrefs.GetInt(mAppKey + key);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key in the preference file if it exists else default value<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;returns&gt;corresponding value&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetInt(string)<br>        public static int GetInt(string key, int defaultValue)<br>        {<br>            if (HasKey(key))<br>                return EditorPrefs.GetInt(mAppKey + key);<br>            return defaultValue;<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Sets the value of the preference identified by key as an integer.<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;param name=&quot;value&quot;&gt;value to save&lt;/param&gt;<br>        public static void SetInt(string key, int value)<br>        {<br>            EditorPrefs.SetInt(mAppKey + key, value);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key in the preference file if it exists.<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;returns&gt;corresponding value&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetFloat(string)<br>        public static float GetFloat(string key)<br>        {<br>            return EditorPrefs.GetFloat(mAppKey + key);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Returns the value corresponding to key in the preference file if it exists else default value<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;returns&gt;corresponding value&lt;/returns&gt;<br>        /// <a href="http://twitter.com/see">@see</a> GetFloat(string,float)<br>        public static float GetFloat(string key, float defaultValue)<br>        {<br>            if (HasKey(key))<br>                return EditorPrefs.GetFloat(mAppKey + key);<br>            return defaultValue;<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Sets the value of the preference identified by key as a float.<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;key to use&lt;/param&gt;<br>        /// &lt;param name=&quot;value&quot;&gt;value to save&lt;/param&gt;<br>        public static void SetFloat(string key, float value)<br>        {<br>            EditorPrefs.SetFloat(mAppKey + key, value);<br>        }</pre><pre>/// &lt;summary&gt;<br>        /// Delete a key<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;<br>        public static void DeleteKey(string key)<br>        {<br>            EditorPrefs.DeleteKey(mAppKey + key);<br>        }<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22a0bf39732b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Controlling game speed in Unity]]></title>
            <link>https://medium.com/@altaf.navalur/controlling-game-speed-in-unity-6b366e6905a2?source=rss-d459f7c3c2c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b366e6905a2</guid>
            <category><![CDATA[timescale-in-unity]]></category>
            <category><![CDATA[game-speed-in-unity]]></category>
            <category><![CDATA[script-editor]]></category>
            <dc:creator><![CDATA[Altaf Navalur]]></dc:creator>
            <pubDate>Sat, 17 Jul 2021 17:11:54 GMT</pubDate>
            <atom:updated>2021-07-17T17:19:02.327Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to control Time.timeScale in Unity via an editor script</h4><blockquote>Often in game development its essential to speed up or slow down for special effects. Sometimes, it becomes important to speedup or slowdown during development process for debugging. For instance, you are reviewing an animation, with regular speed you may not be able to come to a conclusion. In such cases slowing down the game helps a lot. Take another example, you are waiting for time to elapse, say you have to wait for 5 mins before you perform a certain action. Instead of waiting for 5 mins each time, you can speedup the game by 10x, it surely saves a lot of time :)</blockquote><blockquote>In Unity we control speed by changing Time.timeScale. Default value of timeScale is 1. If we increase the value, game will run faster &amp; reducing it below 1 will slow it down.</blockquote><blockquote>To change the speed, all we need to do is set the value of Time.timeScale at runtime.</blockquote><p>To increase speed set the value above 1. For instance,</p><pre>Time.timeScale = 2f;</pre><p>will double the speed &amp;</p><pre>Time.timeScale = 0.5f;</pre><p>will reduce game speed to half.</p><p>Here is the full source code. Create a new file TimeScaleHelper.cs &amp; paste the code below. Make sure to keep this file under Editor folder.</p><pre>using UnityEditor;<br>using UnityEngine;</pre><pre>namespace XcelerateGames.Editor<br>{<br>    /// &lt;summary&gt;<br>    /// A helper calss that runs only on IDE to help speedup or slow down the game<br>    /// &lt;/summary&gt;<br>    public class TimeScaleHelper : EditorWindow<br>    {<br>        [MenuItem(&quot;Tools/Utils/Time Scale&quot;)]<br>        private static void Init()<br>        {<br>            EditorWindow.GetWindow&lt;TimeScaleHelper&gt;(&quot;Time Scale&quot;);<br>        }</pre><pre>private void OnGUI()<br>        {<br>            if(!EditorApplication.isPlaying)<br>            {<br>                GUILayout.Label(&quot;Time Scale can be updated in play mode only!&quot;);<br>            }</pre><pre>EditorGUI.BeginDisabledGroup(!EditorApplication.isPlaying);<br>            GUILayout.BeginVertical();</pre><pre>GUILayout.BeginHorizontal();</pre><pre>if (GUILayout.Button(&quot;0&quot;))<br>                Time.timeScale = 0F;<br>            if (GUILayout.Button(&quot;0.5&quot;))<br>                Time.timeScale = .5F;<br>            if (GUILayout.Button(&quot;1x&quot;))<br>                Time.timeScale = 1F;<br>            if (GUILayout.Button(&quot;2X&quot;))<br>                Time.timeScale = 2F;<br>            if (GUILayout.Button(&quot;10X&quot;))<br>                Time.timeScale = 10F;</pre><pre>GUILayout.EndHorizontal();</pre><pre>GUILayout.EndVertical();</pre><pre>GUILayout.Label(&quot;Time Scale: &quot; + Time.timeScale);<br>            EditorGUI.EndDisabledGroup();</pre><pre>Repaint();<br>        }<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b366e6905a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>