This project is built in Unity and demonstrates Object-Oriented Programming (OOP) principles using C# scripts. Each OOP concept (Encapsulation, Abstraction, Inheritance, and Polymorphism) is explained through practical examples in the Unity Editor, helping developers understand how to apply OOP in real-world Unity game development.
Definition: Encapsulation restricts direct access to data and ensures that it is modified through well-defined methods.
Example in Unity:
-
The Player class has private health variables with public methods to update them.
-
Prevents direct modifications and ensures controlled access.
public class Player : MonoBehaviour
{
private int health = 100;public void TakeDamage(int damage)
{
health -= damage;
Debug.Log("Player Health: " + health);
}
}
Definition: Abstraction hides complex details and provides a simple interface for interacting with objects.
Example in Unity:
-
The
Weaponclass defines an abstract methodAttack(), which is implemented differently inGunandSwordsubclasses.
public abstract class Weapon : MonoBehaviour
{
public abstract void Attack();
}public class Gun : Weapon
{
public override void Attack()
{
Debug.Log("Shooting with gun!");
}
}
public class Sword : Weapon
{
public override void Attack()
{
Debug.Log("Swinging sword!");
}
}
Definition: Inheritance allows a class to derive properties and behaviors from another class.
Example in Unity:
-
Enemyis a base class, andZombieandRobotinherit from it, reusing common properties like health and movement.
public class Enemy : MonoBehaviour
{
public int health = 100;public void TakeDamage(int damage)
{
health -= damage;
Debug.Log("Enemy Health: " + health);
}
}
public class Zombie : Enemy
{
void Start()
{
Debug.Log("Zombie spawned with " + health + " health.");
}
}
public class Robot : Enemy
{
void Start()
{
Debug.Log("Robot activated with " + health + " health.");
}
}
Definition: Polymorphism allows methods to be overridden or used interchangeably through a common interface.
Example in Unity:
-
The
Enemyclass has a virtual methodAttack(), which is overridden byZombieandRobotclasses.
public class Enemy : MonoBehaviour
{
public virtual void Attack()
{
Debug.Log("Enemy attacks!");
}
}public class Zombie : Enemy
{
public override void Attack()
{
Debug.Log("Zombie bites!");
}
}
public class Robot : Enemy
{
public override void Attack()
{
Debug.Log("Robot fires laser!");
}
}
-
Clone the Repository
shCopyEditgit clone https://github.com/yourusername/OOP-Unity-Game.git cd OOP-Unity-Game -
Open in Unity
-
Open Unity Hub and select "Add Project".
-
Choose the cloned folder.
-
-
Run the Scene
-
Press Play in Unity to see OOP principles in action.
-
-
Modify & Experiment
-
Try adding new enemy types, weapons, or extending behaviors using OOP concepts.
-
Feel free to fork this repository and contribute improvements! Submit a pull request if you have enhancements or new OOP examples.
This project is open-source under the MIT License.
🔹 GitHub: https://github.com/alokkrishali 🔹 Facebook: https://www.facebook.com/learngamestutorial 🔹 Youtube: https://www.youtube.com/@LearnGamesTutorial 🔹 Blog: Learn games tutorial
This README.md is SEO-optimized with relevant Unity, OOP, and C# keywords to help your GitHub project rank better in search results! 🚀 Let me know if you want any modifications. 😊