Search Unity

Swap Existing Asset with one from bundle

Discussion in 'Asset Bundles' started by knackname, Mar 16, 2021.

  1. knackname

    knackname

    Joined:
    Mar 14, 2021
    Posts:
    3
    I'm writing a mod for a unity game, and I have scripts running and I've figured out how to load in custom asset bundles but now I'd like to be able to swap an existing element from resources with my custom one. I've written this:
    Code (CSharp):
    1.  
    2. var assetBundle = AssetBundle.LoadFromFile(path);
    3.  
    4. var newLogo = assetBundle.LoadAsset<Texture2D>("logo_modded");
    5.  
    6. foreach(var img in Resources.FindObjectsOfTypeAll<Image>())
    7. {
    8.   if(img.name == "Logo")
    9.   {
    10.     img.sprite = Sprite.Create(newLogo, img.sprite.rect, new Vector2(0,0));
    11.     break;
    12.   }
    13. }
    14.  
    Which has the result I want, the Image's sprite's texture is now my custom one, but is obviously coded horribly.

    A) How do I locate an exsisting resource without having to iterate or linq my way through a giant collection?
    B) How do I replace that resource with my own for the purposes of the rest of the program. I want to redirect the reference to my loaded asset.
    C) At some point I may want to do things like replace scenes, is that possible? Help