Transferred from: http://blog.csdn.net/leonwei/article/details/39233775
Recently do u3d hot update, study a variety of ways without fruit, the easiest way to think first is to use C # reflection mechanism, dynamic loading code, but due to the limitations of the AOT on iOS, resulting in iOS can not be implemented, and finally found the cslight this thing, The execution of CS code can be dynamically compiled within a certain range. Project home Page Http://crazylights.cnblogs.com/github home page Https://github.com/lightszero/CSLightStudio, the author of this cslight is still very powerful, The use of this thing successfully in the project planning tables, such as easy to change the place to achieve dynamic compilation implementation, the following summarizes the understanding of cslight and use of experience.
1. First Cslight support part of C # code, the support situation includes
Project |
Support |
Not supported |
Comments |
Support// |
*/* Not supported */ |
Basic type |
Support for int uint BOOL string float double |
Byte char short is not supported, but can be extended |
Variables and definitions |
Define variables in the same way as C #, and then use them to define and assign values at the same time.
Cases int i; int j=0; int k=i+1; BOOL B1; BOOL B2=true; BOOL B3=false; |
|
Mathematical calculations |
With C # Support + - * / % Five kinds of mathematical calculations Support += -= /= *= %= Five kinds of self-operation Support ++ -- Two self-increment operations, only supported on the left side of the variable ++i does not support i++ support Support > >= < <= = = = && | | Eight kinds of logical operations Support! Take counter Support three mesh operation? : |
Bit arithmetic not supported |
Cycle |
Supports for foreach while Dowhile, supports Continue,break,return
If is supported, can if else nested |
Switch Goto is not supported |
Name space |
Can write Debug.Log (); You can't write. UnityEnging.Debug.Log (); C#evil head can be written using |
Not supported |
Object invocation |
After registering the type New support As and coercion type conversion support Member Variable access support member function call support Registering event proxy support for types [] Index access for supported objects Static support C#evil can write class in the script. |
The class written in the script cannot inherit |
Array |
Array full support Support New Int[3] New int[]{1,2,3} Two kinds of syntax Arrays of any type must register subtypes and array types Generic array As type support For example, you can put list<int> Dictionary<int,string> Register as a type Total use |
|
Generic type |
Support
List<int> as a type cannot have spaces such as List < int > I don't know. |
|
Commissioned |
A delegate interface that supports scripting functions to register to Programs A.TEST+=FUNC1; A.settest (FUNC1); Two forms of |
|
anonymous functions |
Support for lambda expressions You can assign a value to a delegate |
Assigning a lambda expression to a var variable is not supported |
Exception handling |
Support |
|
Inherited |
Can inherit interface written in script, can inherit more |
Class inheritance is not supported (because of the AOT on iOS) |
Get/set |
Supports automatic implementation only int I { Get Set } |
Writing get/set process is not supported |
|
|
|
2. The relationship between the basic program (CS) and the script (Cslight)
3. Steps to use
1) New one environment
Cls_environment env=new cls_environment (this);
2) Register the engine class to the script, such as Env. Regtype (New Csle. Reghelper_type (typeof (List<string>), "list<string>"));
3) Compile Script
dictionary<string, ilist<token>> project = new dictionary<string, ilist<token>> ();
List<string> cspaths=new list<string> ();
List<byte[]> bytes=new list<byte[]> ();
for (int i=0;i<bytes. count;i++) {
Ilist<token> tokens =env. Parsertoken (commontool.bytes2string (Bytes[i],enc));
Project. ADD (Cspaths[i],tokens);
}
Env. Project_compiler (project,true);
4) Dynamic Execution code
Cls_content content=env. Createcontent ();
Ilist<token> Runcodetokens = env. Parsertoken (code);
Cls_content.value Value=null;
Try
{
Icls_expression expr = env. Expr_compilertoken (Runcodetokens);
Value= Env. Expr_execute (expr, content);
}
catch (Exception err) {
Debug.logerror (String. Format ("CS Light run error dump:\n {0} {1} {2}", content. Dumpvalue (), content. DumpStack (NULL), Err. ToString ()));
}
return value;
C#light types that can have permission calls need to be registered in advance
The registration of a delegate is slightly different
Env. Regdeletype (New Csle. Reghelper_deleaction ("Action"));
Debugging errors
1. Play Log to judge
2. Just try to run the script and use content after the error. Dumpvalue can dump the value of a variable on the script stack
Content. DumpStack can dump the script execution stack
Plus the information that the exception itself feeds back
Unity3d Online Update-cslight Usage summary