当前位置:网站首页>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
边栏推荐
- 2. Heap sort "hard to understand sort"
- 【深度学习】图像超分实验:SRCNN/FSRCNN
- Tkinter after how to refresh data and cancel refreshing
- 【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
- Unity之ASE实现卡通火焰
- Unity's ASE realizes cartoon flame
- #HPDC智能基座人才发展峰会随笔
- 【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
- 大表delete删数据导致数据库异常解决
- 微信小程序 01
猜你喜欢
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
Ida Pro reverse tool finds the IP and port of the socket server
写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜
HPDC smart base Talent Development Summit essay
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
【深度学习】图像超分实验:SRCNN/FSRCNN
知否|两大风控最重要指标与客群好坏的关系分析
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
Cocos uses custom material to display problems
随机推荐
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
Matlab experience summary
Create lib Library in keil and use lib Library
Unity之ASE实现全屏风沙效果
webgl_ Enter the three-dimensional world (2)
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
[make a boat diary] [shapr3d STL format to gcode]
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
Briefly describe the working principle of kept
Iterator and for of.. loop
Clang compile link ffmpeg FAQ
Unity's ASE achieves full screen sand blowing effect
从 1.5 开始搭建一个微服务框架链路追踪 traceId
Gd32 F3 pin mapping problem SW interface cannot be burned
【兰州大学】考研初试复试资料分享
Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
webgl_ Graphic transformation (rotation, translation, zoom)
Basic knowledge sorting of mongodb database