BNM-Android icon indicating copy to clipboard operation
BNM-Android copied to clipboard

AssetBundle loading example

Open plastifiedminion opened this issue 1 year ago • 8 comments

Hello, I was wondering if you could provide an example on how to load a AssetBundle using BNM? Doesn't matter if it's by file path or by memory I just want an example on how to load it, thank you!

plastifiedminion avatar Jan 09 '25 02:01 plastifiedminion

It work fully same as Unity's C#. Find any simple C# example and rewrite it to BNM's API. All needed things are included in BNM.

BNM-Dev avatar Jan 12 '25 08:01 BNM-Dev

Thank you, managed to load the asset bundle, but i have an issue (which is probably easy to fix but im not sure how)

I'm trying to register a custom class (monobehaviour) so i can use it on a button in the assetbundle

#ifdef BNM_CLASSES_MANAGEMENT

using namespace BNM::Structures::Unity;
using namespace BNM::Structures::Mono;

struct MobileLeaveLink : public BNM::UnityEngine::MonoBehaviour {

    BNM_CustomClass(MobileLeaveLink,
                    BNM::CompileTimeClassBuilder(nullptr, BNM_OBFUSCATE_TMP("MobileLeaveLink")).Build(),
                    BNM::CompileTimeClassBuilder(BNM_OBFUSCATE_TMP("UnityEngine"), BNM_OBFUSCATE_TMP("MonoBehaviour"), BNM_OBFUSCATE_TMP("UnityEngine.CoreModule")).Build(),
                    {}
    );

    void OpenLeaveLink()
    {
        LOGD("Opening leave link!");
    }

    void Start() {
        LOGD("Mobile link!");
    }

    BNM_CustomMethod(OpenLeaveLink, false, BNM::Defaults::Get<void>(), "OpenLeaveLink");
    BNM_CustomMethod(Start, false, BNM::Defaults::Get<void>(), "Start");
};
#endif

image

and this is the asset bundle side image

Basically when i spawn the button start gets called and it works, but when i press the button the game crashes and it doesn't log anything, is it because i marked the function as public on the asset bundle? how can i mark it as public on bnm? thank you, but its probably not the issue because im pretty sure bnm already marks them as public

plastifiedminion avatar Jan 13 '25 21:01 plastifiedminion

Yes, BNM make all new and added methods as public. Try see unity logs when loading bundle (Tag: Unity). Them can be helpful in fixing such problem. I never tried to use events with BNM, so I don't know why game crash.

BNM-Dev avatar Jan 21 '25 13:01 BNM-Dev

Yes, BNM make all new and added methods as public. Try see unity logs when loading bundle (Tag: Unity). Them can be helpful in fixing such problem. I never tried to use events with BNM, so I don't know why game crash.

i also tried to use an custom behaviour like the op of issue and it also crashed when the event got called, there is no crash log it just crashes

NOTPIES avatar Jan 22 '25 02:01 NOTPIES

Yes, BNM make all new and added methods as public. Try see unity logs when loading bundle (Tag: Unity). Them can be helpful in fixing such problem. I never tried to use events with BNM, so I don't know why game crash.

i also tried to use an custom behaviour like the op of issue and it also crashed when the event got called, there is no crash log it just crashes

Unity won't log anything on crash, but should write to logs on object loading. It can Warn if sth not found or not right.

BNM-Dev avatar Jan 22 '25 14:01 BNM-Dev

Yes, BNM make all new and added methods as public. Try see unity logs when loading bundle (Tag: Unity). Them can be helpful in fixing such problem. I never tried to use events with BNM, so I don't know why game crash.

i also tried to use an custom behaviour like the op of issue and it also crashed when the event got called, there is no crash log it just crashes

Unity won't log anything on crash, but should write to logs on object loading. It can Warn if sth not found or not right.

yeah unity logs that it tries to load the asset bundle, it loads, loading resources from the bundle works too, but when it comes to using custom components made with BNM only start gets called and if you try to use an event it crashes the game without any log

NOTPIES avatar Jan 22 '25 20:01 NOTPIES

I use bundles only for adding some objects for game, so never tested events. Later I will do tests with events and bundles.

BNM-Dev avatar Jan 24 '25 11:01 BNM-Dev

I made same setup and everything is working as intended. No any crashes. Events happening on button click.

Unity editor version (where bundle was build): 2020.3.48f1 Game's unity version: 2021.3.38f1 BNM version: 2.3.0 dev (has no major differences from 2.2.8 and will be released soon).

Unity side: Image

TestScript.cs:

using UnityEngine;

public class TestScript : MonoBehaviour {
    public void OpenLeaveLink() {}
}

BNM side:

