Unity-Design-Pattern
:tea: All Gang of Four Design Patterns written in Unity C# with many examples. And some Game Programming Patterns written in Unity C#. | 各种设计模式的Unity3D C#版本实现
Top Related Projects
Implementations of programming design patterns in Unity with examples in C# when to use them.
Examples of programming design patterns in Unity C#
An ultra-simplified explanation to design patterns
Attribute Extensions for Unity
Quick Overview
QianMo/Unity-Design-Pattern is a comprehensive collection of design patterns implemented in Unity and C#. It serves as an educational resource for game developers and programmers to learn and apply various design patterns in Unity game development. The repository includes detailed explanations, diagrams, and code examples for each pattern.
Pros
- Extensive coverage of design patterns specific to Unity development
- Well-organized structure with clear explanations and visual aids
- Practical code examples that demonstrate pattern implementation in Unity
- Regular updates and contributions from the community
Cons
- Some patterns may be overly complex for beginners
- Limited documentation in languages other than Chinese
- Lack of advanced use cases for some patterns
- Some code examples may need updating for the latest Unity versions
Code Examples
- Singleton Pattern:
public class Singleton<T> : MonoBehaviour where T : Component
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T>();
if (_instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).Name;
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
}
This code implements a generic Singleton pattern for Unity components.
- Observer Pattern:
public class Subject : MonoBehaviour
{
private List<IObserver> observers = new List<IObserver>();
public void AddObserver(IObserver observer)
{
observers.Add(observer);
}
public void RemoveObserver(IObserver observer)
{
observers.Remove(observer);
}
protected void NotifyObservers()
{
foreach (var observer in observers)
{
observer.OnNotify();
}
}
}
This code demonstrates the Subject class in the Observer pattern, managing a list of observers and notifying them.
- Factory Method Pattern:
public abstract class EnemyFactory : MonoBehaviour
{
public abstract Enemy CreateEnemy();
}
public class ZombieFactory : EnemyFactory
{
public override Enemy CreateEnemy()
{
return new Zombie();
}
}
This code shows an implementation of the Factory Method pattern for creating different types of enemies in a game.
Getting Started
To use these design patterns in your Unity project:
- Clone the repository:
git clone https://github.com/QianMo/Unity-Design-Pattern.git
- Copy the desired pattern scripts into your Unity project's Assets folder.
- Implement the pattern in your game logic. For example, to use the Singleton pattern:
public class GameManager : Singleton<GameManager>
{
// Your game manager logic here
}
// Usage in other scripts
GameManager.Instance.SomeMethod();
- Refer to the repository's documentation for detailed explanations and best practices for each pattern.
Competitor Comparisons
Implementations of programming design patterns in Unity with examples in C# when to use them.
Pros of Unity-Programming-Patterns
- More comprehensive coverage of design patterns, including structural and behavioral patterns
- Includes practical examples and use cases for each pattern in Unity
- Well-organized structure with clear explanations and comments
Cons of Unity-Programming-Patterns
- Less focus on Unity-specific implementations compared to Unity-Design-Pattern
- Fewer visual aids and diagrams to illustrate pattern concepts
- Some examples may be simpler or less game-specific than Unity-Design-Pattern
Code Comparison
Unity-Programming-Patterns (Observer Pattern):
public class Subject : MonoBehaviour
{
private List<IObserver> observers = new List<IObserver>();
public void AddObserver(IObserver observer)
{
observers.Add(observer);
}
}
Unity-Design-Pattern (Observer Pattern):
public abstract class Subject
{
private List<Observer> observers = new List<Observer>();
public void Attach(Observer observer)
{
observers.Add(observer);
}
}
Both repositories provide valuable resources for learning and implementing design patterns in Unity. Unity-Programming-Patterns offers a broader range of patterns and more detailed explanations, while Unity-Design-Pattern focuses more on Unity-specific implementations and visual representations. The choice between the two depends on the developer's preference for depth vs. Unity-centric examples.
Examples of programming design patterns in Unity C#
Pros of unity-design-patterns
- More comprehensive coverage of design patterns, including creational, structural, and behavioral patterns
- Includes practical Unity-specific examples for each pattern
- Active development with recent updates and contributions
Cons of unity-design-patterns
- Less detailed explanations and documentation compared to Unity-Design-Pattern
- Fewer stars and forks on GitHub, potentially indicating less community engagement
- Some examples may be more complex or harder to understand for beginners
Code Comparison
Unity-Design-Pattern:
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<T>();
if (_instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).Name;
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
}
unity-design-patterns:
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = (T)FindObjectOfType(typeof(T));
if (instance == null)
{
Debug.LogError("An instance of " + typeof(T) + " is needed in the scene, but there is none.");
}
}
return instance;
}
}
}
An ultra-simplified explanation to design patterns
Pros of design-patterns-for-humans
- Written in plain English, making it more accessible to beginners
- Covers a wide range of design patterns with real-world examples
- Language-agnostic, applicable to various programming languages
Cons of design-patterns-for-humans
- Lacks specific Unity implementation examples
- May not cover Unity-specific design patterns or best practices
- Less detailed explanations of pattern implementations
Code Comparison
design-patterns-for-humans (PHP example):
interface Lion
{
public function roar();
}
class AfricanLion implements Lion
{
public function roar()
{
// Implementation
}
}
Unity-Design-Pattern (C# example):
public abstract class Character
{
public abstract void Attack();
}
public class Warrior : Character
{
public override void Attack()
{
// Implementation
}
}
Both repositories provide valuable insights into design patterns, but Unity-Design-Pattern focuses specifically on Unity game development, offering practical examples and implementations tailored for Unity projects. design-patterns-for-humans, on the other hand, provides a more general approach to design patterns, making it suitable for a broader audience across different programming languages and domains.
Attribute Extensions for Unity
Pros of NaughtyAttributes
- Focuses on enhancing Unity Inspector functionality with attribute-based extensions
- Provides a wide range of attributes for customizing inspector appearance and behavior
- Lightweight and easy to integrate into existing Unity projects
Cons of NaughtyAttributes
- Limited scope compared to Unity-Design-Pattern's comprehensive design pattern examples
- May require additional setup for complex inspector customizations
- Less educational value for learning software design patterns
Code Comparison
NaughtyAttributes:
public class Example : MonoBehaviour
{
[ShowAssetPreview]
public Texture2D texture;
[Button("Do Something")]
private void DoSomething() { }
}
Unity-Design-Pattern:
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
public static T Instance => _instance;
protected virtual void Awake() { /* ... */ }
}
NaughtyAttributes focuses on attribute-based inspector enhancements, while Unity-Design-Pattern demonstrates various design patterns implementation in Unity. The former improves workflow and inspector usability, while the latter provides a broader understanding of software architecture in game development.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Design Patterns Written in Unity3D
This repository is about cool design patterns written in Unity3D C#.
- Now 23 Gang of Four Patterns have all been finished in Unity3D in this repository.
- Each pattern contains the corresponding structure implementations, application examples and diagrams. Same way with Naphier/unity-design-patterns, in this repository each pattern is contained in a separate folder. Inside these are a folder ("Structure") to show what classes are used in the pattern's structure in Unity3D(with a scene) and a folder or folders ("Example") showing one or more real-world example of using the pattern in Unity3D along with a scene showing it in action. Each pattern folder may contain one or more Example.
- Game design patterns from book Game Programming Patterns have been partially implemented.
æ¤repo为Unity3Dä¸åç§è®¾è®¡æ¨¡å¼çå®è·µä¸è¿ç¨ã
- ç®åå·²ç»å¨Unityä¸å®ç°äºã设计模å¼:å¯å¤ç¨é¢å对象软件çåºç¡ãä¸ä¹¦ä¸æåºç23ç§è®¾è®¡æ¨¡å¼ã
- æ¯ç§æ¨¡å¼é½å å«å¯¹åºçç»æå®ç°ãåºç¨ç¤ºä¾ä»¥åå¾ç¤ºä»ç»ã类似Naphier/unity-design-patternsçç»æï¼æ¤repoä¸çæ¯ç§æ¨¡å¼ç¨åç¬çæ件夹åå¼ãæ¯ç§æ¨¡å¼å¯¹åºçæ件夹ä¸å å«äºå为âStructureâçåæ件夹ï¼éé¢åæ¾çæ¯æ¤æ¨¡å¼å¨Unityä¸ç使ç¨ä»£ç çåºæ¬æ¡æ¶å®ç°ï¼èå¦å¤å å«çExampleåæ件夹ä¸åæ¾çæ¯æ¤æ¨¡å¼å¨Unityä¸ä½¿ç¨çå®é 示ä¾ãæ¯ç§æ¡æ¶å®ç°æå®ä¾ç¤ºä¾å®ç°é½å å«å¯¹åºçåºæ¯ï¼æ¯ç§æ¨¡å¼æ件夹ä¸å¯è½å å«ä¸ä¸ªæè å¤ä¸ªExampleã
- ã游æç¼ç¨æ¨¡å¼ãä¸ä¹¦ä¸ä»ç»ç常ç¨æ¸¸æ设计模å¼çUnityçå®ç°ä¹æé¨åå®ç°ã
Contents
IãGang of Four Patterns in Unity ï¼23ç§GOF设计模å¼çUnityå®ç°ï¼
Behavioral Patterns è¡ä¸ºå模å¼
- Command Pattern å½ä»¤æ¨¡å¼
- State Pattern ç¶æ模å¼
- Observer Pattern è§å¯è 模å¼
- Chain of Responsibility Pattern 责任é¾æ¨¡å¼
- Mediator Pattern ä¸ä»è 模å¼
- Interpreter Pattern 解éå¨æ¨¡å¼
- Iterator Pattern è¿ä»£å¨æ¨¡å¼
- Memento Pattern å¤å¿å½æ¨¡å¼
- Strategy Pattern çç¥æ¨¡å¼
- Template Method Pattern 模æ¿æ¹æ³æ¨¡å¼
- Visitor Pattern 访é®è 模å¼
Structural Patterns ç»æå模å¼
- Adapter Pattern éé å¨æ¨¡å¼
- Bridge Pattern æ¡¥æ¥æ¨¡å¼
- Composite Pattern ç»å模å¼
- Decorator Pattern è£ é¥°æ¨¡å¼
- Facade Pattern å¤è§æ¨¡å¼
- Flyweight Pattern 享å 模å¼
- Proxy Pattern 代ç模å¼
Creational Patterns å建å模å¼
- Prototype Pattern åå模å¼
- Singleton Pattern åä¾æ¨¡å¼
- Abstract Factory Pattern æ½è±¡å·¥å模å¼
- Builder Pattern 建é è 模å¼
- Factory Method Pattern å·¥åæ¹æ³æ¨¡å¼
IIãGame Programming Patterns in Unity ï¼ã游æç¼ç¨æ¨¡å¼ãçUnityå®ç°ï¼
- Subclass Sandbox Pattern åç±»æ²ç模å¼
- Type Object Pattern ç±»å对象模å¼
- Component Pattern ç»ä»¶æ¨¡å¼
- Event Queue Pattern äºä»¶éå模å¼
- Game Loop Pattern 游æ循ç¯æ¨¡å¼
- Service Locator Pattern æå¡å®ä½å¨æ¨¡å¼
- Data Locality Pattern æ°æ®å±é¨æ§æ¨¡å¼
- Dirty Flag Pattern èæ 记模å¼
- Object Pool Pattern å¯¹è±¡æ± æ¨¡å¼
Reference resources(åè)
- http://gameprogrammingpatterns.com/
- https://www.youtube.com/playlist?list=PLF206E906175C7E07
- https://github.com/Naphier/unity-design-patterns
- http://www.dofactory.com/net/design-patterns
- https://sourcemaking.com/design_patterns
- http://www.habrador.com/tutorials/programming-patterns/
- Gang of Four Patterns
- Head First Design Patterns
- 设计模å¼ä¸æ¸¸æå®ç¾å¼å
Top Related Projects
Implementations of programming design patterns in Unity with examples in C# when to use them.
Examples of programming design patterns in Unity C#
An ultra-simplified explanation to design patterns
Attribute Extensions for Unity
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot