Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question RuntimeInitializeLoadType in dll can't run automatically in unity 2022

Discussion in 'Scripting' started by pyjenkins, Jul 25, 2023.

  1. pyjenkins

    pyjenkins

    Joined:
    Aug 9, 2022
    Posts:
    3
    RuntimeInitializeLoadType.BeforeSceneLoad and
    RuntimeInitializeLoadType.AfterSceneLoad in dll can't run automatically in unity 2022, it can run in unity 2019, is there something new to limit code auto run that in dll for untiy 2022?
     
  2. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    280
    I just tested my project which depends on a static method in a DLL with the [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] attribute in Unity 2022.3.0, and I had no issues. I made a build with Mono, and with IL2CPP, and all worked as expected in editor and in both builds.
     
    Bunny83 likes this.
  3. pyjenkins

    pyjenkins

    Joined:
    Aug 9, 2022
    Posts:
    3
    It is weird, the attribute is on a public static method, and in code mode it's no problem. When exported as dll, it is not running. And no any error output.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    The next obvious test to me would be to make a C# method with that RTILT attribute, see if the method gets called.

    Reimport all is also a great step.

    And ultimately I suppose you could call the DLL one from your C# one if that works. Yuck... :)

    I hate putting code in DLLs anyway, it just slows down my debugging. YMMV.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Another thought: I wonder if being in a DLL causes slightly-different code stripping rules to be applied when you build, and those rules are stripping your static method / class.

    Try just putting a reference to that static method inside something else you know builds in your project. That should stop stripping from happening.
     
    Bunny83 and CodeRonnie like this.
  6. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    280
    That's a good thought about code stripping. I wonder if it's to blame.
     
  7. pyjenkins

    pyjenkins

    Joined:
    Aug 9, 2022
    Posts:
    3
    It still does't work for me. My code is in Assembly-CSharp.dll, no a independent assembly definition. I run the dll on unity editor,just copy the dll file from another project with the code. And I check that the code is in the dll file.
    I found in unity 2019 I can rename the dll file, but in unity 2022 I can't do it. If I rename the dll, there is a Assembly name 'Assembly-CSharp' does not match file name error.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Sounds like maybe there's some internal name that has to match the outer name??

    The other thing to check is that you marked the DLL to be included in the particular target you are building.