当前位置:网站首页>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
边栏推荐
- Cut ffmpeg as needed, and use emscripten to compile and run
- Using eating in cocos Creator
- 使用cpolar建立一个商业网站(2)
- Nacos一致性协议 CP/AP/JRaft/Distro协议
- unnamed prototyped parameters not allowed when body is present
- [target detection] yolov5 Runtong voc2007 data set
- Tkinter after how to refresh data and cancel refreshing
- Android -- jetpack: the difference between livedata setValue and postvalue
- 【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
- Jacobo code coverage
猜你喜欢
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
webgl_ Enter the three-dimensional world (1)
2. 堆排序『较难理解的排序』
Unity's ASE achieves full screen sand blowing effect
OpenGL's distinction and understanding of VAO, VBO and EBO
【目标检测】YOLOv5跑通VOC2007数据集
unnamed prototyped parameters not allowed when body is present
Typescript release 4.8 beta
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
随机推荐
How to deploy the super signature distribution platform system?
【微信小程序】Chapter(5):微信小程序基础API接口
Briefly describe the working principle of kept
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
避坑:Sql中 in 和not in中有null值的情况说明
2. Basic knowledge of golang
MySQL bit类型解析
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
Database exception resolution caused by large table delete data deletion
Connecting FTP server tutorial
简述keepalived工作原理
TypeScript 发布 4.8 beta 版本
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
Detailed explanation of Cocos creator 2.4.0 rendering process
2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
HW初级流量监控,到底该怎么做
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)