<?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 Ibrahim Aslan on Medium]]></title>
        <description><![CDATA[Stories by Ibrahim Aslan on Medium]]></description>
        <link>https://medium.com/@ibraslnn?source=rss-e38885de7abf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*np6pp6ivWylk6WUiiBdlAg.jpeg</url>
            <title>Stories by Ibrahim Aslan on Medium</title>
            <link>https://medium.com/@ibraslnn?source=rss-e38885de7abf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 00:02:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ibraslnn/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[Leveraging Object Pooling Pattern in Unity]]></title>
            <link>https://medium.com/@ibraslnn/leveraging-object-pooling-pattern-in-unity-8c506707d693?source=rss-e38885de7abf------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c506707d693</guid>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity2d]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Ibrahim Aslan]]></dc:creator>
            <pubDate>Thu, 30 May 2024 19:39:34 GMT</pubDate>
            <atom:updated>2024-05-30T19:39:34.922Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oeawgYeElEos3Oj6sHZS5Q.png" /></figure><p>In Unity game development, optimizing performance and resource management is paramount, especially in resource-intensive games. One design pattern that addresses these concerns effectively is the Object Pooling pattern. In this guide, we’ll explore what Object Pooling is, its benefits and drawbacks, and how to implement it in Unity.</p><h4>What is the Object Pooling Pattern?</h4><p>Object Pooling is a creational design pattern that pre-allocates a set of objects and reuses them rather than creating and destroying them repeatedly. This approach is particularly useful in scenarios where instantiating and destroying objects frequently can lead to performance bottlenecks, such as in games with numerous projectiles, enemies, or other dynamic elements.</p><h4>Advantages of Object Pooling</h4><ul><li><strong>Performance Improvement:</strong> Reduces the overhead of frequent instantiation and garbage collection, leading to smoother gameplay.</li><li><strong>Memory Management: </strong>Helps in managing memory more efficiently by controlling the number of active objects.</li><li><strong>Predictability:</strong> Provides more predictable performance characteristics, essential for maintaining consistent frame rates.</li></ul><h4>Disadvantages of Object Pooling</h4><ul><li><strong>Initial Memory Usage:</strong> Requires upfront memory allocation for the pool, which can be substantial depending on the pool size.</li><li><strong>Complexity:</strong> Adds complexity to the codebase, making it necessary to manage object states carefully.</li><li><strong>Overhead:</strong> Maintaining a large pool of objects that may not always be in use can lead to unnecessary memory consumption.</li></ul><h4>Implementation in Unity</h4><p>Let’s dive into a practical implementation of Object Pooling in Unity. We will create a generic object pool for GameObjects:</p><pre>using System.Collections.Generic;<br>using UnityEngine;<br><br>namespace ObjectPool<br>{<br>    /// &lt;summary&gt;<br>    /// Generic object pool for GameObjects.<br>    /// &lt;/summary&gt;<br>    /// &lt;typeparam name=&quot;T&quot;&gt;Type of the pooled items.&lt;/typeparam&gt;<br>    public class ObjectPool&lt;T&gt;<br>    {<br>        private readonly GameObject _poolItemPref;<br>        private readonly Transform _poolHolder;<br>        private readonly int _poolStartSize;<br>        private readonly Stack&lt;T&gt; _pool;<br>        private int _poolSize;<br><br>        /// &lt;summary&gt;<br>        /// Initializes a new instance of the ObjectPool class.<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;itemPref&quot;&gt;The prefab of the object to be pooled.&lt;/param&gt;<br>        /// &lt;param name=&quot;poolHolder&quot;&gt;The transform to serve as the parent for the pooled objects.&lt;/param&gt;<br>        /// &lt;param name=&quot;startSize&quot;&gt;The initial size of the object pool.&lt;/param&gt;<br>        public ObjectPool(GameObject itemPref, Transform poolHolder, int startSize)<br>        {<br>            _poolItemPref = itemPref;<br>            _poolHolder = poolHolder;<br>            _poolStartSize = startSize;<br>            _poolSize = 0;<br>            _pool = new Stack&lt;T&gt;();<br>            Fill();<br>        }<br><br>        /// &lt;summary&gt;<br>        /// Fills the object pool to reach the specified start size.<br>        /// &lt;/summary&gt;<br>        private void Fill()<br>        {<br>            if (_poolSize &gt;= _poolStartSize) return;<br>            int step = _poolStartSize - _poolSize;<br>            for (int i = 0; i &lt; step; i++)<br>            {<br>                SpawnNewItem();<br>            }<br>        }<br>        <br>        /// &lt;summary&gt;<br>        /// Spawns a new item from the prefab and adds it to the object pool.<br>        /// &lt;/summary&gt;<br>        private void SpawnNewItem()<br>        {<br>            var item = Object.Instantiate(_poolItemPref, _poolHolder);<br>            Push(item);<br>        }<br><br>        /// &lt;summary&gt;<br>        /// Adds an item to the object pool.<br>        /// &lt;/summary&gt;<br>        /// &lt;param name=&quot;item&quot;&gt;The GameObject to be added to the pool.&lt;/param&gt;<br>        public void Push(GameObject item)<br>        {<br>            _poolSize++;<br>            _pool.Push(item.GetComponent&lt;T&gt;());<br>            item.SetActive(false);<br>        }<br><br>        /// &lt;summary&gt;<br>        /// Retrieves an item from the object pool, creating a new one if the pool is empty.<br>        /// &lt;/summary&gt;<br>        /// &lt;returns&gt;The retrieved item from the pool.&lt;/returns&gt;<br>        public T Pull()<br>        {<br>            if (_poolSize &lt;= 0) SpawnNewItem();<br>            _poolSize--;<br>            T item = _pool.Pop();<br>            item.SetActive(true); //Optional<br>            return item;<br>        }<br>    }<br>}</pre><p>In this implementation, we define a generic ObjectPool class that handles the pooling of GameObjects. Here&#39;s a breakdown of the key components:</p><ol><li><strong>Initialization:</strong> The constructor initializes the pool with a specified prefab, parent transform, and starting size.</li><li><strong>Filling the Pool:</strong> The Fill method ensures the pool is populated to the desired size at the start.</li><li><strong>Spawning New Items:</strong> The SpawnNewItem method instantiates new objects and adds them to the pool.</li><li><strong>Pushing to the Pool:</strong> The Push method deactivates and returns objects to the pool.</li><li><strong>Pulling from the Pool:</strong> The Pull method retrieves an object from the pool, instantiating a new one if necessary.</li></ol><h4>Example Usage</h4><p>Let’s consider an example where we implement an object pool for projectiles in a shooting game. This example demonstrates how to set up and use the ObjectPool class in a practical scenario.</p><p>First, create a prefab for the projectile and add a Projectile component to it:</p><pre>using UnityEngine;<br><br>public class Projectile : MonoBehaviour<br>{<br>    public float speed = 10f;<br><br>    private void Update()<br>    {<br>        transform.Translate(Vector3.forward * speed * Time.deltaTime);<br>    }<br><br>    private void OnBecameInvisible()<br>    {<br>        gameObject.SetActive(false);<br>    }<br>}</pre><p>Then, create the PlayerController to manage the projectiles and their pool:</p><pre>using UnityEngine;<br><br>public class PlayerController : MonoBehaviour<br>{<br>    [SerializeField] private Transform shootPoint;<br>    <br>    [SerializeField] private GameObject projectilePrefab;<br>    [SerializeField] private Transform poolHolder;<br>    [SerializeField] private int poolStartSize = 10;<br><br>    private ObjectPool&lt;Projectile&gt; _projectilePool;<br><br>    private void Awake()<br>    {<br>        _projectilePool = new ObjectPool&lt;Projectile&gt;(projectilePrefab, poolHolder, poolStartSize);<br>    }<br><br>    private void Update()<br>    {<br>        if (Input.GetButtonDown(&quot;Fire1&quot;))<br>        {<br>            Shoot();<br>        }<br>    }<br><br>    private void Shoot()<br>    {<br>        Projectile projectile = _projectilePool.Pull();<br>        projectile.transform.position = shootPoint.position;<br>        projectile.transform.rotation = shootPoint.rotation;<br>    }<br>}</pre><p>In this example:</p><ol><li><strong>Projectile: </strong>The Projectile script controls the projectile&#39;s movement and deactivates it when it goes off-screen.</li><li><strong>PlayerController:</strong> Has the object pool of projectiles. Pull a projectile from the pool when the player shoots.</li></ol><h4>Conclusion</h4><p>The Object Pooling pattern is a powerful tool in the game developer’s arsenal, offering significant performance benefits in scenarios with frequent object creation and destruction. While it adds some complexity, the trade-offs are often worth it for maintaining smooth and efficient gameplay.</p><p>As always, it’s crucial to balance the use of such patterns with thoughtful consideration of your game’s specific needs. Happy coding, and may your game development journey be ever smooth and efficient!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c506707d693" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Singleton Pattern in Unity]]></title>
            <link>https://medium.com/@ibraslnn/understanding-singleton-pattern-in-unity-adecd03af83c?source=rss-e38885de7abf------2</link>
            <guid isPermaLink="false">https://medium.com/p/adecd03af83c</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[unity2d]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Ibrahim Aslan]]></dc:creator>
            <pubDate>Sun, 28 Apr 2024 20:44:31 GMT</pubDate>
            <atom:updated>2024-04-28T20:53:37.508Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RnzKX9tQv9VlXqnh2g0ajA.png" /></figure><p>In the realm of Unity game development, managing the lifecycle and access to certain critical components or managers efficiently is crucial. One design pattern that comes to the rescue is the Singleton pattern. In this guide, we’ll delve into what the Singleton pattern is.</p><h3>What is the Singleton Pattern?</h3><p>The Singleton pattern is a creational design pattern that ensures a class has only one instance and provides a global point of access to that instance. It’s particularly useful when you want exactly one instance of a class to be accessible globally, such as managers, game controllers, or resources that should be shared across the entire application.</p><h3>Advantages of Singleton Pattern</h3><ol><li><strong>Global Access:</strong> With a Singleton, you can access its instance from anywhere in your codebase, making it convenient for managing global resources.</li><li><strong>Memory Efficiency:</strong> Singleton ensures that only one instance of the class is created, thus preventing unnecessary memory allocations.</li><li><strong>Lazy Initialization:</strong> The instance is created when it’s first requested, rather than at application startup, saving resources until needed.</li><li><strong>Thread Safety:</strong> Properly implemented, Singletons can provide thread safety when accessed from multiple threads.</li></ol><h3>Disadvantages of Singleton Pattern</h3><ol><li><strong>Global State:</strong> While global access is advantageous, it can also lead to overuse and tightly coupled code, making it harder to maintain and test.</li><li><strong>Difficulty in Testing:</strong> Dependencies on Singletons can make unit testing more challenging, as Singletons introduce hidden dependencies.</li><li><strong>Potential for Abuse:</strong> Developers may abuse Singletons, leading to code that is difficult to understand and maintain.</li></ol><h3>Implementation in Unity</h3><p>Let’s take a look at a simple implementation of a Singleton in Unity using C#:</p><pre>using UnityEngine;<br><br>public abstract class MySingleton&lt;T&gt; : MonoBehaviour where T : MonoBehaviour<br>{<br>    public static T Instance { get; private set; }<br><br>    protected virtual void Awake()<br>    {<br>        if (Instance == null)<br>        {<br>            Instance = this as T;<br>        }<br>        else<br>        {<br>            Debug.Log($&quot;There is an instance of the {typeof(T).FullName}&quot;);<br><br>            if (Instance.gameObject) Destroy(Instance.gameObject);<br><br>            Instance = this as T;<br>        }<br>    }<br>}</pre><p>Suppose you have a GameManager that controls the flow of your game. You want it to be a Singleton so that it’s easily accessible from any script. Here’s how you would implement it:</p><pre>using UnityEngine;<br><br>public class GameManager : MySingleton&lt;GameManager&gt;<br>{<br>    // Additional GameManager functionality can be added here<br>}</pre><p>Now, you can access the GameManager instance from anywhere in your code using <strong>‘GameManager.Instance’</strong>.</p><p>In conclusion, while the Singleton pattern comes with its advantages and disadvantages, when used carefully, it can greatly enhance the organization and efficiency of your Unity projects. Just remember to wield its power responsibly and avoid over-reliance on global state.</p><p>I hope you found this guide helpful and that it enhances your game development journey. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=adecd03af83c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generic Finite State Machine in Unity]]></title>
            <link>https://medium.com/@ibraslnn/generic-finite-state-machine-in-unity-a3442dfe3351?source=rss-e38885de7abf------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3442dfe3351</guid>
            <category><![CDATA[unity-game-development]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Ibrahim Aslan]]></dc:creator>
            <pubDate>Tue, 12 Mar 2024 19:28:21 GMT</pubDate>
            <atom:updated>2024-03-13T23:52:02.836Z</atom:updated>
            <content:encoded><![CDATA[<p>In game development, managing the behavior of game entities, like players, enemies, and NPCs, can be a challenging task. One effective way to tackle this challenge is by using a state machine. In this blog post, we’ll explore the importance of state machines in Unity and how they can be used to create robust and flexible player behavior, with a focus on the generic nature of state machines.</p><h3>What is a State Machine?</h3><p>A state machine is a programming concept commonly used in game development to control the behavior of game entities by organizing their actions into distinct states. Each state represents a specific behavior or condition of the entity, and transitions between states define how the entity responds to different stimuli.</p><h3>Why Use a State Machine?</h3><p>State machines offer several advantages for managing player behavior in Unity:</p><ol><li><strong>Modular Code:</strong> State machines allow developers to break down complex player behaviors into smaller, manageable components, making it easier to understand and maintain the codebase.</li><li><strong>Clear Logic Flow:</strong> With a state machine, the logic flow of player behavior becomes clear and structured, making it easier to debug and modify.</li><li><strong>Flexibility:</strong> State machines provide flexibility in handling different scenarios and conditions in the game, allowing for easy modification and extension of player behavior.</li><li><strong>Scalability:</strong> As the game grows in complexity, state machines scale well, allowing developers to add new states or modify existing ones without rewriting large portions of code.</li></ol><h3>Using Generic State Machines:</h3><p>One powerful aspect of state machines in Unity is their ability to be generic. By making use of generics, state machines can be customized to work with any type of game entity and data object. In our example, we’ve utilized a generic state machine to manage player behavior. This means that the state machine is not tied to any specific type of entity or data object, allowing for greater flexibility and reusability across different game projects.</p><p>The most essential scripts for a state machine are <strong>State </strong>and <strong>State Machine</strong>. Let’s start with these scripts:</p><p>The <strong>State </strong>script defines the base class for states in the state machine. It contains methods for entering and exiting states, performing checks, and updating logic and physics.</p><pre>public abstract class State&lt;T1, T2&gt; where T1 : MonoBehaviour where T2 : ScriptableObject <br>{<br>    protected T1 obj;<br>    protected T2 objData;<br>    protected StateMachine&lt;T1, T2&gt; stateMachine;<br>    protected float startingTime;<br>    protected bool isAnimationFinished;<br>    protected readonly string animBoolName;<br><br>    public State(T1 obj, StateMachine&lt;T1, T2&gt; stateMachine, T2 objData, string animBoolName = null)<br>    {<br>        this.obj = obj;<br>        this.stateMachine = stateMachine;<br>        this.objData = objData;<br>        this.animBoolName = animBoolName;<br>    }<br><br>    public virtual void Enter()<br>    {<br>        startingTime = Time.time;<br>        obj.Anim.SetBool(animBoolName, true);<br>        isAnimationFinished = false;<br>        DoChecks();<br>    }<br>    <br>    public virtual void Exit()<br>    {<br>        obj.Anim.SetBool(animBoolName, false);<br>    }<br>    <br>    public virtual void DoChecks()<br>    {<br>        <br>    }<br>    <br>    public virtual void LogicUpdate()<br>    {<br>        <br>    }<br>    <br>    public virtual void PhysicsUpdate()<br>    {<br>        DoChecks();<br>    }<br><br>    public void AnimationFinishTrigger() =&gt; isAnimationFinished = true;<br>}</pre><p>In our example (you will see below), each player state (e.g., idle, movement, rolling) inherits from the <strong>State </strong>class and implements its specific behavior and logic. This modular approach allows for easy management and customization of player behavior within the state machine.</p><p>The <strong>StateMachine </strong>script manages the current state of the player entity and handles state transitions. It initializes the starting state and allows for changing states during gameplay.</p><pre>public class StateMachine&lt;T1, T2&gt; where T1 : MonoBehaviour where T2 : ScriptableObject<br>{<br>    public Action OnStateChanged;<br>    public State&lt;T1, T2&gt; CurrentState { get; private set; }<br>    public State&lt;T1, T2&gt; PreviousState { get; private set; }<br><br>    public void Initialize(State&lt;T1, T2&gt; startingState)<br>    {<br>        CurrentState = startingState;<br>        CurrentState.Enter();<br>        OnStateChanged?.Invoke();<br>    }<br>    <br>    public void ChangeState(State&lt;T1, T2&gt; newState)<br>    {<br>        PreviousState = CurrentState;<br>        CurrentState.Exit();<br>        CurrentState = newState;<br>        CurrentState.Enter();<br>        OnStateChanged?.Invoke();<br>    }<br>}</pre><p>In our example, the <strong>StateMachine </strong>instance is associated with the player entity and controls its behavior based on the current state. Transitions between states occur in response to player input and game conditions, providing a seamless and dynamic gameplay experience.</p><h3>Example Usage:</h3><p>In this example, we are going to consider the main entity of every game, Player.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*HrvrkCOp4QbnV5VhtK-J0g.png" /><figcaption>A diagram that represents the state machine system.</figcaption></figure><p>Assume that, we have a base class called <strong>EntityState </strong>for the entity states in our game, e.g. Player, Enemy. This script inherits from the base <strong>State </strong>class, and the states of the player will be inherited from this class.</p><pre>public class EntityState&lt;T1, T2&gt; : State&lt;T1, T2&gt; where T1: Entity&lt;T2&gt; where T2: EntityData<br>{<br>    public EntityState(T1 obj, StateMachine&lt;T1, T2&gt; stateMachine,T2 objData, string animBoolName) : base(obj, stateMachine, objData, animBoolName)<br>    {<br>    }<br>    <br>    public override void Enter()<br>    {<br>        base.Enter();<br>    }<br><br>    public override void Exit()<br>    {<br>        base.Exit();<br>    }<br><br>    public override void LogicUpdate()<br>    {<br>        base.LogicUpdate();<br>    }<br><br>    public override void PhysicsUpdate()<br>    {<br>        base.PhysicsUpdate();<br>    }<br><br>    public override void DoChecks()<br>    {<br>        base.DoChecks();<br>    }<br>}</pre><p>We also have <strong>Entity </strong>and <strong>EntityData </strong>scripts to populate generic values of the states. And <strong>Player </strong>and <strong>PlayerData </strong>scripts inherits from these classes.</p><pre>public abstract class Entity&lt;T&gt; : MonoBehaviour where T : EntityData<br>{<br>    #region Components<br>    public Animator Anim { get; private set; }<br>    public Rigidbody2D Rb { get; private set; }<br>    #endregion<br>    <br>    public T Data;<br>    <br>    protected virtual void Awake()<br>    {<br>        Anim = GetComponent&lt;Animator&gt;();<br>        Rb = GetComponent&lt;Rigidbody2D&gt;();<br>    }<br><br>    protected virtual void Start() { }<br><br>    protected virtual void Update() { }<br>}</pre><pre>public class EntityData : ScriptableObject<br>{<br>    [Header(&quot;MOVEMENT&quot;)]<br>    public float MoveSpeed;<br>}</pre><p>And it is time for the Player. We have a main script called <strong>Player </strong>to control the game object and the states.</p><pre>public class Player : Entity&lt;PlayerData&gt;<br>{<br>    #region State Machine<br>    public StateMachine&lt;Player, PlayerData&gt; StateMachine { get; private set; }<br>    public PlayerIdleState IdleState { get; private set; }<br>    public PlayerMoveState MoveState { get; private set; }<br>    public PlayerRollState RollState { get; private set; }<br>    public PlayerDeathState DeathState { get; private set; }<br>    #endregion<br><br>    #region Components<br>    public PlayerInputHandler InputHandler { get; private set; }<br>    #endregion<br><br>    #region Built-In Functions<br>    protected override void Awake()<br>    {<br>        base.Awake();<br>        InputHandler = GetComponent&lt;PlayerInputHandler&gt;();<br>        StateMachine = new();<br><br>        IdleState = new PlayerIdleState(this, StateMachine, Data, &quot;idle&quot;);<br>        MoveState = new PlayerMoveState(this, StateMachine, Data, &quot;move&quot;);<br>        RollState = new PlayerRollState(this, StateMachine, Data, &quot;roll&quot;);<br>        DeathState = new PlayerDeathState(this, StateMachine, Data, &quot;death&quot;);<br>    }<br><br>    protected override void Start()<br>    {<br>        base.Start();<br>        StateMachine.Initialize(IdleState);<br>    }<br><br>    protected override void Update()<br>    {<br>        base.Update();<br>        StateMachine.CurrentState.LogicUpdate();<br>    }<br><br>    private void FixedUpdate()<br>    {<br>        StateMachine.CurrentState.PhysicsUpdate();<br>    }<br>    #endregion<br>}</pre><p>And <strong>PlayerData </strong>to define data objects for the player. This script contains properties like RollSpeed, RollTime, etc…</p><pre>[CreateAssetMenu(fileName = &quot;PlayerData_&quot;, menuName = &quot;Data/Entity/Player Data&quot;, order = 0)]<br>public class PlayerData : EntityData<br>{<br>    [Space(7)] <br>    [Header(&quot;ROLL&quot;)]<br>    public float RollSpeed;<br>    public float RollTime;<br>}</pre><p>Let’s check some states of the player. As I mentioned, the states of the player will be inherited from <strong>EntityState</strong>. Here is a main state from the Player, <strong>PlayerMovementState</strong>. It is a main state for the <em>idling </em>and <em>moving</em>.</p><pre>public class PlayerMovementState : EntityState&lt;Player, PlayerData&gt;<br>{<br>    protected Vector2 movementInput;<br>    <br>    public PlayerMovementState(Player obj, StateMachine&lt;Player, PlayerData&gt; stateMachine, PlayerData objData, string animBoolName) : base(obj, stateMachine, objData, animBoolName)<br>    {<br>    }<br><br>    public override void Enter()<br>    {<br>        base.Enter();<br>    }<br><br>    public override void Exit()<br>    {<br>        base.Exit();<br>    }<br><br>    public override void LogicUpdate()<br>    {<br>        base.LogicUpdate();<br>        movementInput = obj.InputHandler.MovementInput;<br>    }<br><br>    public override void PhysicsUpdate()<br>    {<br>        base.PhysicsUpdate();<br>    }<br>}</pre><p>And finally, <strong>PlayerIdleState </strong>to control the player when it doesn’t move.</p><pre>public class PlayerIdleState : PlayerMovementState<br>{<br>    public PlayerIdleState(Player obj, StateMachine.StateMachine&lt;Player, PlayerData&gt; stateMachine, PlayerData objData, string animBoolName) : base(obj, stateMachine, objData, animBoolName)<br>    {<br>    }<br><br>    public override void Enter()<br>    {<br>        base.Enter();<br>        obj.Rb.SetVelocityZero();<br>    }<br><br>    public override void Exit()<br>    {<br>        base.Exit();<br>    }<br><br>    public override void LogicUpdate() <br>    {<br>        base.LogicUpdate();<br>        <br>        // Transition to move state if movement input is detected<br>        if (movementInput.x != 0 || movementInput.y != 0)<br>        {<br>            stateMachine.ChangeState(obj.MoveState);<br>        }<br>    }<br>}</pre><p>This state inherits from <strong>PlayerMovementState</strong>, and use the <em>movementInput </em>variable of the <strong>PlayerMovementState </strong>to change state.</p><h3>Conclusion:</h3><p>In conclusion, state machines are invaluable tools for managing player behavior in Unity. By organizing player actions into distinct states and defining transitions between states, developers can create robust, flexible, and scalable player behavior systems. Whether you’re creating a simple 2D platformer or a complex open-world RPG, mastering state machines is essential for crafting engaging and immersive gameplay experiences in Unity.</p><p>So, next time you’re designing player behavior in Unity, remember to harness the power of generic state machines for smoother, more dynamic gameplay!</p><p>As you continue your journey in game development, keep experimenting with state machines and exploring new ways to leverage their capabilities. By mastering this fundamental concept, you’ll unlock endless possibilities for creating immersive and enjoyable player experiences in your Unity games.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3442dfe3351" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Most Useful Packages for Unity: NaughtyAttributes]]></title>
            <link>https://medium.com/@ibraslnn/most-useful-packages-for-unity-naughtyattributes-0f69263007a5?source=rss-e38885de7abf------2</link>
            <guid isPermaLink="false">https://medium.com/p/0f69263007a5</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <dc:creator><![CDATA[Ibrahim Aslan]]></dc:creator>
            <pubDate>Mon, 12 Feb 2024 17:47:14 GMT</pubDate>
            <atom:updated>2024-02-12T18:53:24.077Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*k3ZY-zaqYfthNVHGYqucIw.png" /></figure><p>In the dynamic landscape of Unity game development, developers constantly seek ways to optimize their workflows and enhance code readability. <strong>NaughtyAttributes</strong>, a powerful extension for Unity, offers a plethora of tools to achieve precisely that. In this blog post, we’ll dive into the world of <strong>NaughtyAttributes</strong>, exploring its diverse range of features and providing practical examples to illustrate their applications.</p><h3>1. Button</h3><p>Display a button in the Inspector to execute a method.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [Button(&quot;Reset Game&quot;)]<br>    private void ResetGame()<br>    {<br>        // Reset game logic here<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*VO0kf4XbLwLfS6iGOJ2XxA.png" /></figure><h3>2. ReadOnly</h3><p>Make a field read-only in the Inspector.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [ReadOnly]<br>    public int score;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*e6Kwt128dxILFN-o6GZEaQ.png" /></figure><h3>3. ShowIf</h3><p>Conditionally show a field based on a boolean condition.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    public bool hasKey;<br>    <br>    [ShowIf(&quot;hasKey&quot;)]<br>    public GameObject door;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*efcAAkrYddPGp4c-AO6yMg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*77eZcuJeM5Rxd8_HEXuFNg.png" /></figure><h3>4. HideIf</h3><p>Conditionally hide a field based on a boolean condition.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    public bool isWalking;<br><br>    [HideIf(&quot;isWalking&quot;)]<br>    public GameObject vehicle;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*nVM1RgvW1mCqC0uGgk-jXQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*irCAkcxpw175UtB2ATpT_w.png" /></figure><h3>5. MinMaxSlider</h3><p>Provide a slider in the Inspector to select a value within a specified range.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [MinMaxSlider(0, 100)]<br>    public Vector2 speedRange;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*H4KXMs3GNYW6irspQ5_73w.png" /></figure><h3>6. Required</h3><p>Mark a field as required in the Inspector, preventing null assignments.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [Required]<br>    public Transform target;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*zj2B9ScbFLHgJHto8IUlSg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*yydHE77q2zb8Ftd0KzAmsA.png" /></figure><h3>7. Dropdown</h3><p>Display a dropdown menu in the Inspector to select from predefined options.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [Dropdown(&quot;difficultyOptions&quot;)]<br>    public string difficulty;<br>    private string[] difficultyOptions = { &quot;Easy&quot;, &quot;Medium&quot;, &quot;Hard&quot; };<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*d_Jb50PeDVctVjKollyh5Q.png" /></figure><h3>8. BoxGroup</h3><p>Group fields within a box in the Inspector for better organization.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [BoxGroup(&quot;Movement Settings&quot;)]<br>    public float speed;<br>    [BoxGroup(&quot;Movement Settings&quot;)]<br>    public float jumpForce;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*jmnSbJly9eKaVsIU1gjBCA.png" /></figure><h3>9. Tag</h3><p>Allows selection of tags directly from the Inspector.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [Tag]<br>    public string playerTag;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*BXHS4EZLgILwxhJ4K4kCjA.png" /></figure><h3>10. ShowAssetPreview</h3><p>Displays a preview of assets (such as textures or models) in the Inspector.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [ShowAssetPreview]<br>    public Sprite itemIcon;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*W-0GYn95FT4Hv5gyWPP6XA.png" /></figure><h3>11. ValidateInput</h3><p>Validates the input value entered in the Inspector using a custom method.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [ValidateInput(&quot;ValidateHealth&quot;)]<br>    public int health;<br>    private bool ValidateHealth(int value)<br>    {<br>        return value &gt;= 0; // Health cannot be negative<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*buyrBkx_I5nDolkos465mg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*6D9bAHRrgC5d1cyogAysoQ.png" /></figure><h3>12. HorizontalLine</h3><p>Draws a horizontal line. You can adjust the height (thickness) and color of the line.</p><pre>using UnityEngine;<br>using NaughtyAttributes;<br><br>public class NaughtyTest : MonoBehaviour<br>{<br>    [HorizontalLine(color: EColor.Yellow, height: 5)]<br>    public string saturn;<br><br>    [HorizontalLine(color: EColor.Red, height: 10)]<br>    public string mars;<br><br>    [HorizontalLine(color: EColor.Blue, height: 15)]<br>    public string neptune;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*eC9fSxv9K5Z7saCcT5Vcog.png" /></figure><p><strong>NaughtyAttributes </strong>provides Unity developers with a powerful arsenal of tools to streamline their development process and enhance code readability. From simplifying the Inspector interface with buttons and sliders to conditional visibility of fields, <strong>NaughtyAttributes </strong>empowers developers to create more intuitive and efficient workflows. By incorporating <strong>NaughtyAttributes </strong>into your Unity projects, you can elevate your game development experience and unlock new levels of productivity and creativity.</p><p>You can download it via this link:</p><p><a href="https://assetstore.unity.com/packages/tools/utilities/naughtyattributes-129996">NaughtyAttributes | Utilities Tools | Unity Asset Store</a></p><p>And here is the official documentation of the package:</p><p><a href="https://dbrizov.github.io/na-docs/index.html">NaughtyAttributes&#39; Docs - NaughtyAttributes 2.1.4 documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0f69263007a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>