UpdateDefaultSprite() публичный Метод

public UpdateDefaultSprite ( exAtlas, _atlas, int _index ) : void
_atlas exAtlas,
_index int
Результат void
Пример #1
0
    // ------------------------------------------------------------------
    /// \param _atlas the new atlas
    /// \param _index the index of the element in the new atlas
    /// \param _changeDefaultAnimSprite if this is true, the default animation sprite will be changed so when we use stopAction = defaultSprite it will change to this new one
    /// Set a new picture in an atlas to this sprite
    // ------------------------------------------------------------------

    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);
        }
    }