Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question I need to locate a sprite position from an atlas, it doesn't working...

Discussion in 'Scripting' started by ChaosVan, Aug 10, 2020.

  1. ChaosVan

    ChaosVan

    Joined:
    Dec 13, 2016
    Posts:
    12
    Code (csharp):
    1.  
    2.             void OnSuccess(RawImage holder, Sprite sprite, object data)
    3.             {
    4.                 holder.texture = sprite.texture;
    5.                 Vector2 pos = new Vector2(sprite.textureRect.x / sprite.texture.width, sprite.textureRect.y / sprite.texture.height);
    6.                 Vector2 size = new Vector2(sprite.textureRect.width / sprite.texture.width, sprite.textureRect.height / sprite.texture.height);
    7.                 holder.uvRect = new Rect(pos, size);
    8.             }
    9.             void OnSuccess(MeshRenderer holder, Sprite sprite, object data)
    10.             {
    11.                 holder.material.mainTexture = sprite.texture;
    12.                 holder.material.mainTextureScale = new Vector2(sprite.textureRect.width / sprite.texture.width, sprite.textureRect.height / sprite.texture.height);
    13.                 holder.material.mainTextureOffset = new Vector2(sprite.textureRect.x / sprite.texture.width, sprite.textureRect.y / sprite.texture.height);
    14.             }
    15.  
    It worked correctly on Unity2018.4, but it doesn't work on Unity2019.4, the value of textureRect.x or y is zero.
     
  2. ChaosVan

    ChaosVan

    Joined:
    Dec 13, 2016
    Posts:
    12
    The sprite is get from an SpriteAtlas