unity build_error ai_generated true

IL2CPP error: System.MissingMethodException: Method not found: 'Void UnityEngine.UI.Image::set_sprite(UnityEngine.Sprite)'

ID: unity/il2cpp-missing-method

Also available as: JSON · Markdown · 中文
78%Fix Rate
85%Confidence
1Evidence
2023-08-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2021.3.30f1 active
2022.3.10f1 active
6000.0.0b12 active

Root Cause

IL2CPP code stripping removes a method that is referenced via reflection or a code path not visible to the linker.

generic

中文

IL2CPP 代码剥离删除了通过反射或链接器不可见的代码路径引用的方法。

Official Documentation

https://docs.unity3d.com/Manual/IL2CPP-ErrorTroubleshooting.html

Workarounds

  1. 85% success Add [Preserve] attribute to the specific method: `[Preserve] private void SetSpriteMethod(Sprite s) { image.sprite = s; }`
    Add [Preserve] attribute to the specific method: `[Preserve] private void SetSpriteMethod(Sprite s) { image.sprite = s; }`
  2. 92% success Create a link.xml file in Assets/Plugins with: <linker><assembly fullname="UnityEngine.UI"><type fullname="UnityEngine.UI.Image" preserve="all"/></assembly></linker>
    Create a link.xml file in Assets/Plugins with: <linker><assembly fullname="UnityEngine.UI"><type fullname="UnityEngine.UI.Image" preserve="all"/></assembly></linker>
  3. 75% success Use `#if !UNITY_EDITOR` to force a direct call to the method in a startup script so IL2CPP sees it
    Use `#if !UNITY_EDITOR` to force a direct call to the method in a startup script so IL2CPP sees it

中文步骤

  1. Add [Preserve] attribute to the specific method: `[Preserve] private void SetSpriteMethod(Sprite s) { image.sprite = s; }`
  2. Create a link.xml file in Assets/Plugins with: <linker><assembly fullname="UnityEngine.UI"><type fullname="UnityEngine.UI.Image" preserve="all"/></assembly></linker>
  3. Use `#if !UNITY_EDITOR` to force a direct call to the method in a startup script so IL2CPP sees it

Dead Ends

Common approaches that don't work:

  1. 65% fail

    Preserve only works on specific methods or fields, not entire files; may not cover all reflection paths.

  2. 40% fail

    Disabling stripping increases build size significantly and may hide but not fix the underlying reflection issue.

  3. 90% fail

    The issue is in IL2CPP code generation, not asset import; rebuild doesn't change stripping behavior.