“This is the third day of my participation in the First Challenge 2022, for more details: First Challenge 2022”.
π About the author
As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!
Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)
Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!
π premise
During this period of time, I learned a Unity framework and prepared to apply it to the actual project, but it was difficult for me to learn because the author could not see the end of the framework and did not have any detailed documents. Fortunately, the author opened all the contents of the framework and recorded the learning process again.
π Practice
π Framework step 3 – Event subscription
Since we’re going to hire her, we have to start with her good points:
It simplifies communication between components, separates event senders and receivers, and greatly reduces coupling. Here’s an example:
Recently the real machine demo, black wu empty γ γ such as state of the wu is empty, experience value, monster blood etc, will have corresponding change at any time, such as monster death, characters gain experience and completion of judgment, and so on, an incident occurred more than N events to the corresponding change, if only one active modify you pitch me/I, were transferred to the logical line not chaotic just strange, It’s not like you play bondage. So we’re going to go from active to passive, and I’m going to send out a broadcast, and anyone who wants to listen to me can sign up, and if you don’t want to, it doesn’t matter. It’s a great decoupling effect. Xiao Kong was born in Android, and he is very good at using EventBus subscription in Android, so he learned the subscription events of this framework at the first time
That’s it. If an event changes, the place that did the registration gets the message and handles its own logic. Sent by subscribers, received by multiple subscribers.
Here we go, let’s see how it works;
The first step
You need to get the Event component first
EventComponent eventComponent = UnityGameFramework.Runtime.GameEntry.GetComponent();
The second step
Subscribe to the event
eventComponent.Subscribe(OpenUIFormSuccessEventArgs.EventId, InitUISuccess);
Subscribe to events of the first parameter is the built-in, successful event is to initialize OpenUIFormSuccessEventArgs γ γ opened interface, the second parameter is a function of your statement, after the completion of the interface will trigger the function. There are more built-in functions available, see the chart below for details.
The third step
Loading the UI
The fourth step
Print in the callback function to see if the message was received.
Step 5
Remember to unregister the OnLeave method. Now that you’re out of the process, don’t bother listening for anything else. Otherwise, it will cause memory garbage.
Running results:
A. Is it that simple? Love for the demon of the small empty at this time and have an idea: the actual development can not be so simple, there may be MORE than N UI, more than N time, this if much is what? No hurry. Keep trying
A run found ah callback method ah. This is good, if you use the fourth parameter to pass custom data, then return to do different processing for different data.
666, but there is still a question, look carefully above [OpenUIForm] in the third parameter – priority, I tried to input a different level of running found not as expected [this is a cat] to execute first. Xiao Kong hurried to read the official case, and found that the official case was divided into resource priorities – font, sound, music/scene and other big categories. Should not be used for small equivalents (i.e., both UIs). I’ll leave that for later.
This is similar to EventBus for Android. In the future, I hope to find that the framework also has EventBus sticky events and cross-component monitoring. Add a frame built-in component diagram:
How’s thatοΌ Is it easy to use? As if I heard someone say in that remote mountain village,
π other
π’ author: Kom and Kom in Kom
π’ reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)
π’ welcome to like π collect π message π