SKSprite.setUVs C# (CSharp) Method

setUVs() public method

public setUVs ( Rect uvRect ) : void
uvRect Rect
return void
    public void setUVs( Rect uvRect )
    {
        var uvs = new Vector2[4];

        if( !_isFlipped )
        {
            uvs[0] = new Vector2( uvRect.xMin, uvRect.yMax );
            uvs[1] = new Vector2( uvRect.xMin, uvRect.yMin );
            uvs[2] = new Vector2( uvRect.xMax, uvRect.yMin );
            uvs[3] = new Vector2( uvRect.xMax, uvRect.yMax );
        }
        else
        {
            uvs[3] = new Vector2( uvRect.xMin, uvRect.yMax );
            uvs[2] = new Vector2( uvRect.xMin, uvRect.yMin );
            uvs[1] = new Vector2( uvRect.xMax, uvRect.yMin );
            uvs[0] = new Vector2( uvRect.xMax, uvRect.yMax );
        }

        meshFilter.sharedMesh.uv = uvs;
    }

Usage Example

Esempio n. 1
0
    /// <summary>
    /// tick method. if it returns true it indicates the animation is complete
    /// </summary>
    public bool tick(float deltaTime)
    {
        if (_isStopped)
        {
            return(true);
        }

        if (_isPaused)
        {
            return(false);
        }

        // handle delay and return if we are still delaying
        if (!_delayComplete && _elapsedDelay < delay)
        {
            _elapsedDelay += deltaTime;

            // are we done delaying?
            if (_elapsedDelay >= delay)
            {
                _delayComplete = true;
            }

            return(false);
        }

        var isComplete = false;

        // handle applying speed
        deltaTime *= animationState.speed;

        // increment or decrement the total elapsed time then clamp from 0 to totalDuration
        if (_isReversed)
        {
            _totalElapsedTime -= deltaTime;
        }
        else
        {
            _totalElapsedTime += deltaTime;
        }

        _totalElapsedTime = Mathf.Clamp(_totalElapsedTime, 0, _totalDuration);

        // using our fresh totalElapsedTime, figure out what iteration we are on
        _completedIterations = (int)Mathf.Floor(_totalElapsedTime / _duration);

        // we can only be loopiong back on a PingPong if our loopType is PingPong and we are on an odd numbered iteration
        _isLoopingBackOnPingPong = false;
        if (animationState.wrapMode == WrapMode.PingPong)
        {
            // infinite loops and we are on an odd numbered iteration
            if (iterations < 0 && _completedIterations % 2 != 0)
            {
                _isLoopingBackOnPingPong = true;
            }
            else if (iterations > 0)
            {
                // we have finished all iterations and we went one over to a non looping back iteration
                // so we still count as looping back so that we finish in the proper location
                if (_completedIterations >= iterations && _completedIterations % 2 == 0)
                {
                    _isLoopingBackOnPingPong = true;
                }
                else if (_completedIterations < iterations && _completedIterations % 2 != 0)
                {
                    _isLoopingBackOnPingPong = true;
                }
            }
        }


        // figure out the current elapsedTime
        if (iterations > 0 && _completedIterations >= iterations)
        {
            // we finished all iterations so clamp to the end of this tick
            _elapsedTime = _duration;

            // if we arent reversed, we are done
            if (!_isReversed)
            {
                isComplete = true;
            }
        }
        else if (_totalElapsedTime < _duration)
        {
            _elapsedTime = _totalElapsedTime;             // havent finished a single iteration yet
        }
        else
        {
            // TODO: when we increment a completed iteration (go from 0 to 1 for example) we should probably run through once setting
            // _elapsedTime = duration so that complete handlers in a chain or flow fire when expected
            _elapsedTime = _totalElapsedTime % _duration;             // have finished at least one iteration
        }


        // check for completion when going in reverse
        if (_isReversed && _totalElapsedTime <= 0)
        {
            isComplete = true;
        }


        // return true only if we are complete
        if (isComplete)
        {
            // fire off the completion handler and return true
            return(true);
        }


        // if we are looping back on a PingPong loop
        var convertedElapsedTime = _isLoopingBackOnPingPong ? _duration - _elapsedTime : _elapsedTime;

        //Adjust the converted elapsed time when doing a pingpong loopback so the last frame is not
        //displayed 2x longer than other frames
        if (_isLoopingBackOnPingPong && _duration - _elapsedTime > _secondsPerFrame)
        {
            convertedElapsedTime = _duration - _elapsedTime - _secondsPerFrame;
        }

        var desiredFrame = Mathf.FloorToInt(convertedElapsedTime / _secondsPerFrame);

        //Adjust for the total elapsed time when in ping-pong mode so frame 0 is not displayed 2x
        //longer than other frames
        if (_isLoopingBackOnPingPong && desiredFrame == 0)
        {
            _totalElapsedTime += _secondsPerFrame;
        }

        if (desiredFrame != _currentFrame)
        {
            _currentFrame = desiredFrame;
            _sprite.setUVs(animationState.textureInfo[_currentFrame].uvRect);
        }

        return(false);
    }