[Unity] HTFramework Framework (44) [Advanced] Command System

Updated: May 29, 2023.
Github source code: [Click me to get the source code]
Gitee source code: [Click me to get the source code]

command system

指令系统Provides another supplementary solution for Unity dynamic patches, hot updates, etc. We can 指令代码compile and execute any segment in real time (please rest assured that the performance overhead of real-time compilation is extremely low), and achieve the operation of modifying the program function at runtime.

easy to use

Define the InstructionAgent

First of all, define an InstructionAgentobject, InstructionAgentwhich literally means that 可执行指令的代理者it is 指令系统the simplest interface that has been opened up. It integrates 源码读取, 代码编译, and . Users don’t need to care about anything, just write the source code and call it directly.代码执行

public class Test : HTBehaviour
{
    
    
    [Label("指令代码")] public InstructionAgent Agent;
}

Edit instruction code

After defining InstructionAgentthe object, we go back to the editor, mount the Test script on the scene object, and then we can see the 指令代码editing window.
insert image description here

We type a few paragraphs in it 指令代码(the syntax of the instruction code will be explained later):
insert image description here

The functions implemented by these pieces of code are:

1. #NewObj "New Object" ----- create a new GameObject named 新的物体;
2. #Define [A] "New Object" ----- define identifier [A], point to 新的物体;
3. #Define [Light ] "UnityEngine.Light" ----- Define the identifier [Light], point to UnityEngine.Light;
4. #AddCom [A] [Light] ----- [A]Add a component to the target object of the identifier [Light].

execute instruction code

Back to the IDE, in Testthe class, we only need to call a Executemethod to execute this instruction code (click Executethe button in the edit panel, but only at runtime):

public class Test : HTBehaviour
{
    
    
    [Label("指令代码")] public InstructionAgent Agent;

    private void Start()
    {
    
    
        Agent.Execute();
    }
}

Then run the scene in the editor, and the effect of the instruction code will be produced (note that the main module of the framework must exist in the scene HTFramework):
insert image description here
Of course, in addition to editing the code in the editor, it is definitely possible to directly InstructionAgentassign the code, as follows:

public class Test : HTBehaviour
{
    
    
    [Label("动态指令代码")] public InstructionAgent Agent;

    private void Start()
    {
    
    
        Agent.Code = "你的代码******";

        Agent.Execute();
    }
}

command code syntax

Of course, the above only introduces 指令代码the rare ones used, why is it called 指令代码instead of called directly 代码? That's because the code here is 逐行编译、逐行执行yes, and each line is more like one 指令, such as making A object perform B behavior, setting the D field of C component to the E value, etc. In simple terms, it is behavior-based 指令code, and the abbreviation is naturally 指令代码.

basic grammar

The basic syntax of the command code is as follows, one line represents one command:

#NewObj Args
1.#NewObj: instruction keyword;
2.Args: the parameter of the instruction behavior, which can be an identifier, a value of value type (can have multiple parameters).
Note: Command keywords and parameters are separated by commas 空格, strings must be ""wrapped in double quotes (the string cannot contain double quotes, and escape characters \"are not allowed), and identifiers must be []wrapped in commas. Currently, only identifiers of the string type are supported, and the identifier The definition of the character must be used before he.

command keyword

Currently only the following command keywords are supported, and each keyword represents one type 指令行为:

1. #Define [A] "B"
——Define the identifier , that is, define [A]the value of the identifier "B";
2. #AddCom "Path" "ComType"
——Add component , that is, "Path"add a component to the object pointed to by the path "ComType";
3. #RemoveCom "Path" "ComType"
——Remove component , that is, "Path"remove the component from the object pointed to by the path "ComType";
4. #SetField "Path" "ComType" "FieldName" Args
——Set the field value , that is, set the value of the field of the "Path"component whose path points to the object , which must be a value type (not an identifier); 5. ——Set the attribute value , that is, set the attribute of the component whose path points to the object Value , must be a value type (cannot be an identifier); 6. ——Create a new game object , that is, create a new game object named ; 7. ——Delete a game object , that is, delete the game object pointed by the path; 8. — —"ComType""FieldName"ArgsArgs
#SetProperty "Path" "ComType" "PropertyName" Args
"Path""ComType""PropertyName"ArgsArgs
#NewObj "Name"
"Name"
#DeleteObj "Path"
"Path"
#Rename "Path" "Name"
Rename the game object , that is, "Path"rename the game object pointed to by the path "Name";
9. #Active "Path" true
——Activate and hide the game object , that is, "Path"activate or hide the game object pointed to by the path true、false;
10. #SendMessage "Path" "MethodName" Args(可选)
——Send a message to the game object , that is, to "Path"the path pointed to The game object sends a message "MethodName", Argswhich must be a value type (optional);
11. #SetParent "Path" "Parent Path"
——Set the parent of the game object , that is, "Path"set the game object pointed by the path "Parent Path"as the child;
12. #SetPosition "Path" Vector3(0,0,0) true
——Set the position of the game object , that is, set "Path"the path pointed to The position of the game object is Vector3(0,0,0), the third parameter truerepresents whether to use the world coordinates;
13. #SetRotation "Path" Vector3(0,0,0) true
——Set the rotation of the game object , that is, "Path"the rotation of the game object pointed by the path is set Vector3(0,0,0), the third parameter truerepresents whether to use the world coordinates;
14. #SetScale "Path" Vector3(0,0,0)
——Set the zoom of the game object , that is, set "Path"the zoom of the game object pointed by the path to Vector3(0,0,0);

note

Currently only //single-line comments are supported.

Supported value types

Currently, only value types such as string, bool, int, float, Vector2, are supported Vector3.

Identifier naming convention

Strictly speaking, there is currently no naming convention for identifiers, except that they must be []wrapped, and there are no hard requirements (note that identifiers cannot appear "").

advanced use

By understanding the entire 指令系统usage rules, I think you have already understood that the instruction code can be derived from 网络, AB包中, or even 数据库中, then use it to realize your 紧急性、临时性program patching function (remove after the official patch goes online), think about it or Very good.

Custom commands: [Unity] HTFramework framework (forty-five) [Advanced] command system - custom commands.

runtime inspector

When running in the editor, a runtime inspection panel (Runtime Data) will appear, which is mainly used for debugging or data monitoring. The current panel is as follows:
insert image description here
1. No Runtime Data!

Guess you like

Origin blog.csdn.net/qq992817263/article/details/130918484