MIT License

项目介绍

heyJordanParker/yaSingleton 是一个托管在 GitHub 上的开源项目,主要使用 C# 编写,采用 MIT License 许可证,目前获得 162 个 Star、9 次 Fork。

仓库全名
heyJordanParker/yaSingleton
开源许可证
MIT License
Star 数
162
Fork 数
9
未关闭 Issue
1

README 摘要

yaSingleton Yet Another Singleton library for Unity and part of the Elarion Framework. It provides Singletons based on ScriptableObjects instead of the more conventionally used MonoBehaviours. Example Unity project can be found here. Why Why use yaSingleton and why use ScriptableObjects? No duplicate instances of your singletons across different scenes. Single point of entry that's guaranteed to run before any of…

完整内容见 GitHub 原始 README 或本页 。

常见问题

yaSingleton 用什么语言开发?

主要使用 C# 编写,采用 MIT License 许可证,目前有 162 个 Star。

yaSingleton 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 yaSingleton 的源码?

可以直接克隆仓库:git clone https://github.com/heyJordanParker/yaSingleton.git,默认分支为 master;安装与运行步骤见仓库 README。

yaSingleton 最近还在维护吗?

仓库最近一次更新为 2020年10月1日,当前有 1 个未关闭 Issue;Star 数为 162,可作为社区活跃度的参考。

关于

A singleton pattern implementation for Unity3d. Based on ScriptableObjects instead of the conventional MonoBehaviour approach.

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

语言占比

C#

README

yaSingleton

Yet Another Singleton library for Unity and part of the Elarion Framework. It provides Singletons based on ScriptableObjects instead of the more conventionally used MonoBehaviours.

Example Unity project can be found here.

Why

Why use yaSingleton and why use ScriptableObjects?

  • No duplicate instances of your singletons across different scenes.
  • Single point of entry that’s guaranteed to run before any of your Awake scripts.
  • Easy versioning. Since singletons are saved as ScriptableObjects, they’re outside the scenes and thus easy to version control.
  • Thread-safe LazySingleton.
  • Improved performance; all singletons share a single set of events (Update, LateUpdate, and FixedUpdate).
  • Easy to integrate with your existing code base.
  • Just works! No need to initialize anything or mess with the Resources folder.

Getting Started

Adding yaSingleton to your project can be as easy as downloading it and extracting it anywhere in your Assets Folder. Alternatively, for easier updates (and extra points), you can add it as a git submodule.

Adding yaSingleton as a git submodule

If you’re not familiar with git submodules, you can get started here.

Adding the submodule

I’ll describe a simple setup in which yaSingleton lives in the root directory of the project - modify it to suit your needs.

First, go to your project’s directory and run:

cd Assets
git submodule add https://github.com/jedybg/yaSingleton.git

This will clone the project in a the yaSingleton directory. You’ll need to commit the folder and the .gitmodules file (tracking all submodules) to your repository.

Updating the submodule

The great thing about submodules is that they’re simple repositories. You can easily go to your project’s directory and use git pull to update to the latest version.

cd Assets/yaSingleton
git pull # or any other git command

Just remember that this will generate a changeset in your repository which you’ll have to later commit.

Migration to yaSingleton

Migrating to yaSingleton is quite simple.

First, inherit the base class:

# public class UIManager : MonoBehaviour becomes
public class UIManager : Singleton<UIManager> 

Or if you want the singleton to be lazy-loaded:

public class UIManager : LazySingleton<UIManager>

Add the CreateAssetMenuAttribute:

[CreateAssetMenu(fileName = "UI Manager", menuName = "Singletons/UIManager")]
public class UIManager : Singleton<UIManager> 

Next, replace initialization methods:

void Awake() {
    // Initialization code here. This happens before any scene has loaded, so it doesn't have access to GameObjects.
}
void Start() {
    // Initialization code here. This happens on the first frame of the application and has access to all scenes.
}
// become:

protected override void Initialize() {
    base.Initialize();
    
    // Initialization code
}

Replace deinitialization methods:

void OnApplicationQuit() {
    // Deinitialization code
}
void OnDestroy() {
    // Deinitialization code
}
// become:

protected override void Deinitialize() {
    base.Deinitialize();
    
    // Deinitialization code
}

Finally, replace Unity event functions:

void Update() {
    // Update code
}
void LateUpdate() {
    // Late Update code
}
void FixedUpdate() {
    // Fixed Update code
}
// become:

public override void OnUpdate() {
    // Update code
}
public override void OnLateUpdate() {
    // Late Update code
}
public override void OnFixedUpdate() {
    // Fixed Update code
}

Singletons automatically create a single MonoBehaviour to handle all Unity events and coroutines for improved performance. Supported Unity events are:

OnFixedUpdate, OnUpdate, OnLateUpdate, OnApplicationFocus, OnApplicationPause, OnApplicationQuit, OnDrawGizmos, OnPostRender, OnPreCull, OnPreRender. They’re all using the original event’s name with the On prefix (if it wasn’t in the name already).

Accessing scene objects

Having your singletons in ScriptableObjects means you won’t be able to reference scene objects in your inspector fields. Although a hassle at first, this can make your scene architecture a lot cleaner, since it will remove potential dependencies between scenes and the problems associated with them.

Here are three ways you can cleanly access scene objects that happen to work perfectly with yaSingleton.

1. Registration

Instead of your Singleton knowing about the specific objects, you can have the objects register themselves in the singleton. Works great if you’re managing many instances of the same behavior (units, items, etc).

class MyBehaviour : MonoBehaviour {
    void OnEnable() {
        MySingleton.AddBehaviour(this);     
    }
    void OnDisable() {
        MySingleton.RemoveBehaviour(this);
    }
}
2. Events

Instead of calling a method on all of the dependant objects, you can have an event to which the dependant objects can hook to.

class MySingleton : Singleton<MySingleton> {
    public event Action sampleEvent = () => { };
    
    public void CallEvent() {
        sampleEvent();
    }
}

class MyBehaviour : MonoBehaviour {
    void OnEnable() {
        MySingleton.sampleEvent += OnSampleEvent;  
    }
    void OnDisable() {
        MySingleton.sampleEvent -= OnSampleEvent;  
    }
    void OnSampleEvent() {
        // Awesome code
    }
}
3. Finding objects

Finally and most straightforward, you can simply find the objects in the scene and go about your business. I suggest using FindGameObjectsWithTag.

class MySingleton : Singleton<MySingleton> {
    private GameObject[] _units;
    
    protected override void Initialize() {
        base.Initialize();
        _units = GameObject.FindGameObjectsWithTag("Unit");
    }
}

Contributing

The project is, of course, open to contributions. Just fork it and post a pull request.

Authors

  • Jordan Georgiev - The one to blame - jedybg

License

This project is licensed under the MIT - see the LICENSE file for details.

评论

暂无评论