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

[Resolved-Sorta-Whatever] Component from dll in assetbundle isn't recognized when loaded in app

Discussion in 'Scripting' started by UnbridledGames, Apr 30, 2021.

  1. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    I'm working on a mod for a game in my spare time (not my game, modding it for fun). I've designed a few prefabs in Unity, and packaged them up inside some arrays, referenced in a component on a GameObject.

    Simple example:
    I have 2 Item prefabs and 2 Recipe prefabs.

    I have a simple class:
    public class AssetContainer : MonoBehavior {
    public ItemData[] items;
    public RecipeData[] recipes;
    }


    This AssetContainer is on a GameObject, and the 2 items and 2 recipes are are referenced in there. That GameObject is made into a prefab and saved into an assetbundle.

    The ItemData, RecipeData, and AssetContainer classes are in a .dll that houses the mod. The dll is in the Unity assets folder and works fine.

    The game loads, links in the dll, and other code loads the assetbundle. The assetbundle loads. The GameObject is there and accessable. When I do a GetComponents<Component>() on the gameObject, I get the transform, and I get an object of the type MyNamespace.AssetContainer.

    The weird problem is when I instead do a GetComponents<AssetContainer>() it returns null. It's like the mod knows it's of type AssetContainer... but it's a different AssetContainer? I cannot even cast it to AssetContainer. It fails, invalid cast. Yet, component.GetType() says it's an AssetContainer.

    Whats going on?

    Here's the really weird part. This WAS working. I've been developing mods for this game for a while, adding more and more assets to that AssetContainer object. It's worked for 2+ weeks now. What changed today is I changed how the dll's are loaded. Not sure it matters, but before the main library that loaded the assets was loaded by the modding tool (BepInEx, in case anyone is familiar, from the plugins directory) and today I made a change to make things more modular by loading the dll's on the fly later in the load process.

    I'm totally at a loss at how I can have an AssetContainer that's of type AssetContainer, but cannot be recognized as an AssetContainer. I'm thinking that something else may have changed in the interim? Some other library updated? Some dependency that dictates how components are recognized? Could that cause this? Something else I'm missing?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,755
    Pretty sure they would be linked only by their GUID, which is found in the .meta file for the script.

    Those GUIDs would have to match between projects at a minimum. There would obviously be other requirements like the public fields provided from the asset would need to be named precisely the same.

    Sample meta file for a script... the GUID is underlined in red:

    Screen Shot 2021-04-29 at 5.24.23 PM.png
     
  3. UnbridledGames

    UnbridledGames

    Joined:
    May 12, 2020
    Posts:
    139
    After hours of banging my head against this, I noticed that the earlier loading of the mod used Assembly.LoadFile, and the later code had loaded them through Assembly.Load. Apparently there's enough of a difference there that Unity doesn't see the assembly correctly enough to identify the same exact MonoBehavior? Dunno. Switched the way the assembly is loaded and it works. The reasons why I'm leaving for another day. Lost too many hours on this already.