Skip to content

These are some set of tools that I have created to smoothen my journey of game development with unity 3D. Maybe I'll add more in future as the need arise. Read below the names and usages of tools currently present in this package.

License

Notifications You must be signed in to change notification settings

FahimKamal/Unity_FK-Toolbox_Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 

Repository files navigation

FK-Toolbox

These are some set of tools that I have created to smoothen my journey of game development with unity 3D. Maybe I'll add more in future as the need arise. Read below the names and usages of tools currently present in this package.

Dependencies

This package has below dependencies. You have to install them first.

  • Tri-Inspector

    https://github.com/codewriter-packages/Tri-Inspector.git
  • Serialized Dictionary by ayellowpaper

    https://assetstore.unity.com/packages/tools/utilities/serialized-dictionary-243052

    *Already added in the project, no farther action needed.

Installation

Installation process for this package and all dependencies that are from github is same and written below.

  • Copy below URL:

    https://github.com/FahimKamal/Unity_FK-Toolbox.git#package
  • Open your Package Manager from Unity editor.

  • Press the + icon on the top left corner.

  • Select Add Package from git URL... option.

  • Paste the URL in the textbox and press Add button.

  • Custom Attributes

    Expand
    • [ShowIf] Attribute

      I have taken this solution from a StackOverFlow answer. The link to the question is: here

      Usage
      • Using a field to hide/show another field:
      public bool showHideList = false; 
      [ShowIf(ActionOnConditionFail.DontDraw, ConditionOperator.And, nameof(showHideList))]
      public string aField = "item 1";

      hide/show a field

      • Using a field to enable/disable another field:
      public bool enableDisableList = false;
      
      [ShowIf(ActionOnConditionFail.JustDisable, ConditionOperator.And, 
      nameof(enableDisableList))]
      public string anotherField = "item 2";

      Enable/Disable a field

      • Using multiple conditions on the same field:
      public bool condition1;    
      public bool condition2;    
      [ShowIf(ActionOnConditionFail.JustDisable, ConditionOperator.And, nameof(condition1), 
      nameof(condition2))]    
      public string oneLastField= "last field";

      hide/show a field

      • Using a method to get a condition value:
      [ShowIf(ActionOnConditionFail.JustDisable, ConditionOperator.And,nameof(CalculateIsEnabled))]
      public string yetAnotherField = "one more";    
      public bool CalculateIsEnabled()    
      {
          return true;    
      }

      Using a method to get a condition value

    • [RequireReference] Attribute

      There are certain fields in your scripts like GameObject, Transform, Prefab that can't be null. Otherwise it will throw an error while running the game. In that places you can add this attribute to give you an warning, to set that fields with appropriate object reference.

      Usage
      • Add the attribute like below example.

        [RequireReference]
        [SerializeField] private PopupEvent popupEvent;

        You will see something like this in inspector.

      • (Optional) You can also add you own warning text.

        [RequireReference("You must set this reference. Otherwise script will crush.")]
        [SerializeField] private PopupEvent popupEvent;

        You will see something like this in inspector.

    • [ShowOnlyFK] Attribute

      This attribute will let you make a serialized filed read only in inspector. You can see the values but can't edit them in inspector. Can be applied on all serializable fields.
      But you will be able to change them through code. This Attribute was created by taking help from unity forum. Visit the question and answer here.

      Usage
      • Add the attribute on your desired serializeField like below.

         public class DemoClass : MonoBehaviour
         {
              [SerializeField, ShowOnlyFK] private int intField = 12;
              [SerializeField, ShowOnlyFK] private string stringField = "You can only see me";
              [SerializeField, ShowOnlyFK] private List<GameObject> allSpriteRendererObjs = new List<GameObject>();
         }

        You will see something like this in inspector.

  • Event System with Scriptable Object

    This is a flexible and easy-to-use event system for Unity that allows you to define events with custom data types. It decouples event logic from game objects and scripts, making it easier to maintain and refactor code. It includes a base event class and a void event class, and events can be raised by calling the RaiseEvent method. Several example classes and a demo scene are included to demonstrate the system's functionality.

    Expand

    You will find some build-in type of events that you can use for your different use case.

    • Void Event : You can raise this event for your specific events and all other scripts that has subscribed to this event will listen and execute their specific tasks. No data will be passed on.
    • Int Event : Will work same as Void Event only you will be able to passed on a int value.
    • String Event : Will work same as Void Event only you will be able to passed on a string value.
    • Custom Event : Will work same as Void Event but with more custom data type. by extending the BaseEvent<T> class you can passed on other data types even custom data class. See use case section to understand how to do that.
    Usage
    • [Void Event]

      • Initialization:

        • Right Click in your Project Window and select.
          Create -> Events -> Void Event. Give it a name and save it.
        • In your Broadcaster Script: Write these lines to reference the event and drag-n-drop the event from your assets folder.
         [RequireReference]
         [SerializeField] private VoidEvent damageEvent;
        • Now to raise the event write these lines of code:
         private void OnCollisionEnter2D(Collision2D col)
         {
            if (damageEvent != null)
            {
                damageEvent.RaiseEvent();
            }
         }
        • Now in your Listener Scripts for example your UI controller : Write these lines to reference the event and drag-n-drop the event from your assets folder.
        [RequireReference]
        [SerializeField] private VoidEvent damageEvent;
        
        ...
        
        private void OnEnable()
        {
          damageEvent.onEventRaised.AddListener(OnEventRaised);
        }
        
        private void OnDisable()
        {
          damageEvent.onEventRaised.RemoveListener(OnEventRaised);
        }
        
        private void OnEventRaised()
        {
          messageBox.text = "Player is collide with an enemy";
          ...
          // Other codes.
          ...
        }
        
        ...
        • Whatever you have in your OnEventRaised() method will be executed when the event is raised from the Broadcaster script.
    • [Int Event]

      • Initialization:

        • Right Click in your Project Window and select.
          Create -> Events -> Int Event. Give it a name and save it.
        • In your Broadcaster Script: Write these lines to reference the event and drag-n-drop the event from your assets folder.
         [RequireReference]
         [SerializeField] private IntEvent damageEvent;
        • Now to raise the event write these lines of code: Value of damageAmount will ge passed on as parameter.
         ...
         int damageAmount = 10;
         ... 
        
         private void OnCollisionEnter2D(Collision2D col)
         {
            if (damageEvent != null)
            {
                damageEvent.RaiseEvent(damageAmount);
            }
         }
        • Now in your Listener Scripts for example your UI controller : Write these lines to reference the event and drag-n-drop the event from your assets folder.
        [RequireReference]
        [SerializeField] private IntEvent damageEvent;
        
        ...
        
        private void OnEnable()
        {
          damageEvent.onEventRaised.AddListener(OnEventRaised);
        }
        
        private void OnDisable()
        {
          damageEvent.onEventRaised.RemoveListener(OnEventRaised);
        }
        
        private void OnEventRaised(int damageAmount)
        {
          messageBox.text = "Player took damage of" + damageAmount;
          ...
          // Other codes.
          ...
        }
        
        ...
        • In this case damageAmount will be carried here from Broadcaster and you can use the value as you need.
    • [Custom Event]

      • Initialization: Maybe you need to send some other data type like float or maybe some other custom data class. You can do that by extending BaseEvent<T> class.
      • Let's create a Event that will passed on float value. See below code:
      [CreateAssetMenu(menuName = "Events/Float Event")]
      public class FloatEvent : BaseEvent<float>
      {
      
      }
      • That's it. Now use it same way you would use Int Event.
      • Let's Create a Event that will passed on a data class. See below code:
      [CreateAssetMenu(menuName = "Events/Messenger Event")]
      public class PopupEvent : BaseEvent<Messenge>
      {
      }
        [Serializable]
        public class Messenge
        {
            public string description;
            public string title;
            public bool onlyLog;
      
            public Messenge(string description, string title, bool onlyLog)
            {
                this.description = description;
                this.title = title;
                this.onlyLog = onlyLog;
            }
        }
      • Above Event class has be used by the Popup Manager. It's that simple. You can use above event same way you would use IntEvent or FloatEvent.
  • Popup Manager

    Popup Manager is a small package that can be useful for debugging in mobile games. Its purpose is to help determine if a specific task or method is being called. It works similar to how we use the Debug.Log() method in the console.

    To use Popup Manager, simply add the Popup Manager Prefab to your scene. You can use it in three ways:

    • Show Popup notification in different situation.
    • Print log in log window.
    • Print log in your Unity console.

    Expand

    See below example to know how to use this. Also you will find a sample scene which will demonstrate of it's usage.

    Example

    Usage

    Initialization

    • Add the Popup Manager prefab into your scene.
    • Select the features that you want to use in your game.
    • Make sure Message Receiver Event is set. You will find that in resource folder.

      Initialization

    • Create a new variable like bellow, in your scripts where you want to call and show Popup/log.
      [RequireReference]
      [SerializeField] private PopupEvent popupEvent;
    • Set reference to PopupEvent from inspector. You will find that in resource folder.

      Initialization

    • Now each time you need to show popup or log text call below method from popupEvent.
      popupEvent.ShowPopup(description:"Button pressed from hello button", title:"Notification");
      popupEvent.ShowPopup("Game Closing.");
      popupEvent.ShowPopup("Data saved to cloud", onlyLog:true);
      • description: The message that you want to print in console/log and as popup body.
      • title:(Optional) The title for popup window.
      • onlyLog:(Optional) Set it to true if you only want to see it in console or log window in mobile device.

    Note: In your final build just un-check usePopup and useLogWindow option to remove popups and log screen from your game. No need to remove or comment-out any code.

    Note to self: For customize look of the Popup Manager in inspector. You have written some codes. Reference to those codes in future.
  • Save Manager

    Save Manager works same as the unity buildin PlayerPrefs. It has the same methods and more to work with. You can save int, float, bool and string data types. What is special about this package is that you can find all your saved data in one place as a json file in key-value pair format. Also you will be about to see the saved values in your inspector while in play mode as well as search by keys. Which is not possible with PlayerPrefs.

    Expand

    See below example to know how to use this.

    Example
    Inspector View

    Example
    While in play mode

    Usage

    Initialization

    • Add the Save Data Manager script with a empty game object in your game.
    • Keep in mind this is a singleton script. So, add this script at the first scene of your game.
    • Selecting Runtime Only option will let you save data only while in play mode. After exiting the all data will be lost. If not selected then data will be saved at Application.persistentDataPath location on you device as a .json file.

    To save data: Use below code:

    SaveData.SetBool("boolVal", value);  
    SaveData.SetInt("IntVal", mRandomInt);
    SaveData.SetFloat("FloatVal", mRandomFloat);
    SaveData.SetString("StringVal", mRandomString);

    To load data: Use below code:

    SaveData.GetBool("boolVal", value);  
    SaveData.GetInt("IntVal", mRandomInt);
    SaveData.GetFloat("FloatVal", mRandomFloat);
    SaveData.GetString("StringVal", mRandomString);

    Read the static SaveData class to find out all available method that you can use. You will find all method that is available to you with PlayerPrefs.

  • Haptic Feedback

    To give haptic feedback or vibration effect in you game you can use this static class.

    Expand
    • Simply use bellow code to give haptic feedback in you code.
      Vibrator.Vibrate(HapticEffect.High);
      // or
      Vibrator.Vibrate(HapticEffect.Medium);
      // or
      Vibrator.Vibrate(HapticEffect.Small);
      // or
      Vibrator.Vibrate(HapticEffect.Little);
      
      // You can also give custom duration in milliseconds
      Vibrator.Vibrate(250);
      Above code dosen't work with latest versions of Unity. Use below repo instead.
      https://github.com/CandyCoded/HapticFeedback
      
  • FloatReference and FloatVariable

    I have taken this from GDC Talk-2017 by Ryan Hipple. You can find it on Youtube. Watch it here.

    Usage
    • To use FloatReferance in your script write below code.
      [SerializeField] private FloatReference testFloat;
    • You will be give two option Use Constant or Use Variable.


      *Use Constant: You will be able to use it as any other float variable.
      *Use Variable: Value will be taken from a scriptable object of type FloatVariable.
      *To Create aFloatVariable object: In Project window Right-click and select Create -> FloatVariable.
  • Sprite Swap [Experimental]

    There are multiple scripts can be found in this section. These scripts will find gameObjects with spriteRenderer and Image component attached with it and will be able to swap sprites of it with new given sprite. They are specifically written for an internal project. Maybe will use it in future on maybe never. Help was taken from this unity forum post.
    These scripts are not added with package, you can only find them in the main branch.

    About

    These are some set of tools that I have created to smoothen my journey of game development with unity 3D. Maybe I'll add more in future as the need arise. Read below the names and usages of tools currently present in this package.

    Topics

    Resources

    License

    Stars

    Watchers

    Forks

    Packages

    No packages published