├── .gitignore ├── Assets ├── Scenes.meta ├── Scenes │ ├── Test.unity │ └── Test.unity.meta ├── TestScripts.meta ├── TestScripts │ ├── AttributeCache.cs │ ├── AttributeCache.cs.meta │ ├── AttributeCacheInherit.cs │ ├── AttributeCacheInherit.cs.meta │ ├── CacheHelper.cs │ ├── CacheHelper.cs.meta │ ├── CachedAttribute.cs │ ├── CachedAttribute.cs.meta │ ├── Example.cs │ ├── Example.cs.meta │ ├── ExternalCache.cs │ ├── ExternalCache.cs.meta │ ├── InnerCache.cs │ ├── InnerCache.cs.meta │ ├── MultipleAttributeTest.cs │ ├── MultipleAttributeTest.cs.meta │ ├── PerformanceChecker.cs │ ├── PerformanceChecker.cs.meta │ ├── Test1.cs │ ├── Test1.cs.meta │ ├── Test2.cs │ ├── Test2.cs.meta │ ├── Test3.cs │ ├── Test3.cs.meta │ ├── Test4.cs │ ├── Test4.cs.meta │ ├── Test5.cs │ ├── Test5.cs.meta │ ├── TestComponent.cs │ ├── TestComponent.cs.meta │ ├── TestInheritScript.cs │ └── TestInheritScript.cs.meta ├── UnityCache.meta ├── UnityCache │ ├── Editor.meta │ ├── Editor │ │ ├── PreCacheEditor.cs │ │ └── PreCacheEditor.cs.meta │ ├── Examples.meta │ ├── Examples │ │ ├── CacheExample.cs │ │ ├── CacheExample.cs.meta │ │ ├── PreCacheExample.cs │ │ ├── PreCacheExample.cs.meta │ │ ├── _Example.unity │ │ └── _Example.unity.meta │ ├── LICENSE.txt │ ├── LICENSE.txt.meta │ ├── README.txt │ ├── README.txt.meta │ ├── Scripts.meta │ └── Scripts │ │ ├── Cached.cs │ │ ├── Cached.cs.meta │ │ ├── CachedAttribute.cs │ │ ├── CachedAttribute.cs.meta │ │ ├── PreCachedAttribute.cs │ │ ├── PreCachedAttribute.cs.meta │ │ ├── UCache.cs │ │ └── UCache.cs.meta ├── unity-cache-0.32.unitypackage └── unity-cache-0.32.unitypackage.meta ├── LICENSE.txt ├── ProjectSettings ├── AudioManager.asset ├── ClusterInputManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── PresetManager.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset └── UnityConnectSettings.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything 2 | /* 3 | /*/ 4 | 5 | # Inverse ignore some stuff 6 | !/Assets/ 7 | !/ProjectSettings/ 8 | !.gitignore 9 | 10 | # OS Stuff 11 | .DS_Store 12 | ._* 13 | .Spotlight-V100 14 | .Trashes 15 | ehthumbs.db 16 | Thumbs.db 17 | $RECYCLE.BIN/ 18 | Desktop.ini -------------------------------------------------------------------------------- /Assets/Scenes.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4a2be26a45f660d4bb8163f6b0261bb8 3 | folderAsset: yes 4 | timeCreated: 1464868011 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scenes/Test.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/Assets/Scenes/Test.unity -------------------------------------------------------------------------------- /Assets/Scenes/Test.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9014ab5464aaf65409a40546c26b3400 3 | timeCreated: 1464868018 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/TestScripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 522a08ef5265f144baa3fbebb0cad01e 3 | folderAsset: yes 4 | timeCreated: 1464868005 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/TestScripts/AttributeCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | using UnityEngine.Profiling; 4 | 5 | namespace TestScripts { 6 | public class AttributeCache : MonoBehaviour { 7 | public int Tries = 1000; 8 | 9 | [Cached] 10 | public TestComponent Test = null; 11 | 12 | void Update() { 13 | DirectLoop(); 14 | ReflectedLoop(); 15 | CachedLoop(); 16 | } 17 | 18 | void DirectLoop() { 19 | GC.Collect(); 20 | Profiler.BeginSample("Init Cache Directly"); 21 | for ( int i = 0; i < Tries; i++ ) { 22 | Direct(); 23 | } 24 | Profiler.EndSample(); 25 | } 26 | 27 | void ReflectedLoop() { 28 | GC.Collect(); 29 | Profiler.BeginSample("Init Static Attribute Cache by Reflection (first time)"); 30 | for ( int i = 0; i < Tries; i++ ) { 31 | Reflected(); 32 | } 33 | Profiler.EndSample(); 34 | } 35 | 36 | void CachedLoop() { 37 | GC.Collect(); 38 | Profiler.BeginSample("Init Static Attribute Cache with known fields (next times)"); 39 | for ( int i = 0; i < Tries; i++ ) { 40 | Cached(); 41 | } 42 | Profiler.EndSample(); 43 | } 44 | 45 | void Direct() { 46 | Test = GetComponent(); 47 | } 48 | 49 | void Reflected() { 50 | CacheHelper.CacheAll(this, false); 51 | } 52 | 53 | void Cached() { 54 | CacheHelper.CacheAll(this, true); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Assets/TestScripts/AttributeCache.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ca293e56f50e4804a8e1815ffc78ad5a 3 | timeCreated: 1464941655 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/AttributeCacheInherit.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | namespace TestScripts { 7 | public class AttributeCacheInherit : MonoBehaviour { 8 | public int Tries = 1000; 9 | 10 | protected virtual void Awake() { 11 | CacheAll(); 12 | } 13 | 14 | void CacheAll() { 15 | var type = GetType(); 16 | CacheFields(GetFieldsToCache(type)); 17 | } 18 | 19 | List GetFieldsToCache(Type type) { 20 | var fields = new List(); 21 | foreach ( var field in type.GetFields() ) { 22 | foreach ( var a in field.GetCustomAttributes(false) ) { 23 | if ( a is CachedAttribute ) { 24 | fields.Add(field); 25 | } 26 | } 27 | } 28 | return fields; 29 | } 30 | 31 | void CacheFields(List fields) { 32 | var iter = fields.GetEnumerator(); 33 | while ( iter.MoveNext() ) { 34 | var type = iter.Current.FieldType; 35 | iter.Current.SetValue(this, GetComponent(type)); 36 | } 37 | } 38 | 39 | void Update() { 40 | MemberLoop(); 41 | } 42 | 43 | void MemberLoop() { 44 | GC.Collect(); 45 | UnityEngine.Profiling.Profiler.BeginSample("Init Member Attribute Cache by Reflection"); 46 | for ( int i = 0; i < Tries; i++ ) { 47 | Member(); 48 | } 49 | UnityEngine.Profiling.Profiler.EndSample(); 50 | } 51 | 52 | void Member() { 53 | CacheAll(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Assets/TestScripts/AttributeCacheInherit.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fe510ac61c64a8041bf5cbf2ef0a8f60 3 | timeCreated: 1465023374 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/CacheHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | namespace TestScripts { 7 | public static class CacheHelper { 8 | static Dictionary> _cachedTypes = new Dictionary>(); 9 | 10 | public static void CacheAll(MonoBehaviour instance, bool internalCache = true) { 11 | var type = instance.GetType(); 12 | if ( internalCache ) { 13 | List fields = null; 14 | if ( !_cachedTypes.TryGetValue(type, out fields) ) { 15 | fields = GetFieldsToCache(type); 16 | _cachedTypes[type] = fields; 17 | } 18 | CacheFields(instance, fields); 19 | } else { 20 | CacheFields(instance, GetFieldsToCache(type)); 21 | } 22 | } 23 | 24 | static List GetFieldsToCache(Type type) { 25 | var fields = new List(); 26 | foreach ( var field in type.GetFields() ) { 27 | foreach ( var a in field.GetCustomAttributes(false) ) { 28 | if ( a is CachedAttribute ) { 29 | fields.Add(field); 30 | } 31 | } 32 | } 33 | return fields; 34 | } 35 | 36 | static void CacheFields(MonoBehaviour instance, List fields) { 37 | var iter = fields.GetEnumerator(); 38 | while ( iter.MoveNext() ) { 39 | var type = iter.Current.FieldType; 40 | iter.Current.SetValue(instance, instance.GetComponent(type)); 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Assets/TestScripts/CacheHelper.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a27110a143906c540a91fa6f768184b8 3 | timeCreated: 1464943052 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/CachedAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TestScripts { 4 | [AttributeUsage(AttributeTargets.Field)] 5 | public class CachedAttribute : Attribute { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Assets/TestScripts/CachedAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 04095a263d1cc914e9659965ac32efc4 3 | timeCreated: 1464941873 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/Example.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace TestScripts { 4 | public class Example : MonoBehaviour { 5 | Rigidbody _rigidbody; 6 | 7 | void Start() { 8 | _rigidbody = GetComponent(); 9 | } 10 | 11 | void Update() { 12 | _rigidbody.AddForce(Vector3.up * Time.deltaTime); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Assets/TestScripts/Example.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 81c31a6f7e8d58a4bae12a3ebff073b6 3 | timeCreated: 1465021996 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/ExternalCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | namespace TestScripts { 6 | public static class ExternalCache { 7 | static Dictionary _trans = new Dictionary(); 8 | 9 | public static Transform GetCachedTransfom(this GameObject owner) { 10 | Transform item = null; 11 | if ( !_trans.TryGetValue(owner, out item) ) { 12 | item = owner.GetComponent(); 13 | _trans.Add(owner, item); 14 | } 15 | return item; 16 | } 17 | 18 | static Dictionary _test = new Dictionary(); 19 | 20 | public static TestComponent GetCachedTestComponent(this GameObject owner) { 21 | TestComponent item = null; 22 | if ( !_test.TryGetValue(owner, out item) ) { 23 | item = owner.GetComponent(); 24 | _test.Add(owner, item); 25 | } 26 | return _test[owner]; 27 | } 28 | 29 | static Dictionary> _cache = new Dictionary>(); 30 | 31 | public static T GetCachedComponent(this GameObject owner) where T : Component { 32 | var type = typeof(T); 33 | 34 | Dictionary container = null; 35 | if ( !_cache.TryGetValue(owner, out container) ) { 36 | container = new Dictionary(); 37 | _cache.Add(owner, container); 38 | } 39 | Component item = null; 40 | if ( !container.TryGetValue(type, out item) ) { 41 | item = owner.GetComponent(); 42 | container.Add(type, item); 43 | } 44 | 45 | return item as T; 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Assets/TestScripts/ExternalCache.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 024b9a8b5e577e843a7654506e7675eb 3 | timeCreated: 1464926609 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/InnerCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | namespace TestScripts { 6 | public class InnerCache : MonoBehaviour { 7 | Dictionary _cache = new Dictionary(); 8 | 9 | public T Get() where T : Component { 10 | var type = typeof(T); 11 | Component item = null; 12 | if ( !_cache.TryGetValue(type, out item) ) { 13 | item = GetComponent(); 14 | _cache.Add(type, item); 15 | } 16 | 17 | return item as T; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Assets/TestScripts/InnerCache.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8fd2a0f01941eb2448779958399107b8 3 | timeCreated: 1464928264 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/MultipleAttributeTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | using UnityEngine.Profiling; 4 | 5 | namespace TestScripts { 6 | public class MultipleAttributeTest : MonoBehaviour { 7 | public int Tries = 1000; 8 | 9 | [Cached] 10 | public Test1 t1; 11 | [Cached] 12 | public Test2 t2; 13 | [Cached] 14 | public Test3 t3; 15 | [Cached] 16 | public Test4 t4; 17 | [Cached] 18 | public Test5 t5; 19 | 20 | void Update() { 21 | DirectLoop(); 22 | ReflectedLoop(); 23 | CachedLoop(); 24 | } 25 | 26 | void DirectLoop() { 27 | GC.Collect(); 28 | Profiler.BeginSample("Init Cache Directly - 5 components"); 29 | for ( int i = 0; i < Tries; i++ ) { 30 | Direct(); 31 | } 32 | Profiler.EndSample(); 33 | } 34 | 35 | void ReflectedLoop() { 36 | GC.Collect(); 37 | Profiler.BeginSample("Init Static Attribute Cache by Reflection (first time) - 5 components"); 38 | for ( int i = 0; i < Tries; i++ ) { 39 | Reflected(); 40 | } 41 | Profiler.EndSample(); 42 | } 43 | 44 | void CachedLoop() { 45 | GC.Collect(); 46 | Profiler.BeginSample("Init Static Attribute Cache with known fields (next times) - 5 components"); 47 | for ( int i = 0; i < Tries; i++ ) { 48 | Cached(); 49 | } 50 | Profiler.EndSample(); 51 | } 52 | 53 | void Direct() { 54 | t1 = GetComponent(); 55 | t2 = GetComponent(); 56 | t3 = GetComponent(); 57 | t4 = GetComponent(); 58 | t5 = GetComponent(); 59 | } 60 | 61 | void Reflected() { 62 | CacheHelper.CacheAll(this, false); 63 | } 64 | 65 | void Cached() { 66 | CacheHelper.CacheAll(this, true); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /Assets/TestScripts/MultipleAttributeTest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6ecac4334cd78e24fb4970ca572552d7 3 | timeCreated: 1465028311 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/PerformanceChecker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | using UnityEngine.Profiling; 4 | 5 | namespace TestScripts { 6 | public class PerformanceChecker : MonoBehaviour { 7 | public bool TestTransform = false; 8 | public bool TestComponent = false; 9 | public int Tries = 1000; 10 | public InnerCache Cache = null; 11 | public AttributeCache AttrCache = null; 12 | public TestInheritScript TestInh = null; 13 | 14 | float _counter = 0; 15 | bool _skip = true; 16 | 17 | // Transform 18 | Transform _transVar = null; 19 | 20 | Transform _transProp = null; 21 | Transform TransProp { 22 | get { 23 | if ( !_transProp ) { 24 | _transProp = transform; 25 | } 26 | return _transProp; 27 | } 28 | } 29 | 30 | Transform _transPropConv = null; 31 | Transform TransPropConv { 32 | get { 33 | if ( (object)_transPropConv == null ) { 34 | _transPropConv = transform; 35 | } 36 | return _transPropConv; 37 | } 38 | } 39 | 40 | bool _transPropCached = false; 41 | Transform _transPropChecked = null; 42 | Transform TransPropChecked { 43 | get { 44 | if ( !_transPropCached ) { 45 | _transPropChecked = transform; 46 | _transPropCached = true; 47 | } 48 | return _transPropChecked; 49 | } 50 | } 51 | 52 | // TestComponent 53 | TestComponent _testVar = null; 54 | 55 | TestComponent _testProp = null; 56 | TestComponent TestProp { 57 | get { 58 | if ( !_testProp ) { 59 | _testProp = GetComponent(); 60 | } 61 | return _testProp; 62 | } 63 | } 64 | 65 | TestComponent _testPropConv = null; 66 | TestComponent TestPropConv { 67 | get { 68 | if ( (object)_testPropConv == null ) { 69 | _testPropConv = GetComponent(); 70 | } 71 | return _testPropConv; 72 | } 73 | } 74 | 75 | bool _testPropCached = false; 76 | TestComponent _testPropChecked = null; 77 | TestComponent TestPropChecked { 78 | get { 79 | if ( !_testPropCached ) { 80 | _testPropChecked = GetComponent(); 81 | _testPropCached = true; 82 | } 83 | return _testPropChecked; 84 | } 85 | } 86 | 87 | void Awake() { 88 | _transVar = transform; 89 | _testVar = GetComponent(); 90 | } 91 | 92 | void Update() { 93 | if ( _skip ) { 94 | _skip = false; 95 | return; 96 | } 97 | 98 | GC.Collect(); 99 | 100 | _counter = 0; 101 | 102 | if ( TestTransform ) { 103 | Direct_Trans(); 104 | 105 | LocalCacheVariable_Trans(); 106 | LocalCacheProperty_Trans(); 107 | LocalCachePropertyConv_Trans(); 108 | LocalCachePropertyChecked_Trans(); 109 | 110 | ExternalCacheSimple_Trans(); 111 | ExternalCacheTemplate_Trans(); 112 | 113 | InnerCacheTemplate_Trans(); 114 | } 115 | 116 | if ( TestComponent ) { 117 | DirectT_Test(); 118 | DirectT_Null_Test(); 119 | DirectsT_Test(); 120 | DirectsT_Null_Test(); 121 | DirectS_Test(); 122 | DirectS_Null_Test(); 123 | DirectsS_Test(); 124 | DirectsS_Null_Test(); 125 | 126 | LocalCacheVariable_Test(); 127 | LocalCacheProperty_Test(); 128 | LocalCachePropertyConv_Test(); 129 | LocalCachePropertyChecked_Test(); 130 | 131 | ExternalCacheSimple_Test(); 132 | ExternalCacheTemplate_Test(); 133 | 134 | InnerCacheTemplate_Test(); 135 | 136 | InnerCacheAttribute_Test(); 137 | InnerCacheAttributeInherit_Test(); 138 | } 139 | } 140 | 141 | // Transform 142 | void Direct_Trans() { 143 | for ( int i = 0; i < Tries; i++ ) { 144 | _counter += transform.position.x; 145 | } 146 | } 147 | 148 | void LocalCacheVariable_Trans() { 149 | for ( int i = 0; i < Tries; i++ ) { 150 | _counter += _transVar.position.x; 151 | } 152 | } 153 | 154 | void LocalCacheProperty_Trans() { 155 | for ( int i = 0; i < Tries; i++ ) { 156 | _counter += TransProp.position.x; 157 | } 158 | } 159 | 160 | void LocalCachePropertyConv_Trans() { 161 | for ( int i = 0; i < Tries; i++ ) { 162 | _counter += TransPropConv.position.x; 163 | } 164 | } 165 | 166 | void LocalCachePropertyChecked_Trans() { 167 | for ( int i = 0; i < Tries; i++ ) { 168 | _counter += TransPropChecked.position.x; 169 | } 170 | } 171 | 172 | void ExternalCacheSimple_Trans() { 173 | for ( int i = 0; i < Tries; i++ ) { 174 | _counter += gameObject.GetCachedTransfom().position.x; 175 | } 176 | } 177 | 178 | void ExternalCacheTemplate_Trans() { 179 | for ( int i = 0; i < Tries; i++ ) { 180 | _counter += gameObject.GetCachedComponent().position.x; 181 | } 182 | } 183 | 184 | void InnerCacheTemplate_Trans() { 185 | for ( int i = 0; i < Tries; i++ ) { 186 | _counter += Cache.Get().position.x; 187 | } 188 | } 189 | 190 | // Test Component 191 | void DirectT_Test() { 192 | Profiler.BeginSample("Get Component"); 193 | for ( int i = 0; i < Tries; i++ ) { 194 | _counter += GetComponent().Vector.x; 195 | } 196 | Profiler.EndSample(); 197 | } 198 | 199 | void DirectT_Null_Test() { 200 | Profiler.BeginSample("Get Component Null"); 201 | for ( int i = 0; i < Tries; i++ ) { 202 | #pragma warning disable CS0219 203 | var item = GetComponent(); 204 | #pragma warning restore CS0219 205 | } 206 | Profiler.EndSample(); 207 | } 208 | 209 | void DirectsT_Test() { 210 | Profiler.BeginSample("Get Components"); 211 | for ( int i = 0; i < Tries; i++ ) { 212 | _counter += GetComponents()[0].Vector.x; 213 | } 214 | Profiler.EndSample(); 215 | } 216 | 217 | void DirectsT_Null_Test() { 218 | Profiler.BeginSample("Get Components Null"); 219 | for ( int i = 0; i < Tries; i++ ) { 220 | #pragma warning disable CS0219 221 | var items = GetComponents(); 222 | #pragma warning restore CS0219 223 | } 224 | Profiler.EndSample(); 225 | } 226 | 227 | void DirectS_Test() { 228 | Profiler.BeginSample("Get Component By Type"); 229 | for ( int i = 0; i < Tries; i++ ) { 230 | _counter += (GetComponent(typeof(TestComponent)) as TestComponent).Vector.x; 231 | } 232 | Profiler.EndSample(); 233 | } 234 | 235 | void DirectS_Null_Test() { 236 | Profiler.BeginSample("Get Component By Type Null"); 237 | for ( int i = 0; i < Tries; i++ ) { 238 | #pragma warning disable CS0219 239 | var item = GetComponent(typeof(Test1)) as Test1; 240 | #pragma warning restore CS0219 241 | } 242 | Profiler.EndSample(); 243 | } 244 | 245 | void DirectsS_Test() { 246 | Profiler.BeginSample("Get Components By Type"); 247 | for ( int i = 0; i < Tries; i++ ) { 248 | _counter += (GetComponents(typeof(TestComponent))[0] as TestComponent).Vector.x; 249 | } 250 | Profiler.EndSample(); 251 | } 252 | 253 | void DirectsS_Null_Test() { 254 | Profiler.BeginSample("Get Components By Type Null"); 255 | for ( int i = 0; i < Tries; i++ ) { 256 | #pragma warning disable CS0219 257 | var items = GetComponents(typeof(Test1)); 258 | #pragma warning restore CS0219 259 | } 260 | Profiler.EndSample(); 261 | } 262 | 263 | void LocalCacheVariable_Test() { 264 | Profiler.BeginSample("Local Variable"); 265 | for ( int i = 0; i < Tries; i++ ) { 266 | _counter += _testVar.Vector.x; 267 | } 268 | Profiler.EndSample(); 269 | } 270 | 271 | void LocalCacheProperty_Test() { 272 | Profiler.BeginSample("Cached Property"); 273 | for ( int i = 0; i < Tries; i++ ) { 274 | _counter += TestProp.Vector.x; 275 | } 276 | Profiler.EndSample(); 277 | } 278 | 279 | void LocalCachePropertyConv_Test() { 280 | Profiler.BeginSample("Cached Property Converted"); 281 | for ( int i = 0; i < Tries; i++ ) { 282 | _counter += TestPropConv.Vector.x; 283 | } 284 | Profiler.EndSample(); 285 | } 286 | 287 | void LocalCachePropertyChecked_Test() { 288 | Profiler.BeginSample("Cached Property Checked"); 289 | for ( int i = 0; i < Tries; i++ ) { 290 | _counter += TestPropChecked.Vector.x; 291 | } 292 | Profiler.EndSample(); 293 | } 294 | 295 | void ExternalCacheSimple_Test() { 296 | Profiler.BeginSample("Static Extension Cache Concrete"); 297 | for ( int i = 0; i < Tries; i++ ) { 298 | _counter += gameObject.GetCachedTestComponent().Vector.x; 299 | } 300 | Profiler.EndSample(); 301 | } 302 | 303 | void ExternalCacheTemplate_Test() { 304 | Profiler.BeginSample("Static Extension Cache"); 305 | for ( int i = 0; i < Tries; i++ ) { 306 | _counter += gameObject.GetCachedComponent().Vector.x; 307 | } 308 | Profiler.EndSample(); 309 | } 310 | 311 | void InnerCacheTemplate_Test() { 312 | Profiler.BeginSample("CacheComponent Cache"); 313 | for ( int i = 0; i < Tries; i++ ) { 314 | _counter += Cache.Get().Vector.x; 315 | } 316 | Profiler.EndSample(); 317 | } 318 | 319 | void InnerCacheAttribute_Test() { 320 | Profiler.BeginSample("Attribute Cache Cocrete"); 321 | for ( int i = 0; i < Tries; i++ ) { 322 | _counter += AttrCache.Test.Vector.x; 323 | } 324 | Profiler.EndSample(); 325 | } 326 | 327 | void InnerCacheAttributeInherit_Test() { 328 | Profiler.BeginSample("Attribute Cache Inherit"); 329 | for ( int i = 0; i < Tries; i++ ) { 330 | _counter += TestInh.Test.Vector.x; 331 | } 332 | Profiler.EndSample(); 333 | } 334 | } 335 | } 336 | -------------------------------------------------------------------------------- /Assets/TestScripts/PerformanceChecker.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 51f2937d00abd004da653e354e07b586 3 | timeCreated: 1464868034 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test1.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace TestScripts { 4 | public class Test1 : MonoBehaviour { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test1.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e4ff81252cd4c5245924942e04984959 3 | timeCreated: 1465028256 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test2.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace TestScripts { 4 | public class Test2 : MonoBehaviour { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test2.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2359e67b8e24cf741a52791a7031a74c 3 | timeCreated: 1465028265 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test3.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace TestScripts { 4 | public class Test3 : MonoBehaviour { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test3.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9808c43118c4ceb48beefd7c4127aafa 3 | timeCreated: 1465028273 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test4.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace TestScripts { 4 | public class Test4 : MonoBehaviour { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test4.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 638a4f568c7faf143844878c833ff287 3 | timeCreated: 1465028281 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test5.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace TestScripts { 4 | public class Test5 : MonoBehaviour { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Assets/TestScripts/Test5.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1b2931a3c9b79154985404a53d8f75ec 3 | timeCreated: 1465028288 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/TestComponent.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace TestScripts { 4 | public class TestComponent : MonoBehaviour { 5 | public Vector3 Vector = new Vector3(1, 0, 0); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Assets/TestScripts/TestComponent.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d356819aebd4e2c488b83ced58a8c2cf 3 | timeCreated: 1464868881 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/TestScripts/TestInheritScript.cs: -------------------------------------------------------------------------------- 1 | namespace TestScripts { 2 | public class TestInheritScript : AttributeCacheInherit { 3 | [Cached] 4 | public TestComponent Test; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Assets/TestScripts/TestInheritScript.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5b9eaba2d0d0266499b3f22ff0e6c047 3 | timeCreated: 1465023401 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityCache.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: edc66882a4176f64fbcc86f515d5ca7f 3 | folderAsset: yes 4 | timeCreated: 1465025274 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityCache/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 377e57c70cc284fb28ffb8eb6284e4e0 3 | folderAsset: yes 4 | timeCreated: 1466175500 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityCache/Editor/PreCacheEditor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | using UnityEditor; 6 | using UnityEditor.Callbacks; 7 | 8 | namespace UnityCache { 9 | public static class PreCacheEditor { 10 | public static bool PreCacheOnBuild = true; 11 | public static bool WriteToLog = false; 12 | 13 | [MenuItem("UnityCache/PreCache")] 14 | public static bool PreCacheFromMenu() { 15 | return PreCache(true); 16 | } 17 | 18 | static bool PreCache(bool force) { 19 | bool result = false; 20 | var items = GameObject.FindObjectsOfType(); 21 | foreach ( var item in items ) { 22 | if ( PreCacheAll(item, force) ) { 23 | EditorUtility.SetDirty(item); 24 | if ( WriteToLog ) { 25 | Debug.LogFormat("PreCached: {0} [{1}]", item.name, item.GetType()); 26 | } 27 | result = true; 28 | } 29 | } 30 | return result; 31 | } 32 | 33 | static bool PreCacheAll(MonoBehaviour instance, bool force) { 34 | var type = instance.GetType(); 35 | return CacheFields(instance, GetFieldsToCache(type), force); 36 | } 37 | 38 | static List GetFieldsToCache(Type type) { 39 | var fields = new List(); 40 | foreach ( var field in type.GetFields() ) { 41 | foreach ( var a in field.GetCustomAttributes(false) ) { 42 | if ( a is PreCachedAttribute ) { 43 | fields.Add(field); 44 | } 45 | } 46 | } 47 | return fields; 48 | } 49 | 50 | static bool CacheFields(MonoBehaviour instance, List fields, bool force) { 51 | bool cached = false; 52 | SerializedObject serObj = null; 53 | 54 | var iter = fields.GetEnumerator(); 55 | while ( iter.MoveNext() ) { 56 | if ( serObj == null ) { 57 | serObj = new SerializedObject(instance); 58 | } 59 | var type = iter.Current.FieldType; 60 | var name = iter.Current.Name; 61 | 62 | var property = serObj.FindProperty(name); 63 | if ( !property.objectReferenceValue || force ) { 64 | cached = true; 65 | property.objectReferenceValue = instance.GetComponent(type); 66 | } 67 | if ( WriteToLog ) { 68 | Debug.Log("Cached value: " + property.objectReferenceValue); 69 | } 70 | } 71 | if ( cached ) { 72 | serObj.ApplyModifiedProperties(); 73 | } 74 | return cached; 75 | } 76 | 77 | [PostProcessScene()] 78 | static void OnPostProcessScene() { 79 | if ( PreCacheOnBuild && PreCache(false) ) { 80 | Debug.LogWarning("You have non-cached objects on scenes. Now that objects cached before build, but changes could not be saved. Use UnityCache/PreCache command to cache it."); 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Assets/UnityCache/Editor/PreCacheEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b1dfc13f5e8d74ace87393829f780e59 3 | timeCreated: 1466175510 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityCache/Examples.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 318fa6535f8804514a0cb3310cb5de75 3 | folderAsset: yes 4 | timeCreated: 1466176269 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityCache/Examples/CacheExample.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | // Add cache dependency 3 | using UnityCache; 4 | 5 | public class CacheExample : MonoBehaviour { 6 | // This members would be initialized on load 7 | // Only public members 8 | [Cached] 9 | public Transform MyTransform = null; 10 | // But you can cache hide-in-inspector objects 11 | [HideInInspector] 12 | [Cached] 13 | public Rigidbody MyRigidbody = null; 14 | 15 | void Awake() { 16 | // Cache all [Cached] members 17 | UCache.CacheAll(this); 18 | } 19 | 20 | void Start() { 21 | // Now we can use cached items 22 | var pos = MyTransform.position; 23 | } 24 | 25 | void FixedUpdate() { 26 | // Even hidden one 27 | MyRigidbody.AddForce(Vector3.forward * 10 * Time.fixedDeltaTime); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Assets/UnityCache/Examples/CacheExample.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 248bdbe93efcdac4d96a1f365b90f829 3 | timeCreated: 1466092669 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityCache/Examples/PreCacheExample.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | // Add cache dependency 3 | using UnityCache; 4 | 5 | public class PreCacheExample : MonoBehaviour { 6 | // This members would be cached in scene objects after execute UnityCache/PreCache command in menu 7 | // Only public members 8 | [PreCached] 9 | public Transform MyTransform = null; 10 | // But you can cache hide-in-inspector objects 11 | [HideInInspector] 12 | [PreCached] 13 | public Rigidbody MyRigidbody = null; 14 | 15 | void Awake() { 16 | // No field initialization on load! 17 | } 18 | 19 | void Start() { 20 | // Now we can use cached items 21 | var pos = MyTransform.position; 22 | } 23 | 24 | void FixedUpdate() { 25 | // Even hidden one 26 | MyRigidbody.AddForce(Vector3.forward * 10 * Time.fixedDeltaTime); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Assets/UnityCache/Examples/PreCacheExample.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 87f2aa0a8435d42b69eca763fbd160ca 3 | timeCreated: 1466175262 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityCache/Examples/_Example.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/Assets/UnityCache/Examples/_Example.unity -------------------------------------------------------------------------------- /Assets/UnityCache/Examples/_Example.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c2531d3c6a84188429b3a3d3dc69337d 3 | timeCreated: 1466094063 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityCache/LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016-2018 Konstantin Khitrykh (KonH) 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 | -------------------------------------------------------------------------------- /Assets/UnityCache/LICENSE.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d520f9509003f48269caa31954c2edee 3 | timeCreated: 1466223989 4 | licenseType: Free 5 | TextScriptImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityCache/README.txt: -------------------------------------------------------------------------------- 1 | UnityCache 2 | Version: 0.32 3 | 4 | Simple scripts to cache your components using attributes. 5 | You can do it at runtime with some performance issues and in editor without it. 6 | 7 | Runtime Example: 8 | Components loaded using [Cached] attribute and you do not want to call GetComponent() on each one. 9 | See Examples/CacheExample.cs 10 | 11 | Editor Example: 12 | Components loaded in Editor (before application start) and saved to instance variables. 13 | See Examples/PreCacheExample.cs 14 | 15 | License: MIT (see LICENSE.txt beside) 16 | -------------------------------------------------------------------------------- /Assets/UnityCache/README.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f14a26845d9dc43179851a79d49b655f 3 | timeCreated: 1466223989 4 | licenseType: Free 5 | TextScriptImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fb70da030f24b4628b96029c5204f314 3 | folderAsset: yes 4 | timeCreated: 1466176262 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/Cached.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace UnityCache { 4 | /// 5 | /// Experimental feature: 6 | /// You can make instance of this component in our script and use it for lazy initialization 7 | /// Pro: 8 | /// - You can cache external dependencies by passing its GameObject to constructor 9 | /// Contra: 10 | /// - You want to explicit create all instances (definition and initialization) 11 | /// 12 | /// Any component 13 | public class Cached where T : Component { 14 | GameObject _target = null; 15 | bool _isCached = false; 16 | T _component = null; 17 | 18 | public Cached(GameObject target) { 19 | _target = target; 20 | } 21 | 22 | public T Value { 23 | get { 24 | if ( !_isCached ) { 25 | _component = _target.GetComponent(); 26 | _isCached = true; 27 | } 28 | return _component; 29 | } 30 | } 31 | 32 | public T SafeValue { 33 | get { 34 | if ( _target ) { 35 | if ( !_component ) { 36 | _component = _target.GetComponent(); 37 | } 38 | return _component; 39 | } 40 | return null; 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/Cached.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 275603284457bc94bbd5066c6e001973 3 | timeCreated: 1466328911 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/CachedAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace UnityCache { 4 | /// 5 | /// Used with Cache.CacheAll() to init instance members with GetComponent 6 | /// 7 | [AttributeUsage(AttributeTargets.Field)] 8 | public class CachedAttribute : Attribute { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/CachedAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 664e42d7da606894a8a90f8a5c778153 3 | timeCreated: 1466092649 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/PreCachedAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace UnityCache { 4 | /// 5 | /// Used it for caching in editor (UnityCache/PreCache) 6 | /// 7 | [AttributeUsage(AttributeTargets.Field)] 8 | public class PreCachedAttribute : Attribute { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/PreCachedAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8b49ea7d4a41041fba58a9a2e51413ca 3 | timeCreated: 1466175357 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/UCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | namespace UnityCache { 7 | public static class UCache { 8 | static Dictionary> _cachedTypes = new Dictionary>(); 9 | 10 | /// Init all 'instance' members marked with [Cached] attribute using GetComponent 11 | /// MonoBehaviour instance 12 | public static void CacheAll(MonoBehaviour instance) { 13 | var type = instance.GetType(); 14 | List fields = null; 15 | if ( !_cachedTypes.TryGetValue(type, out fields) ) { 16 | fields = GetFieldsToCache(type); 17 | _cachedTypes[type] = fields; 18 | } 19 | CacheFields(instance, fields); 20 | } 21 | 22 | static List GetFieldsToCache(Type type) { 23 | var fields = new List(); 24 | foreach ( var field in type.GetFields() ) { 25 | foreach ( var a in field.GetCustomAttributes(false) ) { 26 | if ( a is CachedAttribute ) { 27 | fields.Add(field); 28 | } 29 | } 30 | } 31 | return fields; 32 | } 33 | 34 | static void CacheFields(MonoBehaviour instance, List fields) { 35 | var iter = fields.GetEnumerator(); 36 | while ( iter.MoveNext() ) { 37 | var type = iter.Current.FieldType; 38 | iter.Current.SetValue(instance, instance.GetComponent(type)); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Assets/UnityCache/Scripts/UCache.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 829ed829108af4f2abf9ec44fb86ef58 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Assets/unity-cache-0.32.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/Assets/unity-cache-0.32.unitypackage -------------------------------------------------------------------------------- /Assets/unity-cache-0.32.unitypackage.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6695d0db5f4e68746aad7bf4a76ac435 3 | timeCreated: 1466331055 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Konstantin Khitrykh (KonH) 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 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/PresetManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/PresetManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 2018.1.6f1 2 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KonH/UnityCache/6be9f033ae0f0b8a1634a5c8152df15fbf28b5e1/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityCache 2 | ## Version: 0.40 3 | 4 | ## Release notes 5 | ### 0.40 6 | 1. Migrate to Unity 2018.1.6f1 7 | 2. Refactoring 8 | ### 0.32 9 | 1. Experimental cache component added 10 | 2. Warning on scene build (if found uncached components) added 11 | 12 | ## Overview 13 | 14 | Simple scripts to cache your components using attributes. 15 | You can do it at runtime with some performance issues and in editor without it. 16 | 17 | ### Runtime Example: 18 | Components loaded using [Cached] attribute and you do not want to call GetComponent() on each one. 19 | 20 | using UnityEngine; 21 | // Add cache dependency 22 | using UnityCache; 23 | 24 | public class CacheExample : MonoBehaviour { 25 | // This members would be initialized on load 26 | // Only public members 27 | [Cached] 28 | public Transform MyTransform = null; 29 | // But you can cache hide-in-inspector objects 30 | [HideInInspector] 31 | [Cached] 32 | public Rigidbody MyRigidbody = null; 33 | 34 | void Awake() { 35 | // Cache all [Cached] members 36 | UCache.CacheAll(this); 37 | } 38 | 39 | void Start () { 40 | // Now we can use cached items 41 | var pos = MyTransform.position; 42 | } 43 | 44 | void FixedUpdate () { 45 | // Even hidden one 46 | MyRigidbody.AddForce(Vector3.forward * 10 * Time.fixedDeltaTime); 47 | } 48 | } 49 | 50 | ### Editor Example: 51 | Components loaded in Editor (before application start) and saved to instance variables. 52 | 53 | using UnityEngine; 54 | // Add cache dependency 55 | using UnityCache; 56 | 57 | public class PreCacheExample : MonoBehaviour { 58 | // This members would be cached in scene objects after execute UnityCache/PreCache command in menu 59 | // Only public members 60 | [PreCached] 61 | public Transform MyTransform = null; 62 | // But you can cache hide-in-inspector objects 63 | [HideInInspector] 64 | [PreCached] 65 | public Rigidbody MyRigidbody = null; 66 | 67 | void Awake() { 68 | // No field initialization on load! 69 | } 70 | 71 | void Start () { 72 | // Now we can use cached items 73 | var pos = MyTransform.position; 74 | } 75 | 76 | void FixedUpdate () { 77 | // Even hidden one 78 | MyRigidbody.AddForce(Vector3.forward * 10 * Time.fixedDeltaTime); 79 | } 80 | } 81 | 82 | ## LICENSE 83 | MIT (see LICENSE.txt beside) 84 | --------------------------------------------------------------------------------