Esempio n. 1
0
    public void Update()
    {
        // Get latest texture
        if (
            GetComponent <Renderer>() != null &&
            GetComponent <Renderer>().sharedMaterial != null)
        {
            Atlas =
                GetComponent <Renderer>().sharedMaterial.GetTexture("_MainTex");
            // If received new image, make sure it's uptimized and stuff
            if (_previousAtlas != Atlas)
            {
                PrepNewTexture();
            }
        }

        // Get new cell bounds and scaling from layout and animationSet
        LayoutData curLayoutData   = DefaultLayoutData;
        var        layoutComponent = GetComponent <BbSpriteLayout>();

        if (layoutComponent != null && layoutComponent.enabled)
        {
            curLayoutData = layoutComponent.MyLayoutData;
        }
        int currentCell = 0;
        var animation   = GetComponent <BbSpriteAnimation>();

        if (animation != null && animation.enabled)
        {
            currentCell = animation.CurrentCell;
        }
        _cellBounds = curLayoutData.GetCell(
            currentCell,
            (_spriteToCamRotation.y - transform.eulerAngles.y + 360) % 360);
        _scale = curLayoutData.GetCellScale(currentCell, Atlas);

        // Check for reason to update the mesh
        var dirtyMesh = false;

        if (_previousAtlas != Atlas)
        {
            _previousAtlas = Atlas;
            dirtyMesh      = true;
        }
        if (_previousYRotation != transform.eulerAngles.y)
        {
            _previousYRotation = transform.eulerAngles.y;
            dirtyMesh          = true;
        }
        if (_previousCellBounds != _cellBounds)
        {
            _previousCellBounds = _cellBounds;
            dirtyMesh           = true;
        }
        if (_previousMyFacingType != MyFacingType)
        {
            _previousMyFacingType = MyFacingType;
            dirtyMesh             = true;
        }
        if (_previousScale != _scale)
        {
            _previousScale = _scale;
            var cCollider = GetComponent <CapsuleCollider>();
            if (cCollider != null)
            {
                cCollider.center = new Vector3(0, _scale.y / 2, 0);
                cCollider.radius = _scale.x / 2;
                cCollider.height = _scale.y;
            }
            dirtyMesh = true;
        }
        if (_previousSpriteToCamRotation != _spriteToCamRotation)
        {
            _previousSpriteToCamRotation = _spriteToCamRotation;
            dirtyMesh = true;
        }
        if (_previousVerticalOffset != VerticalOffset)
        {
            _previousVerticalOffset = VerticalOffset;
            dirtyMesh = true;
        }
        if (dirtyMesh)
        {
            UpdateMesh();
        }
    }