{"record":{"id":"37a409d7592d2f4a","repo":"EllanJiang/GameFramework","slug":"you-must-set-decrypt-resource-callback-before-add-load","errorCode":null,"errorMessage":"You must set decrypt resource callback before add load resource agent helper.","messagePattern":"You must set decrypt resource callback before add load resource agent helper\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Resource/ResourceManager.cs","lineNumber":1035,"sourceCode":"                m_VersionListProcessor.SetDownloadManager(downloadManager);\n            }\n\n            if (m_ResourceUpdater != null)\n            {\n                m_ResourceUpdater.SetDownloadManager(downloadManager);\n            }\n        }\n\n        /// <summary>\n        /// 设置解密资源回调函数。\n        /// </summary>\n        /// <param name=\"decryptResourceCallback\">要设置的解密资源回调函数。</param>\n        /// <remarks>如果不设置，将使用默认的解密资源回调函数。</remarks>\n        public void SetDecryptResourceCallback(DecryptResourceCallback decryptResourceCallback)\n        {\n            if (m_ResourceLoader.TotalAgentCount > 0)\n            {\n                throw new GameFrameworkException(\"You must set decrypt resource callback before add load resource agent helper.\");\n            }\n\n            m_DecryptResourceCallback = decryptResourceCallback;\n        }\n\n        /// <summary>\n        /// 设置资源辅助器。\n        /// </summary>\n        /// <param name=\"resourceHelper\">资源辅助器。</param>\n        public void SetResourceHelper(IResourceHelper resourceHelper)\n        {\n            if (resourceHelper == null)\n            {\n                throw new GameFrameworkException(\"Resource helper is invalid.\");\n            }\n\n            if (m_ResourceLoader.TotalAgentCount > 0)\n            {","sourceCodeStart":1017,"sourceCodeEnd":1053,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Resource/ResourceManager.cs#L1017-L1053","documentation":"Thrown by ResourceManager.SetDecryptResourceCallback if load-resource agent helpers have already been added (m_ResourceLoader.TotalAgentCount > 0). The decrypt callback is applied to agent helpers at creation time, so setting it afterwards is too late; the library forbids the late assignment.","triggerScenarios":"Calling AddLoadResourceAgentHelper (directly or via ResourceComponent helpers) before calling SetDecryptResourceCallback with a custom callback.","commonSituations":"Unity execution-order issues where ResourceComponent.Start adds agent helpers before a script sets the callback; forgetting that helper registration happens in Awake/Start.","solutions":["Call SetDecryptResourceCallback before any AddLoadResourceAgentHelper call, e.g. in Awake with a lower Script Execution Order","Use [DefaultExecutionOrder] or Project Settings > Script Execution Order to force the callback setup script to run first","If no custom decryption is needed, skip SetDecryptResourceCallback entirely (a default callback is used)"],"exampleFix":"// before\nresourceManager.AddLoadResourceAgentHelper(helper);\nresourceManager.SetDecryptResourceCallback(MyDecrypt); // throws\n// after\nresourceManager.SetDecryptResourceCallback(MyDecrypt);\nresourceManager.AddLoadResourceAgentHelper(helper);","handlingStrategy":"validation","validationCode":"if (resourceManager.TotalAgentCount == 0) resourceManager.SetDecryptResourceCallback(myDecrypt); else throw new InvalidOperationException(\"Set decrypt callback before adding agent helpers\");","typeGuard":"bool CanSetDecryptCallback(ResourceManager rm) => rm.TotalAgentCount == 0;","tryCatchPattern":"try { resourceManager.SetDecryptResourceCallback(myDecrypt); } catch (GameFrameworkException ex) { Debug.LogError($\"Call SetDecryptResourceCallback before AddLoadResourceAgentHelper: {ex.Message}\"); }","preventionTips":["Set all callbacks before adding any agent helpers","Control Unity script execution order ([DefaultExecutionOrder])","Note the default decrypt callback exists if no custom logic is needed"],"tags":["resource","gameframework","call-order","initialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}