GithubHelp home page GithubHelp logo

labbbirder / unitysingleton Goto Github PK

View Code? Open in Web Editor NEW
23.0 1.0 3.0 84 KB

Unity单例模块,支持Editor和Runtime,简单可靠,独立于场景。自带可视化窗口

License: MIT License

C# 100.00%
singleton unity

unitysingleton's Introduction

UnitySingleton

可以在Editor和Runtime混用的Unity单例模块。稳定可靠,懒加载,无额外开销,任何情况都不会出现多实例化。

GitHub last commit GitHub package.json version openupm

Features

  • 兼容所有场景,稳定可靠的提供一个单例:
    • Editor和Runtime混合使用
    • 中途重新编译脚本
    • 不同的Reload Domain选项
    • 场景切换
    • etc
  • 可以包含子物体:基于MonoBehaviour,可添加子GameObject
  • 独立于场景:单例默认使用HideFlags隐藏,不属于任何场景,不需要考虑入口场景
  • 可检索:可视化窗口检索单例列表(Window/bbbirder/Singletons)

Basic Usage

只需要继承Singleton<T>

using UnityEngine;
using com.bbbirder.unity;
public class YourComponent:Singleton<YourComponent>{
    protected override void Awake(){
        SayHello();
    }
    public void SayHello(){
        print("hello,world");
    }
}

需要调用的地方 :

    YourComponent.Instance.SayHello();

通过Window/bbbirder/Singletons窗口查看当前的单例

窗口截图

More Controls

指定实例化时机

[assembly: SingletonAutoLoad(typeof(Manager))]
public class Manager:Singleton<Manager>{ //Manager 将会自动实例化

}

SingletonAutoLoadAttribute接收2个参数:

  • 目标单例类型
  • 实例化时机 enum SingletonCreateCondition
    • LazyLoad 懒加载,通过Instance获取时才实例化。无SingletonAutoLoad属性时默认模式
    • DomainReload 即时加载,在脚本开始运行前实例化。SingletonAutoLoad缺省选项
    • EnterPlay 进入播放模式加载

使用形如[assembly:]的特性使得SingletonAutoLoadAttribute在内部检索所有单例类型时消耗小,且非常迅速。可以放心的在Runtime环境下使用此模块。

指定销毁时机

public class Manager:Singleton<Manager>{
    public override SingletonDestroyCondition DestroyCondition => SingletonDestroyCondition.ExitEdit;
}

SingletonDestroyCondition 具有以下选项:

  • Never 不自动销毁
  • SceneUnload 场景卸载时
  • ReloadDomain 脚本重加载时 (默认)
  • ExitPlay 切出播放模式时
  • ExitEdit 切出编辑模式时

自定义实例化方法

public class TestSingleton : Singleton<TestSingleton>
{
    protected new static TestSingleton CreateInstance(){
        var go = Singleton<TestSingleton>.CreateInstance();
        go.name += " (Singleton)";
        return go;
    }
}

unitysingleton's People

Contributors

labbbirder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.