Hot update of Unity with ECMAScript4 (ActionScript3) -- operator overloading and implicit type conversion

In C#, certain types define implicit type conversions and operator overloading. In Unity, some objects also define implicit type conversions and operator overloading. Typical cases are: UnityEngine.Object. The destruction of UnityEngine.Object is to call Destory. The object that has been called Destory does not really become null, but operations on it will cause an exception.

Unity overloads UnityEngine.Object "==", "!=" operators, and implicit conversions.

public static bool operator ==(Object x, Object y);
public static bool operator !=(Object x, Object y);

public static implicit operator bool(Object exists);

Therefore, in Unity, the way to judge whether a GameObject has been deleted by Destory is to judge whether it is == null, or use it directly as the judgment condition of if.

Operator overloading and implicit type conversions in scripting systems

In order to support these features of Unity, the hotfix script system also adds operator overloading. Open the last project , we made some changes to the code, and we can see that most of the operator overloading in the script is also feasible.

package
{
    
    [Doc]
    /**
     * ...
     * @author
     */ 
    public  class Main
    {
        
        
        public function Main() 
        {    
        }

        public function update():void
        {    
        }
        
    }
    
}
import unityengine.GameObject;
import unityengine.UObject;
//新建一个GameObject
var obj:GameObject = new GameObject("test");
if( obj != null)
{
    trace( "print obj" );
    trace("obj:", obj);
}
//销毁GameObject
UObject.destroyImmediate_(obj);

if (!obj) // Implicit type conversion takes effect. 
{
    trace( "Implicit type conversion, obj is converted to bool, since obj has been destroyed, it is false" );
    trace("obj:", obj);
}

if( obj === null )
{
    trace("obj is null");
}
else
{
    trace( "Strict equality comparison: obj is not null" );
    trace("obj not null");
}

 The execution result is as follows:

 

Then let's test other operator overloads:

In Unity, vectors and matrices have operator overloading. Now we modify the script to the following code:

package
{
    
    [Doc]
    /**
     * ...
     * @author
     */ 
    public  class Main
    {
        
        
        public function Main() 
        {    
        }

        public function update():void
        {    
        }
        
    }
    
}
import unityengine.Matrix4x4;
import unityengine.Vector3;
import unityengine.Vector4;

var v:Vector3 = new Vector3(4, 5, 6 );
 // Addition, subtraction, multiplication and division of Vector3 
trace( v, "+ Vector3.one =" , v + Vector3.one);
trace( v, "- Vector3.one =", v - Vector3.one);
trace( v, "* 5 =", v * 5 );
trace( v, "/ 5 =", v / 5 );

// Define a 4x4 matrix 
var m4:Matrix4x4 = Matrix4x4.scale( Vector3.one * 2 );
trace(m4);

// Demonstrate matrix and Vector3 multiplication. 
var v4:Vector4 = new Vector4( vx, vy, vz, 1 );
trace(  "m4 * v4 = ", m4 * v4 );

The execution result is as follows:

 

 When you use the wrong operator overloading, the compiler will raise an error if it can be checked at compile time.

For example, Matrix4x4 and Vector4 are directly multiplied, and must be a matrix X vector. So, the following code will result in a compilation error:

import unityengine.Matrix4x4;
import unityengine.Vector3;
import unityengine.Vector4;

var v:Vector3 = new Vector3(4, 5, 6);

// Define a 4x4 matrix 
var m4:Matrix4x4 = Matrix4x4.scale( Vector3.one * 2 );
trace(m4);

// Demonstrate matrix and Vector3 multiplication. 
var v4:Vector4 = new Vector4( vx, vy, vz, 1 );

var f = v4 * m4;

The compiler reports an error:

 

 

 If you choose to hide the variable type, the compilation will pass, but then the expected execution will not be obtained.

import unityengine.Matrix4x4;
import unityengine.Vector3;
import unityengine.Vector4;

var v:Vector3 = new Vector3(4, 5, 6);


var m4:* = Matrix4x4.scale( Vector3.one * 2 );
trace(m4);

var v4:* = new Vector4( vx, vy, vz, 1 );

// this is incorrect multiplication, the script system will return NaN 
var f = v4 * m4;
trace( f);

 

 So we understand the operator overloading function provided by the script.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324671978&siteId=291194637