├── .github └── FUNDING.yml ├── .gitignore ├── CHANGELOG.md ├── CHANGELOG.md.meta ├── Editor.meta ├── Editor ├── AutoHookPropertyDrawer.cs ├── AutoHookPropertyDrawer.cs.meta ├── TNRD.Autohook.Editor.asmdef └── TNRD.Autohook.Editor.asmdef.meta ├── LICENSE.md ├── LICENSE.md.meta ├── README.md ├── README.md.meta ├── Runtime.meta ├── Runtime ├── AutoHookAttribute.cs ├── AutoHookAttribute.cs.meta ├── AutoHookSearchArea.cs ├── AutoHookSearchArea.cs.meta ├── TNRD.Autohook.asmdef └── TNRD.Autohook.asmdef.meta ├── package.json └── package.json.meta /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: thundernerd 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # This .gitignore file should be placed at the root of your Unity project directory 2 | # 3 | # Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore 4 | # 5 | /[Ll]ibrary/ 6 | /[Tt]emp/ 7 | /[Oo]bj/ 8 | /[Bb]uild/ 9 | /[Bb]uilds/ 10 | /[Ll]ogs/ 11 | /[Mm]emoryCaptures/ 12 | 13 | # Asset meta data should only be ignored when the corresponding asset is also ignored 14 | !/[Aa]ssets/**/*.meta 15 | 16 | # Uncomment this line if you wish to ignore the asset store tools plugin 17 | # /[Aa]ssets/AssetStoreTools* 18 | 19 | # Autogenerated Jetbrains Rider plugin 20 | [Aa]ssets/Plugins/Editor/JetBrains* 21 | 22 | # Visual Studio cache directory 23 | .vs/ 24 | 25 | # Gradle cache directory 26 | .gradle/ 27 | 28 | # Autogenerated VS/MD/Consulo solution and project files 29 | ExportedObj/ 30 | .consulo/ 31 | *.csproj 32 | *.unityproj 33 | *.sln 34 | *.suo 35 | *.tmp 36 | *.user 37 | *.userprefs 38 | *.pidb 39 | *.booproj 40 | *.svd 41 | *.pdb 42 | *.mdb 43 | *.opendb 44 | *.VC.db 45 | 46 | # Unity3D generated meta files 47 | *.pidb.meta 48 | *.pdb.meta 49 | *.mdb.meta 50 | 51 | # Unity3D generated file on crash reports 52 | sysinfo.txt 53 | 54 | # Builds 55 | *.apk 56 | *.unitypackage 57 | 58 | # Crashlytics generated file 59 | crashlytics-build.properties 60 | 61 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [1.0.2] - 2023-12-10 4 | 5 | ### Fixed 6 | - Fixed ReadOnlyWhenFound 7 | 8 | ### Updated 9 | - Package Display Name 10 | 11 | ## [1.0.1] - 2020-10-17 12 | 13 | ### Added 14 | - Extra download option 15 | 16 | ### Updated 17 | - Package Display Name 18 | 19 | ## [1.0.0] - 2020-09-09 20 | ### Added 21 | - Initial version 22 | -------------------------------------------------------------------------------- /CHANGELOG.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f62e7c93fb7b4e44695d253501c66528 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cd6a5b10948de8343a61daa43c14dece 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor/AutoHookPropertyDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEditor; 3 | using UnityEngine; 4 | 5 | namespace TNRD.Autohook 6 | { 7 | [CustomPropertyDrawer(typeof(AutoHookAttribute))] 8 | public class AutoHookPropertyDrawer : PropertyDrawer 9 | { 10 | public override float GetPropertyHeight(SerializedProperty property, GUIContent label) 11 | { 12 | AutoHookAttribute autoHookAttribute = (AutoHookAttribute) attribute; 13 | 14 | if (autoHookAttribute.HideWhenFound && property.objectReferenceValue != null) 15 | { 16 | return 0; 17 | } 18 | 19 | return EditorGUI.GetPropertyHeight(property, label); 20 | } 21 | 22 | public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) 23 | { 24 | AutoHookAttribute autoHookAttribute = (AutoHookAttribute) attribute; 25 | 26 | if (autoHookAttribute.StopSearchWhenFound) 27 | { 28 | if (property.objectReferenceValue == null) 29 | { 30 | Component component = FindComponent(property); 31 | property.objectReferenceValue = component; 32 | } 33 | } 34 | else 35 | { 36 | Component component = FindComponent(property); 37 | 38 | if (property.objectReferenceValue == null && component != null) 39 | { 40 | property.objectReferenceValue = component; 41 | } 42 | } 43 | 44 | EditorGUI.BeginDisabledGroup(autoHookAttribute.ReadOnlyWhenFound && property.objectReferenceValue != null); 45 | EditorGUI.PropertyField(position, property, label); 46 | EditorGUI.EndDisabledGroup(); 47 | } 48 | 49 | private Component FindComponent(SerializedProperty property) 50 | { 51 | SerializedObject root = property.serializedObject; 52 | if (!(root.targetObject is Component)) 53 | { 54 | return null; 55 | } 56 | 57 | Type type = fieldInfo.FieldType; 58 | Component parent = (Component) root.targetObject; 59 | AutoHookAttribute autoHookAttribute = (AutoHookAttribute) attribute; 60 | 61 | switch (autoHookAttribute.SearchArea) 62 | { 63 | case AutoHookSearchArea.Parent: 64 | return parent.GetComponentInParent(type); 65 | case AutoHookSearchArea.Children: 66 | return parent.GetComponentInChildren(type); 67 | case AutoHookSearchArea.DirectChildrenOnly: 68 | return FindComponentInDirectChildren(parent.transform, type); 69 | case AutoHookSearchArea.AllChildrenOnly: 70 | return FindComponentInChildrenRecursive(parent.transform, type); 71 | default: 72 | return parent.GetComponent(type); 73 | } 74 | } 75 | 76 | private Component FindComponentInDirectChildren(Transform parent, Type type) 77 | { 78 | foreach (Transform child in parent) 79 | { 80 | Component component = child.GetComponent(type); 81 | if (component != null) 82 | { 83 | return component; 84 | } 85 | } 86 | 87 | return null; 88 | } 89 | 90 | private Component FindComponentInChildrenRecursive(Transform parent, Type type) 91 | { 92 | foreach (Transform child in parent) 93 | { 94 | Component component = child.GetComponent(type); 95 | if (component != null) 96 | { 97 | return component; 98 | } 99 | 100 | component = FindComponentInChildrenRecursive(child.transform, type); 101 | if (component != null) 102 | { 103 | return component; 104 | } 105 | } 106 | 107 | return null; 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /Editor/AutoHookPropertyDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7c6ba89372804bfda66bcded0079690d 3 | timeCreated: 1599658322 -------------------------------------------------------------------------------- /Editor/TNRD.Autohook.Editor.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "TNRD.Autohook.Editor", 3 | "references": [ 4 | "TNRD.Autohook" 5 | ], 6 | "includePlatforms": [ 7 | "Editor" 8 | ], 9 | "excludePlatforms": [], 10 | "allowUnsafeCode": false, 11 | "overrideReferences": false, 12 | "precompiledReferences": [], 13 | "autoReferenced": true, 14 | "defineConstraints": [], 15 | "versionDefines": [], 16 | "noEngineReferences": false 17 | } -------------------------------------------------------------------------------- /Editor/TNRD.Autohook.Editor.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d17163f88cedc0d4a91c0aa114cd450e 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Christiaan Bloemendaal 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. 22 | -------------------------------------------------------------------------------- /LICENSE.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8edb588f9fe9cc940a584beefdb9a040 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Auto Hook 2 | 3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |