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

I have a question for AddComponent

Open yunli97 opened this issue 1 year ago • 4 comments

I created a new GameObject and tried to call AddComponent[new_GameObject].Call(MyClass::BNMCustomClass.type);. The following code can run correctly: image However, when I used a thread to load the initialization without changing anything else, the program crashed when executing AddComponent. image There were no crash logs.

What is this causing AddComponent to crash?

yunli97 avatar Aug 05 '24 09:08 yunli97

I changed sleep(1) to usleep(1) and it works again.

yunli97 avatar Aug 05 '24 10:08 yunli97

Calling unity methods outside of main thread is dangerous. This is reason of crash. And you can't call it using AddOnLoadEvent too, this event is too early. Hook sth, like Internal_SceneLoaded and create object here.

BNM-Dev avatar Aug 05 '24 11:08 BNM-Dev

yes, I hook AdsManager(MonoBehaviour) .ctor method in OnLoaded_SomeMethod,and create object here. image Does this still count as calling a unity method outside of the main thread?

yunli97 avatar Aug 06 '24 02:08 yunli97

I tried to use AttachIl2Cpp(),but this still crash ,when Code executed to AddComponent[new_GameObject].Call(MyClass::BNMCustomClass.type);

image image

From the logs, it seems that he is on the main thread

yunli97 avatar Aug 06 '24 03:08 yunli97

Try use BNM v2.1.0

BNM-Dev avatar Sep 07 '24 16:09 BNM-Dev

Running it in a sub-thread causes all sorts of weird problems, and I've given up on it

yunli97 avatar Sep 25 '24 10:09 yunli97