当前位置:网站首页>Detailed explanation of unity hot update knowledge points and introduction to common solution principles
Detailed explanation of unity hot update knowledge points and introduction to common solution principles
2022-07-07 15:45:00 【Listen to the rain outside the window】
Unity Detailed explanation of hot update knowledge points and introduction of common solution principles
Hot update knowledge points summary
The concept of hot renewal
When the game or software is updated , There is no need to download the client again for installation , But when the application starts ,
Internal resource or code updates .
Advantages of hot renewal
- Quick fix Bug – Avoid downloading the installation package again , Update the game in time Bug
- Reduce the size of the package – Non core resource upload server , Dynamically load remaining resources at runtime
- Change the game quickly " kernel " – Civet for Prince , Bypass audit , Update the game quickly
Hot update solutions
be based on Lua Hot update solution
principle : In essence, it is to use related plug-ins (
xlua,tolua
) Provide a Lua Operating environment ( virtual machine ), by Unity Provide Lua The ability to program , Give Way C# and Lua Can call and access each other .advantage :
- Lua from C Language writing Strong commonality It can run on almost any operating system .
- Lua As a lightweight and compact scripting language , from Lua Virtual machine interpretation execution , Hotter only needs simple file replacement , No need to compile .
- Strong development experience , There are many mature projects and schemes .
shortcoming :
- Unity Native use C#, Use one more door Lua Language will increase the cost of learning and development
- Lua Is a weakly typed, non object-oriented language , When facing large projects, it will not be like traditional OOP Language, layered and modular development , It is easy to cause code structure confusion and maintenance difficulties .
- You need to use plug-ins in C# Provide... In an environment Lua Running environment , And also need to be Lua and C# Provide data communication and related conversion between , inefficiency , It's far better to use native C# Development .
Solution
XLua
The basic principle :
XLua
The foundation of the framework is the implementation of running in C# Environmental Lua Virtual machines make Lua You can talk to C# Realize mutual call and access .C# call Lua principle :Lua from C Language implementation comes with C/C++ Communication mechanism interface ,C# Can use C And Lua Data communication , Data exchange in memory . Data references directly obtained from memory need to be further converted into C# Can be used ,
xLua
staycastersMap
It defineslua
Data type to C# Type conversion function , After conversion, it can be realized C# call Lua.Lua call C# principle : The principle of data communication is the same as above , Simple basic value types pass C API Easy delivery , But complex objects require adopt Lua Of
userdata
Table and C# Object mapping implementation , Every C# The objects are all in Lua Corresponding to one inuserdata
surface , thenlua
Would beuserdata
Set meta table , The meta table represents the type information of the actual object , stay C# Object passed to Lua After that, you need to tell the type of object , static state / Member method 、 Properties and other information are registered to Lua after ,Lua In order to correctly call C# Related object functions, etc .Meta table can be understood as C# Medium Type class , all C# Objects have a type object pointer , There is a method table in the type object , Static fields and other information ,
userdata
The table only contains the specific members of the class , You also need a meta table to act as a type object in order to correctly and C# Object mapping succeeded .Hot patch principle : utilize IL Inject , To achieve Lua Function substitution C# Primitive function . The basic principle of implementation is : according to Lua Script hot patch command Generate matching files (
DelegatesGensBridge.cs
) It's inside Lua Hot patch function reference provided , Then according to the annotation of characteristics [Hotfix] etc. , Locate on the objective function Create static variablesDelegateBridge
. When Lua It provides a hot patch function , Then this variable is not empty, otherwise it is empty , Then use this variable to judge the condition , When the implementation is not empty, according to the matching file Lua Function reference to execute , Do not continue to execute the original C# function . If it is empty, execute the original C# Functional logic .
ToLua
- The basic principle :
ToLua
The framework is mainly through Static binding To achieve C# And Lua Interaction between . - C# call Lua principle : Data communication and
xLua
similar , After obtaining the data reference in memory, it also needs to be converted , Change the way andXLua
Somewhat different ,ToLua
RealizedLuaInterface
Established Lua And C# Mapping between , Use C# The type of establisheslua
Basic data model , Then the operation rules are established on this basis . - Lua call C# principle : What is different from other frameworks is the characteristics of its static binding , stay Lua call C# when , It provides a C# file (
CustomSettings.cs
) Show registration C# Components , When starting the virtual machine, it will take advantage ofLuaInterface
Will register C# Components map to Lua On , Including the conversion of basic types 、 Custom type to table Transformation 、 Function conversion , Finally, they are registered to Lua The big G surface (global table) To realize Lua call C#.
- The basic principle :
Two kinds of Lua Comparison of solutions
ToLua
The warm update provided is not perfect ,C# Project transfer Lua Recommended for hot updateXLua
Hot patch for .ToLua
Static binding is used to display the registration in the file, which reduces the number of reflections ,XLua
It provides a series of features and reduces the number of reflections to a certain extent .ToLua
Poor support for generics , There will be a lot of disassembly and assembly process , Low efficiency .XLua
Improved support for generics , Reduce the times of disassembling and assembling the box .
be based on C# Hot update solution
- principle : In essence, use the compiled new DLL Replace the old that needs to be updated DLL. Only code hot update , We need to cooperate with AB The resources of the package are used together with hot updates .
- advantage :
- and Unity All development uses C#, Unified development language , Coding is easier .
- Use pure C# There is no need to create another environment such as virtual machine for development , Efficient , The performance is much higher than Lua
- shortcoming :
- Each scheme has its own limitations , Direct reflection not only reduces performance , No support for JIT The system of (IOS) Can't use .
ILRuntime
It's solved JIT But not mature enough , There are many problems in using , Need high hands-on problem-solving ability .
- Solution
- DLL Reflect heat renewal : Use the compiled DLL File to replace , Use reflection to put the required C# The component is bound to the corresponding object to use .
ILRuntime
: In essence DLL Replacement , But it realizes a ILR(IL Runtime ) Enable it to support JIT Hardware environment (IOS) Realize code hot update under .
Basic process of hot update
- Version number comparison , If the version number is different, continue the following process (version.txt Record version number , Optional )
- Download the Reference document (files.txt Record all documents md5 code , Sort of like a directory )
- Confirm the download list , Compare the newly downloaded reference file with the old local reference file , Record missing or different files .( utilize files.txt Medium md5 Code to realize this step )
- According to the download list , Download required resources .( Generally placed on
Application.persistentDataPath
) - decompression ( If the file is compressed, you need to decompress it first , Optional )
- Ensure that the download is successful , Overwrite the local reference file with the latest reference file ( Update the catalog )
Hot update rule
Resource packaging : Code and resources that need to be updated , Must be packaged into
AssetBundle
(AB package ) Download and load ,AB The characteristics of the package are suitable for hot update .( For the following reasons )- AB Package storage location customization , Then it can be put into a readable and writable path to facilitate hot update
- AB Package custom compression , You can choose not to compress or choose LZMA and LZ4 Equal compression mode , Reduce the size of the bag , Faster network transmission .
- Resources can be distributed in different AB In bag , Minimize memory pressure at runtime , It can be used and loaded immediately , Selectively load the required content .
- AB The package supports dynamic updates in the later stage , Significantly reduce the size of the initial installation package , Non core resources AB Upload the package to the server , Late runtime dynamic loading , Improve user experience .
Resource storage path : Give priority to putting the code and resources that need to be updated in
Application.persistentDataPath
, Compared with other special directories , This directory is readable and writable , The runtime is effective and has no content restrictions, which is more suitable for hot update development .( The comparison of several important paths is as follows )Resources
Resources The resources under the folder will be packaged whether they are used or not
All resources are packed into a big package , Search loading is inefficient .
Resources will be compressed , Convert to binary
After packaging, the resources in the folder are read-only
Cannot dynamically change , Unable to do hot update
Use
Resources.Load
load
StreamingAssets
- Cache directory of streaming data
- The resources under the folder will be packaged whether they are used or not
- Resources will not be compressed and encrypted
- After packaging, the resources in the folder are read-only , Mainly storing binary files
- Cannot modify after packaging , Unable to do hot update
- WWW Class loading ( It's usually used
CreateFromFile
, If the resource isAssetBundle
, Whether it is compressed depends on its packaging method ) - Relative paths , The specific path depends on the actual platform
Application.streamingAssetsPath
The path is sometimes inaccurate on different platforms
Application.dataPath
- The path of the game's data folder ( For example, in Unity Editor Medium Assets)
- Unable to do hot update
Application.persistentDataPath
Path to the persistent data store directory ( Sandbox catalog , Does not exist before packaging )
The resources under the folder will be packaged whether they are used or not
Run time effective , read-write
No content restrictions , from
StreamingAsset
Read binary files in or fromAssetBundle
Read the file to writePersistentDataPath
inSuitable for hot update
边栏推荐
- Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
- 最安全的证券交易app都有哪些
- jacoco代码覆盖率
- Connecting FTP server tutorial
- The rebound problem of using Scrollview in cocos Creator
- Configure mongodb database in window environment
- [quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)
- 使用Scrapy框架爬取网页并保存到Mysql的实现
- [deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
- Detailed explanation of Cocos creator 2.4.0 rendering process
猜你喜欢
Keil5 does not support online simulation of STM32 F0 series
The rebound problem of using Scrollview in cocos Creator
webgl_ Enter the three-dimensional world (2)
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
HW primary flow monitoring, what should we do
The difference between full-time graduate students and part-time graduate students!
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
Getting started with webgl (1)
2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
How to create Apple Developer personal account P8 certificate
随机推荐
Pit avoidance: description of null values in in and not in SQL
Nacos conformance protocol cp/ap/jraft/distro protocol
HW初级流量监控,到底该怎么做
[original] all management without assessment is nonsense!
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Unity's ASE achieves full screen sand blowing effect
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
Basic knowledge sorting of mongodb database
HPDC smart base Talent Development Summit essay
Webgl texture
OpenGL common functions
Annexb and avcc are two methods of data segmentation in decoding
Super signature principle (fully automated super signature) [Yun Xiaoduo]
Window环境下配置Mongodb数据库
Keil5 does not support online simulation of STM32 F0 series
How to release NFT in batches in opensea (rinkeby test network)
知否|两大风控最重要指标与客群好坏的关系分析
webgl_ Graphic transformation (rotation, translation, zoom)
Yunxiaoduo software internal test distribution test platform description document
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)