├── .npmignore ├── LICENSE.md.meta ├── README.md.meta ├── package.json.meta ├── Editor ├── template.txt.meta ├── com.bbbirder.injection.Editor.asmdef.meta ├── InjectHelper.cs.meta ├── InjectionSettings.cs.meta ├── TemplateGenerator.cs.meta ├── UnityInjectUtils.cs.meta ├── InjectionSettingsWindow.cs.meta ├── InjectionSourceUI.uxml.meta ├── InjectionWindowUI.uxml.meta ├── InjectionSourceUI.uxml ├── com.bbbirder.injection.Editor.asmdef ├── TemplateGenerator.cs ├── template.txt ├── InjectionSettingsWindow.cs ├── InjectionWindowUI.uxml ├── InjectionSettings.cs ├── UnityInjectUtils.cs └── InjectHelper.cs ├── Documentation ├── usage-di.md.meta ├── usage-decorator.md.meta ├── usage-proxy.md.meta ├── usage-proxy.md ├── usage-di.md └── usage-decorator.md ├── Editor.meta ├── Runtime.meta ├── Samples~ └── MethodFix │ ├── Demo.unity.meta │ ├── com.bbbirder.injection.sample.asmdef.meta │ ├── Demo.cs.meta │ ├── DemoModal.cs.meta │ ├── DemoModal.cs │ ├── com.bbbirder.injection.sample.asmdef │ ├── Demo.cs │ └── Demo.unity ├── Documentation.meta ├── Runtime ├── ClassicalUsages.meta ├── com.bbbirder.injection.asmdef.meta ├── Constants.cs.meta ├── FixHelper.cs.meta ├── IInjection.cs.meta ├── InjectionInfo.cs.meta ├── InjectionAttribute.cs.meta ├── ClassicalUsages │ ├── ProxyData.cs.meta │ ├── Decorator-AutoGen.cs.meta │ ├── OrderDIAttribute.cs.meta │ ├── ServiceContainer.cs.meta │ ├── SimpleDIAttribute.cs.meta │ ├── DecoratorAttribute.cs.meta │ ├── OrderDIAttribute.cs │ ├── ProxyData.cs │ ├── DecoratorAttribute.cs │ ├── SimpleDIAttribute.cs │ ├── ServiceContainer.cs │ └── Decorator-AutoGen.cs ├── IInjection.cs ├── com.bbbirder.injection.asmdef ├── InjectionAttribute.cs ├── Constants.cs ├── InjectionInfo.cs └── FixHelper.cs ├── .github └── workflows │ └── ci.yml ├── package.json ├── LICENSE.md └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | VSProj~ 2 | .github 3 | README.md 4 | Documentation -------------------------------------------------------------------------------- /LICENSE.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 14356c2e544ffe34ea1d907ab3d2f8f6 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f2186d4edaa19b443b2fa1c9ba785a9b 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: add7790670f7d624eb9d946e01ab2660 3 | PackageManifestImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor/template.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ce98390825738344eb1925c70711d599 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Documentation/usage-di.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 79041f1af82164946a2b7f5b38217fe6 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c6af47a612abaff4b9e26e5ee7591997 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Runtime.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 06e9479868352fa4a920ffa6f6413183 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Samples~/MethodFix/Demo.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4c686b13ed31b5a49ba86a2c27364f68 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Documentation.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2c0b8c26a3dbaeb4f8e7aa80a36d3c00 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Documentation/usage-decorator.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 55127db03c3f63148ab97390eb9998e5 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Documentation/usage-proxy.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4d9d250f654dacd4ea3e3e638a00db62 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Runtime/ClassicalUsages.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9f8f02389c389b74e85afc1c067f21ae 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Runtime/com.bbbirder.injection.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 521f16345e07cc2439d976f3bbd67b45 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor/com.bbbirder.injection.Editor.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2567c54de59cd2a49b392206a1c6d31f 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Samples~/MethodFix/com.bbbirder.injection.sample.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 71e2e836302736d418b5e089b90d66f3 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor/InjectHelper.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ba6634917bee7ec408331b633d6656aa 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/Constants.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2c911f63e0861b649a0bd65cd67aa033 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/FixHelper.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ddf99b70e8ecfcd49826915759fa655c 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/IInjection.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3a1865fcca4e0f14ea2bee5b0a8fe8cb 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/InjectionInfo.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 36ae91194cb419b4f94e17c03fb1e6f7 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Editor/InjectionSettings.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9273ff3f41d65c14e99236f70f1f06c7 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Editor/TemplateGenerator.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9a998018f8e3b7a44b5616beab7d6cce 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Editor/UnityInjectUtils.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b77aff1dc6991c343acc25a060739421 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/InjectionAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e4f8561d305c6df4dba609aeeadd988b 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Samples~/MethodFix/Demo.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2c807aa2ed3eacf498e4b8cf0fed1c04 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Editor/InjectionSettingsWindow.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f53701e1414a09b43bf31a057c7e4cbb 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Samples~/MethodFix/DemoModal.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b82c4d7e8a309394b8a9aecb79e26a59 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/ClassicalUsages/ProxyData.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 798b8a97a9e084846b9466e6c2fc3aeb 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/ClassicalUsages/Decorator-AutoGen.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1e1bbfdbc88d96d45bedbfabff63e955 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/ClassicalUsages/OrderDIAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: dd7556feaa4bbc14da18242731861339 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/ClassicalUsages/ServiceContainer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6a7627286c3518f479366b70cdab0f90 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Runtime/ClassicalUsages/SimpleDIAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9eccd17b69df0684891b0a878431acbb 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Create Tag 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v2 13 | - uses: Klemensas/action-autotag@stable 14 | with: 15 | GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" 16 | -------------------------------------------------------------------------------- /Runtime/ClassicalUsages/DecoratorAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 506cafa565bcfb3448e55f06d22a55a9 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Editor/InjectionSourceUI.uxml.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e0689cb498ad5f8459d2bee8fd022488 3 | ScriptedImporter: 4 | internalIDToNameTable: [] 5 | externalObjects: {} 6 | serializedVersion: 2 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} 11 | -------------------------------------------------------------------------------- /Editor/InjectionWindowUI.uxml.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 64949e9c31ce0a04b8aa1a3d01d72ab1 3 | ScriptedImporter: 4 | internalIDToNameTable: [] 5 | externalObjects: {} 6 | serializedVersion: 2 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} 11 | -------------------------------------------------------------------------------- /Samples~/MethodFix/DemoModal.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using System; 5 | 6 | public class DemoModal{ 7 | public static int Salute(){ 8 | return 1; 9 | } 10 | public int ThisSalute(){ 11 | return 1; 12 | } 13 | public float Add(int i, float f){ 14 | return i*f; 15 | } 16 | } -------------------------------------------------------------------------------- /Runtime/IInjection.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace com.bbbirder.injection 4 | { 5 | public interface IInjection : IDirectRetrieve 6 | { 7 | /// 8 | /// set this property to populate injections 9 | /// 10 | /// 11 | public IEnumerable ProvideInjections(); 12 | } 13 | } -------------------------------------------------------------------------------- /Editor/InjectionSourceUI.uxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Runtime/com.bbbirder.injection.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.bbbirder.injection", 3 | "rootNamespace": "", 4 | "references": [ 5 | "GUID:b66755024ec682a4aa0f53d87f0616ea" 6 | ], 7 | "includePlatforms": [], 8 | "excludePlatforms": [], 9 | "allowUnsafeCode": false, 10 | "overrideReferences": false, 11 | "precompiledReferences": [], 12 | "autoReferenced": true, 13 | "defineConstraints": [], 14 | "versionDefines": [], 15 | "noEngineReferences": false 16 | } -------------------------------------------------------------------------------- /Runtime/InjectionAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | using UnityEngine.Assertions; 6 | 7 | namespace com.bbbirder.injection 8 | { 9 | public abstract class InjectionAttribute : DirectRetrieveAttribute 10 | { 11 | /// 12 | /// set this property to populate injections 13 | /// 14 | /// 15 | public abstract IEnumerable ProvideInjections(); 16 | } 17 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.bbbirder.injection", 3 | "displayName": "Unity Injection", 4 | "description": "Unity注入模块,可以运行时改变被注入函数实现。", 5 | "version": "1.3.32", 6 | "hideInEditor": false, 7 | "author": "bbbirder <502100554@qq.com>", 8 | "dependencies": { 9 | "com.bbbirder.directattribute": "2.0.0", 10 | "com.unity.nuget.mono-cecil": "1.10.2" 11 | }, 12 | "samples": [ 13 | { 14 | "displayName": "Method Fix", 15 | "path": "Samples~/MethodFix" 16 | } 17 | ], 18 | "gitHead": "18b3c53f1dec43ebd854d02b30959cf96962da13" 19 | } 20 | -------------------------------------------------------------------------------- /Samples~/MethodFix/com.bbbirder.injection.sample.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.bbbirder.injection.sample", 3 | "rootNamespace": "", 4 | "references": [ 5 | "GUID:b66755024ec682a4aa0f53d87f0616ea", 6 | "GUID:521f16345e07cc2439d976f3bbd67b45" 7 | ], 8 | "includePlatforms": [], 9 | "excludePlatforms": [], 10 | "allowUnsafeCode": false, 11 | "overrideReferences": false, 12 | "precompiledReferences": [], 13 | "autoReferenced": true, 14 | "defineConstraints": [], 15 | "versionDefines": [], 16 | "noEngineReferences": false 17 | } -------------------------------------------------------------------------------- /Editor/com.bbbirder.injection.Editor.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.bbbirder.injection.Editor", 3 | "rootNamespace": "", 4 | "references": [ 5 | "com.bbbirder.injection", 6 | "com.bbbirder.directattribute" 7 | ], 8 | "includePlatforms": [ 9 | "Editor" 10 | ], 11 | "excludePlatforms": [], 12 | "allowUnsafeCode": false, 13 | "overrideReferences": true, 14 | "precompiledReferences": [ 15 | "Mono.Cecil.dll" 16 | ], 17 | "autoReferenced": true, 18 | "defineConstraints": [], 19 | "versionDefines": [], 20 | "noEngineReferences": false 21 | } -------------------------------------------------------------------------------- /Runtime/ClassicalUsages/OrderDIAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace com.bbbirder.injection 4 | { 5 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, Inherited = false)] 6 | public class OrderDIAttribute : Attribute 7 | { 8 | internal int order; 9 | /// 10 | /// The default assign order of this type 11 | /// 12 | /// pick from litter 13 | public OrderDIAttribute(int order) 14 | { 15 | this.order = order; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Documentation/usage-proxy.md: -------------------------------------------------------------------------------- 1 | # 数据代理 2 | 3 | 下面的代码演示实现一个数据代理: 4 | 5 | ```csharp 6 | public class Pat : ProxyData // 需要继承ProxyData 7 | { 8 | public string name { get; set; } 9 | public int age { get; set; } 10 | } 11 | 12 | var pet = new Pet(); 13 | pet.OnSetProperty(key=>{ 14 | print($"set {key}"); 15 | }); 16 | pet.OnGetProperty(key=>{ 17 | print($"get {key}"); 18 | }); 19 | 20 | pet.age += 1; 21 | 22 | /* 23 | output: 24 | get age 25 | set age 26 | */ 27 | 28 | ``` 29 | 30 | 有时你需要判断代理类是否被正确注入: 31 | 32 | ```csharp 33 | print(pet.IsFixed()); // false 34 | FixHelper.InstallAll(); 35 | print(pet.IsFixed()); // true 36 | ``` 37 | -------------------------------------------------------------------------------- /Editor/TemplateGenerator.cs: -------------------------------------------------------------------------------- 1 | // using UnityEngine; 2 | // using UnityEditor; 3 | // using System.Runtime.CompilerServices; 4 | // using System.IO; 5 | 6 | // class TemplateGenerator{ 7 | // [InitializeOnLoadMethod] 8 | // static void Generate(){ 9 | // var input = File.ReadAllText(GetFilePath()); 10 | // var output = Scriban.Template.Parse(input).Render(); 11 | // File.WriteAllText(GetOutputPath(),output); 12 | // Debug.Log(output); 13 | // } 14 | // static string GetFilePath([CallerFilePath]string path=null) 15 | // =>Path.Join(path,"../template.txt"); 16 | // static string GetOutputPath([CallerFilePath]string path=null) 17 | // =>Path.Join(path,"../../Runtime/Decorator-AutoGen.cs"); 18 | // } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 labbbirder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Runtime/Constants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Security.Cryptography; 5 | using System.Text; 6 | 7 | namespace com.bbbirder.injection 8 | { 9 | public static class Constants 10 | { 11 | public const string INJECTED_MARK_NAMESPACE = "com.bbbirder"; 12 | public const string INJECTED_MARK_NAME = "InjectedMarkAttribute"; 13 | 14 | public static string GetInjectedFieldName(string methodName, string methodSignature) 15 | => strBuilder.Clear().Append("_injection_field+").Append(methodName).Append(MD5Hash(methodSignature)).ToString(); 16 | 17 | public static string GetOriginMethodName(string methodName, string methodSignature) 18 | => strBuilder.Clear().Append("_injection_origin+").Append(methodName).Append(MD5Hash(methodSignature)).ToString(); 19 | 20 | static string MD5Hash(string rawContent) 21 | { 22 | var md5 = MD5.Create(); 23 | var buffer = md5.ComputeHash(Encoding.UTF8.GetBytes(rawContent)); 24 | return string.Concat(buffer.Select(b => b.ToString("X"))); 25 | } 26 | 27 | static StringBuilder strBuilder = new(); 28 | } 29 | } -------------------------------------------------------------------------------- /Editor/template.txt: -------------------------------------------------------------------------------- 1 | // Dont Modify It: This is an Auto-Generated File 2 | using System; 3 | using System.Reflection; 4 | 5 | {{ 6 | func get_pt 7 | $cnt = $0 8 | $format = $1 9 | $spliter = $2 10 | $result = '' 11 | for $i in (1..$cnt) 12 | $result += string.replace($format,'i',$i) 13 | if $i!=$cnt 14 | $result += $spliter 15 | end 16 | end 17 | ret $result 18 | end 19 | }} 20 | namespace com.bbbirder.unity { 21 | partial class DecoratorAttribute{ 22 | {{~ for cnt in 1..12 ~}} 23 | {{~pt_gl = get_pt cnt 'Ti' ','~}} 24 | {{~pt_vl = get_pt cnt 'ti' ','~}} 25 | {{~pt_dl = get_pt cnt 'Ti _ti' ','~}} 26 | Func<{{pt_gl}},R> UniversalFunc<{{pt_gl}},R>(MethodInfo mi){ 27 | {{~ for i in 1..cnt ~}} 28 | T{{i}} t{{i}} = default; 29 | {{~ end ~}} 30 | var invocation = new InvocationInfo(){ 31 | invoker = ()=>((Func<{{pt_gl}},R>)originFunc).Invoke({{pt_vl}}), 32 | Method = mi, 33 | argumentGetter = ()=>new object[]{ {{pt_vl}} } 34 | }; 35 | return ({{pt_dl}})=>{ 36 | {{~ for i in 1..cnt ~}} 37 | t{{i}} = _t{{i}}; 38 | {{~ end ~}} 39 | return Decorate(invocation); 40 | }; 41 | } 42 | Action<{{pt_gl}}> UniversalAction<{{pt_gl}}>(MethodInfo mi){ 43 | {{~ for i in 1..cnt ~}} 44 | T{{i}} t{{i}} = default; 45 | {{~ end ~}} 46 | var invocation = new InvocationInfo(){ 47 | invoker = ()=>{ 48 | ((Action<{{pt_gl}}>)originFunc).Invoke({{pt_vl}}); 49 | return null; 50 | }, 51 | Method = mi, 52 | argumentGetter = ()=>new object[]{ {{pt_vl}} } 53 | }; 54 | return ({{pt_dl}})=>{ 55 | {{~ for i in 1..cnt ~}} 56 | t{{i}} = _t{{i}}; 57 | {{~ end ~}} 58 | Decorate(invocation); 59 | }; 60 | } 61 | {{~ end ~}} 62 | } 63 | } -------------------------------------------------------------------------------- /Documentation/usage-di.md: -------------------------------------------------------------------------------- 1 | # 依赖注入 2 | 3 | 依赖注入对于模块间解耦帮助很大。 4 | 5 | ## 基本用法 6 | 7 | 在类成员上使用`[SimpleDI]`标签,使其可被注入。 8 | 9 | ```csharp 10 | public interface IFoo 11 | { 12 | 13 | } 14 | 15 | public class FooA : IFoo 16 | { 17 | 18 | } 19 | 20 | public class FooB : IFoo 21 | { 22 | 23 | } 24 | 25 | public class ServiceA 26 | { 27 | [SimpleDI] public IFoo foo {get;} 28 | } 29 | 30 | ``` 31 | 32 | 定义规则: 33 | 34 | ```csharp 35 | void Start() 36 | { 37 | // 使用FooA实现IFoo 38 | ServiceContainer.Bind().AsSingle(noLazy:true).To(); 39 | } 40 | 41 | ``` 42 | 43 | ## 支持注入的成员 44 | 45 | 支持以下成员: 46 | 47 | + 字段、属性 48 | + 静态成员、实例成员 49 | + 只读成员、可读可写成员 50 | 51 | 如: 52 | 53 | ```csharp 54 | class Foo 55 | { 56 | [SimpleDI] static IFoo foo; 57 | [SimpleDI] static IFoo foo { get; } 58 | [SimpleDI] static IFoo foo { get; set; } 59 | [SimpleDI] IFoo foo; 60 | [SimpleDI] IFoo foo { get; } 61 | [SimpleDI] IFoo foo { get; set; } 62 | } 63 | 64 | ``` 65 | 66 | ## 更多控制 67 | 68 | ### 限定DeclaringType 69 | 70 | ```csharp 71 | void Init() 72 | { 73 | // 使用FooA实现IFoo,只对ServiceA中的成员生效 74 | ServiceContainer.In().Bind().AsSingle(noLazy:true).To(); 75 | } 76 | 77 | ``` 78 | 79 | ### 构造函数 80 | 81 | ```csharp 82 | interface IFoo { } 83 | class Foo : IFoo 84 | { 85 | public Foo(string name, int age) 86 | { 87 | Console.WriteLine($"我的伙伴{name},{age}岁了"); 88 | } 89 | } 90 | 91 | void Init() 92 | { 93 | // output: 我的伙伴小黑儿,3岁了 94 | ServiceContainer.Bind().AsSingle(noLazy:true).To("小黑儿",3); 95 | } 96 | 97 | ``` 98 | 99 | ### 生命模式 ScopeMode 100 | 101 | + Single 单例(默认) 102 | + Transient 每次获取都是新实例 103 | 104 | ```csharp 105 | // 使用FooA实现IFoo 106 | ServiceContainer.Bind().AsSingle().To(); 107 | ServiceContainer.Bind().AsTransient().To(); 108 | ``` 109 | 110 | ### 泛型递归 111 | 112 | 对于`IManager`会分别查找IManager,IFoo,IBar,并支持泛型约束。 113 | 114 | ```csharp 115 | interface IFoo 116 | { 117 | 118 | } 119 | 120 | interface IManager 121 | { 122 | 123 | } 124 | 125 | class Foo : IFoo 126 | { 127 | 128 | } 129 | 130 | class Manager : IManager 131 | { 132 | 133 | } 134 | 135 | ``` 136 | 137 | ```csharp 138 | void Init() 139 | { 140 | ServiceContainer.Get>(); // returns Manager 141 | } 142 | ``` 143 | -------------------------------------------------------------------------------- /Editor/InjectionSettingsWindow.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEngine; 3 | using System; 4 | using UnityEngine.UIElements; 5 | using UnityEditor.UIElements; 6 | using System.IO; 7 | 8 | namespace com.bbbirder.injection.editor 9 | { 10 | 11 | public class InjectionSettingsWindow : EditorWindow 12 | { 13 | const string rootUiAssetGUID = "64949e9c31ce0a04b8aa1a3d01d72ab1"; 14 | const string ElementUiAssetGUID = "e0689cb498ad5f8459d2bee8fd022488"; 15 | [MenuItem("Tools/bbbirder/Unity Injection")] 16 | public static void ShowWindow() 17 | { 18 | var window = GetWindow(); 19 | window.titleContent = new GUIContent("Unity Injection"); 20 | window.Show(); 21 | } 22 | // public override void SaveChanges() 23 | // { 24 | // base.SaveChanges(); 25 | // Debug.Log("save"); 26 | // } 27 | void CreateGUI() 28 | { 29 | var settings = InjectionSettings.instance; 30 | settings.hideFlags &= ~HideFlags.NotEditable; 31 | var uiAsset = GetVisualTreeAssetByGUID(rootUiAssetGUID); 32 | var uiEleAsset = GetVisualTreeAssetByGUID(ElementUiAssetGUID); 33 | uiAsset.CloneTree(rootVisualElement); 34 | var lstSource = rootVisualElement.Q("lstSource"); 35 | var lstError = rootVisualElement.Q("lstError"); 36 | 37 | lstSource.makeItem = uiEleAsset.CloneTree; 38 | lstSource.bindItem = (v, i) => 39 | { 40 | var data = settings.injectionSources[i]; 41 | v.Q