exSprite.SetSprite C# (CSharp) Method

SetSprite() public method

public SetSprite ( exAtlas, _atlas, int _index, bool _changeDefaultAnimSprite = false ) : void
_atlas exAtlas,
_index int
_changeDefaultAnimSprite bool
return void
    public void SetSprite( exAtlas _atlas, int _index, bool _changeDefaultAnimSprite = false )
    {
        bool checkVertex = false;
        bool createMesh = false;

        // pre-check
        if ( _atlas == null ||
             _atlas.elements == null ||
             _index < 0 ||
             _index >= _atlas.elements.Length )
        {
            Debug.LogWarning ( "Invalid input in SetSprite. atlas = " + (_atlas ? _atlas.name : "null") + ", index = " + _index );
            return;
        }

        // it is possible that the atlas is null and we don't have mesh
        if ( atlas_ == null ) {
            createMesh = true;
        }

        //
        if ( atlas_ != _atlas ) {
            atlas_ = _atlas;
            renderer.sharedMaterial = _atlas.material;
            updateFlags |= UpdateFlags.UV;
            checkVertex = true;
        }

        //
        if ( index_ != _index ) {
            index_ = _index;
            updateFlags |= UpdateFlags.UV;
            checkVertex = true;
        }

        //
        if ( checkVertex ) {

            // NOTE: if we use texture offset, it always need to update vertex
            if ( useTextureOffset_ ) {
                updateFlags |= UpdateFlags.Vertex;
            }

            if ( !customSize_ ) {
                exAtlas.Element el = atlas_.elements[index_];

                float newWidth = el.trimRect.width;
                float newHeight = el.trimRect.height;
                // float newWidth = el.coords.width * atlas_.texture.width;
                // float newHeight = el.coords.height * atlas_.texture.height;

                if ( el.rotated ) {
                    float tmp = newWidth;
                    newWidth = newHeight;
                    newHeight = tmp;
                }

                if ( newWidth != width_ || newHeight != height_ ) {
                    width_ = newWidth;
                    height_ = newHeight;
                    updateFlags |= UpdateFlags.Vertex;
                }
            }
        }

        //
        if ( createMesh ) {
            if ( meshFilter ) {
                // create mesh ( in editor, this can duplicate mesh to prevent shared mesh for sprite)
                meshFilter_.mesh = new Mesh();
                meshFilter_.sharedMesh.hideFlags = HideFlags.DontSave;
                updateFlags = UpdateFlags.Vertex | UpdateFlags.UV | UpdateFlags.Color | UpdateFlags.Index;

                // check if update mesh collider
                MeshCollider meshCollider = collider as MeshCollider;
                if ( meshCollider && meshCollider.sharedMesh == null ) {
                    this.UpdateColliderSize(0.2f);
                }
            }
        }

        //
        if ( _changeDefaultAnimSprite && spanim ) {
            spanim.UpdateDefaultSprite ( _atlas, _index );
        }
    }

Usage Example

Example #1
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void Update()
    {
        if (playing && (curAnimation != null))
        {
            // advance the time and check if we trigger any animation events
            float delta   = Time.deltaTime * curAnimation.speed;
            float curTime = curAnimation.time;

            // advance the time
            curAnimation.time += delta;
            Step(curAnimation);

            // save the last state
            float             lastAnimTime  = curAnimation.time;
            exSpriteAnimState lastAnimation = curAnimation;

            //
            int newIdx = curAnimation.clip.TriggerEvents(this,
                                                         lastAnimation,
                                                         lastEventInfoIndex,
                                                         curTime,
                                                         delta,
                                                         curAnimation.wrapMode);

            // NOTE: it is possible in the events, user destroy this component. In this case,
            //       the curAnimation will be null.
            if (curAnimation == null ||
                curAnimation != lastAnimation ||
                lastAnimTime != curAnimation.time /*NOTE: it is possible in the event we reply the same animation*/)
            {
                return;
            }
            lastEventInfoIndex = newIdx;

            // set sprite to current time
            exSpriteAnimClip.FrameInfo fi = GetCurFrameInfo();
            if (fi != null)
            {
                sprite.SetSprite(fi.atlas, fi.index);
            }

            // check if stop
            if (curAnimation.wrapMode == WrapMode.Once ||
                curAnimation.wrapMode == WrapMode.Default)
            {
                if ((curAnimation.speed > 0.0f && curAnimation.time >= curAnimation.length) ||
                    (curAnimation.speed < 0.0f && curAnimation.time <= 0.0f))
                {
                    Stop();
                }
            }
        }
    }
All Usage Examples Of exSprite::SetSprite