struct TestScript : public BNM::UnityEngine::MonoBehaviour {
    BNM_CustomClass(TestScript, BNM::CompileTimeClassBuilder(nullptr, BNM_OBFUSCATE_TMP("TestScript")).Build(), 
    BNM::Defaults::Get<BNM::UnityEngine::MonoBehaviour *>(), // Defaults are significantly faster than CompileTimeClassBuilder
    {});

    void OpenLeaveLink() { LOGI("Opening leave link!"); }
    void Start() { LOGI("Mobile link!"); }

    BNM_CustomMethod(OpenLeaveLink, false, BNM::Defaults::Get<void>(), "OpenLeaveLink");
    BNM_CustomMethod(Start, false, BNM::Defaults::Get<void>(), "Start");
};

Logs: Image

Maybe there is a bug in some special version of unity? @plastifiedminion, @NOTPIES can you both provide details about unity versions and games used to check this thing?

BNM-Dev avatar Mar 31 '25 15:03 BNM-Dev

I made same setup and everything is working as intended. No any crashes. Events happening on button click.

Unity editor version (where bundle was build): 2020.3.48f1 Game's unity version: 2021.3.38f1 BNM version: 2.3.0 dev (has no major differences from 2.2.8 and will be released soon).

Unity side: Image

TestScript.cs:

using UnityEngine;

public class TestScript : MonoBehaviour { public void OpenLeaveLink() {} } BNM side:

struct TestScript : public BNM::UnityEngine::MonoBehaviour { BNM_CustomClass(TestScript, BNM::CompileTimeClassBuilder(nullptr, BNM_OBFUSCATE_TMP("TestScript")).Build(), BNM::Defaults::Get<BNM::UnityEngine::MonoBehaviour *>(), // Defaults are significantly faster than CompileTimeClassBuilder {});

void OpenLeaveLink() { LOGI("Opening leave link!"); }
void Start() { LOGI("Mobile link!"); }

BNM_CustomMethod(OpenLeaveLink, false, BNM::Defaults::Get<void>(), "OpenLeaveLink");
BNM_CustomMethod(Start, false, BNM::Defaults::Get<void>(), "Start");

}; Logs: Image

Maybe there is a bug in some special version of unity? @plastifiedminion, @NOTPIES can you both provide details about unity versions and games used to check this thing?

the unity version is Unity : Built from '2020.3/staging' branch, Version '2020.3.38f1 (8f5fde82e2dc)', Build type 'Release', Scripting Backend 'il2cpp', CPU 'arm64-v8a', Stripping 'Enabled'

NOTPIES avatar May 25 '25 01:05 NOTPIES

05-24 21:24:21.786 1004 1004 I Zygote : Process 4408 exited due to signal 11 (Segmentation fault) the only log related to it

NOTPIES avatar May 25 '25 01:05 NOTPIES

I got this crash and fixed it in BNM v2.4.0 dev. I will try release v2.4.0 in next few days.

BNM-Dev avatar May 30 '25 15:05 BNM-Dev

I got this crash and fixed it in BNM v2.4.0 dev. I will try release v2.4.0 in next few days.

Thank you, I also wanted to ask if BNM supports setting the fields from a custom behaviour from a bundle like in here (Il2CppReferenceField): https://github.com/BepInEx/Il2CppInterop/pull/24

basically for example set an image on a field (asset bundle), field that we make using custom behaviour and then getting the image from c++ side

NOTPIES avatar May 30 '25 22:05 NOTPIES

I got this crash and fixed it in BNM v2.4.0 dev. I will try release v2.4.0 in next few days.

Thank you, I also wanted to ask if BNM supports setting the fields from a custom behaviour from a bundle like in here (Il2CppReferenceField): BepInEx/Il2CppInterop#24

basically for example set an image on a field (asset bundle), field that we make using custom behaviour and then getting the image from c++ side

Yes, BNM fully supports it. I myself used this thing in one of projects.

Unity side:

public class TestScript : MonoBehaviour {
    public string refString;
    public GameObject refGameObject;
    public long longValue;
}

BNM side:

class TestScript : BNM::UnityEngine::MonoBehaviour {
    BNM_CustomClass(TestScript, BNM::CompileTimeClassBuilder({}, "TestScript").Build(), BNM::Defaults::Get<BNM::UnityEngine::MonoBehaviour *>(), {});
    Mono::String *someString{};
    void *refGameObject{};
    long longValue{};
    // Type and name should be same as in unity script
    BNM_CustomField(someString, BNM::Defaults::Get<Mono::String *>(), "refString" /*Unity will check only this name, so in ++ it can be named whatever you want*/);
    BNM_CustomField(refGameObject, BNM::CompileTimeClassBuilder("UnityEngine", "GameObject", "UnityEngine.CoreModule.dll").Build(), "refGameObject");
    BNM_CustomField(longValue, BNM::Defaults::Get<long>(), "longValue");
};

BNM-Dev avatar May 31 '25 07:05 BNM-Dev