unity xlua 中实现画圈,监测逻辑

local gestureDetector = {};
local gestureSum = Vector2.zero;
local gestureLength = 0;
local gestureCount = 0;
local numOfCircleToShow = 1;
local Application = CS.UnityEngine.Application
local Input = CS.UnityEngine.Input

local function  __Test( )
 
    if Application.platform == RuntimePlatform.Android or Application.platform == RuntimePlatform.IPhonePlayer then
        if Input.touches.Length ~= 1 then 
            gestureDetector = {}
            gestureCount = 0;    
        else
            if Input.touches[0].phase == TouchPhase.Canceled or Input.touches[0].phase == TouchPhase.Ended then
                gestureDetector = {}
            elseif Input.touches[0].phase == TouchPhase.Moved then   
                local p = Input.touches[0].position;
                if #gestureDetector == 0 or (p - gestureDetector[#gestureDetector]).magnitude > 10 then
                     table.insert( gestureDetector, p )
                end
            end
        end
    else 
        if Input.GetMouseButtonUp(0) then
        
            gestureDetector = {}
            gestureCount = 0;  
        else
            if Input.GetMouseButton(0) then
                local p = Vector2.New(Input.mousePosition.x, Input.mousePosition.y);
                if #gestureDetector == 0 or (p - gestureDetector[#gestureDetector]).magnitude > 10 then
                    table.insert( gestureDetector, p )
                end
            end
        end
    end

    if #gestureDetector < 10 then
        return false;
    end

    gestureSum = Vector2.zero;
    gestureLength = 0;
    local prevDelta = Vector2.zero;
    for i = 1, #gestureDetector - 1 do
        local delta = gestureDetector[i + 1] - gestureDetector[i];
        local deltaLength = delta.magnitude;
        gestureSum = gestureSum + delta;
        gestureLength = gestureLength + deltaLength;
        local dot = Vector2.Dot(delta, prevDelta);
        if dot < 0 then
            gestureDetector = {}
            gestureCount = 0;
            return false;
        end
        prevDelta = delta;
    end

    local gestureBase = (Screen.width + Screen.height) / 4;
    if gestureLength > gestureBase and gestureSum.magnitude < gestureBase / 2 then
        gestureDetector = {}
        gestureCount = gestureCount + 1
        if gestureCount >= numOfCircleToShow then
            return true;
        end
    end

    return false;
end

 

你可能感兴趣的:(unity xlua 中实现画圈,监测逻辑)