Ksframework knowledge
Https://github.com/mr-kelly/KSFramework
Ksframework is a full-featured Unity 5 development framework that integrates kengine, Slua, and a number of development components for teams of a certain size.
Hot Reload is the focus of development of ksframework--without restarting the game, the overloaded code, configuration table can immediately see the effect of modification, maximize the speed of development, debugging, and in the operation phase convenient product hot update.
Look at demo!.
Double-tap Open assets/game.unity scene and click Play.
Figure: Log output after starting Game.unity
Ksframework did these things.
This time Ksframework's default demo starts and does these things:
- Startup of the Base module
- Launch of the LUA module
- Try to read and print the Excel table gameconfig.xlsx content
- Load UI Interface Login
- The Lua script that executes the UI interface login
- Lua scripts bind UI controls, set UI controls
- Lua script reads and prints Excel table gameconfig.xlsx content
Includes the use of ksframework core modules
In summary, this demo includes the use of several core modules in Ksframework:
- Use of the setting module in Kengine
- Resource loading for UI in Kengine
- Slua scripting engine and UI Union
Next, you will emulate this demo, create/load a new UI interface, create/read a new configuration table.
Try to make an announcement interface billboard
Next, we'll create a UI bulletin Board (Billboard), use a LUA script, and read the announcement content from the configuration table.
1. Create a UI Resource
Figure: Creating a new Scene
Create UI
Figure:kengine – UI -Create UI creating UI Layout
Figure: After clicking the Create UI, the default random name changes the UI name to Billboard
Figure: Modify UI name with Yellow UI logo on the right of the Billboard,ui interface
Figure: Editing a UI scene, a background image, two labels
Save the scene and save it to assets/bundleediting/ui/billboard.unity
Export UI to Assetbundle
Figure: Export-Package assetbundle, shortcut key ctrl+alt+e
2. Loading the UI interface
Well, the Billboard interface has been created and exported to Assetbundle.
Show UI Panel (Openwindow mode)
Next, we open the interface through the code.
code example
Edit Assets/Code/Game.cs
At the end of the onfinishinitmodules function, add such a sentence:
// Start loading our announcement screen! UIModule.Instance.OpenWindow ("Billboard");
Complete and save.
Run effect
Open the scene assets/game.unity, click the play button:
Our UI is open via Assetbundle, and the UI Lua script is not found, so let's create a LUA script.
3. Create a LUA script
Figure: Create a new Lua file in the catalog Product/lua/UI
Lua Script Example
Write a LUA code: Uibillboard Execution Logic
LocalUibase = Import ("Ksframework/uibase")LocalUibillboard ={}extends (Uibillboard, uibase)functionUibillboard:oninit (Controller) self. Controller=controller self. Titlelabel= Self:getuitext ('Title') self. Contentlabel= Self:getuitext ('Content')EndfunctionUibillboard:onopen () self. Titlelabel.text="This is a title"Self . Contentlabel.text="Here is content!"EndreturnUibillboard
In this LUA, a table called Uibillboardis created, and the table must have the OnInit (Controller) function. It sets the text in the UI through the code.
Well, next, we're going to prepare the configuration table for the plan.
4. Create a configuration table
Open the Product/settingsource directory, duplicate a copy of the stringstable.xlsx, and change the name to billboard.xlsx .
Announcement requirements
Open our new billboard.xlsxwith Excel and edit our announcements.
We will probably set the demand, we assume that write 3 announcements, each open the announcement randomly display one. Each bulletin, we give it an English ID, a list of Chinese titles, a list of Chinese content.
Configure Announcements for Excel
The Excel table is modified as follows: (Add announcement content)
Unity automatically monitors Excel changes
Figure: Back to unity, monitor changes to Excel. Click OK.
Automatically generate configuration table code
The previous step monitors for changes, compiles only Excel tables, manually performs some recompilation, and generates configuration table code
tab-generated Billboardsettings class
Figure: This time, open the AppSettings.cs code file, we can find that has generated a class called Billboardsettings.
Generate static code for Slua
Because we're going to use billboardsettings to read the configuration table in Lua, we need to regenerate the static code for Slua.
Modify LUA to randomly read an announcement
Next, modify the LUA code, randomly read an announcement, and set the content, Title
Announcement content randomly displayed
Figure: Run play game.unity, our announcement interface is complete
The announcement interface is complete. We created a UI, wrote C # and LUA code to load it, then created a configuration table, read the configuration table from Lua, and set the display of the UI.
Play Hot heavy load hot reload Lua
Then we just ran the game.unity . Let's try the LUA hot reload: quickly reload the LUA code without restarting the game to facilitate program debugging .
Figure: Menu ksframework –> UI –> reload+reopen: Hot reload Lua script and reopen UI interface that is currently open
We can perform a hot reload from the menu and reopen the UI, or use the shortcut key ctrl+alt+shift+r.
Because of our LUA script, each execution randomly gets an announcement. So you can see the content of the announcement is constantly changing.
Principle of thermal overload implementation
The implementation of hot overloading relies primarily on the last line of each LUA script--return the table;c# layer, after executing the LUA script, caches the returned table and takes it directly from the cache each time it needs to use table, while the hot reload actually deletes the cache table. , so that when this table is needed, the Lua file is re-executed to fetch the table, thus achieving the purpose of hot overloading.
Hot Reload Excel table
We keep running just the game.unity, don't stop. At this point we go to modify the Excel table.
Figure: Modifying an Excel table
Modifying the configuration table and overloading
Once saved, return to unity, prompting for changes to the table.
Figure: Find table changes, click OK to compile the table
Figure: Reload the Configuration table from the menu center.
Overloading effect
Figure: Ctrl+alt+shift+r refreshing Lua
Overloaded Lua, our newly modified configuration table content is in effect.
At this point, our LUA and configuration table changes can be quickly modified without restarting and not re-turning the chrysanthemums.
Copyright notice
(kelly[mr-kelly) (author of Jane's book)
Original link: http://www.jianshu.com/p/ccb491ed4260
Copyright belongs to the author, please contact the author for authorization.
Welcome to GitHub to mention issues.
Ksframework:unity3d Development Framework Quick Start