複製一個紋理爲SpriteAtlas的Sprite的紋理,大小和原始相同

複製一個紋理爲SpriteAtlas的Sprite的紋理,大小和原始相同

之前沒做過關於texture的東西。

在打包的資源中有一個預設所有的圖是打包在一起的。很多Sprite組成的圖形。

在遊戲加載後,需要對一些圖層做處理,這時候需要處理貼圖,但是又不想動原始的圖。
複製Sprite裏的貼圖遇到很多問題。

Sprite.rect 大小可能比 Sprite.textureRect大得多,有時又有textureRectOffset,開始new的圖形只是textureRect,發現比原始圖像小,圓心在中心位置,所以出現了錯位。

又想改座標,但是發現別的地方有問題,最好不要動位置。

還是複製一個一模一樣大小的紋理好一些。

很多問題找了文檔,也沒看懂

關於Sprite.textureRect的
https://docs.unity3d.com/ScriptReference/Sprite-textureRect.html

Sprite.textureRect
Leave feedback
public Rect textureRect;
Description
Get the rectangle this sprite uses on its texture. Raises an exception if this sprite is tightly packed in an atlas.

關於textureRectOffset的
https://docs.unity3d.com/ScriptReference/Sprite-textureRectOffset.html

Sprite.textureRectOffset
Leave feedback
public Vector2 textureRectOffset;
Description
Gets the offset of the rectangle this sprite uses on its texture to the original sprite bounds. If sprite mesh type is FullRect, offset is zero.

關於SetPixels的
https://docs.unity3d.com/ScriptReference/Texture2D.SetPixels.html

public void SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors, int miplevel = 0);
Description
Set a block of pixel colors.

This function is an extended version of SetPixels above; it does not
modify the whole mip level but modifies only blockWidth by blockHeight
region starting at x,y. The colors array must be
blockWidth*blockHeight size, and the modified block must fit into the
used mip level.

這能看懂?反正我沒看懂!! 也沒搜到相關問題~~

自己研究豐衣足食吧~

貼上來做一個記錄。

//複製一個紋理爲SpriteAtlas的Sprite的紋理,大小和原始相同。
    static Color ConstClearColor = Color.clear;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sp">sp是當前的Sprite</param>
    /// <returns></returns>
    public static Texture2D CopyTextureFrom(Sprite sp)
    {
        //這裏是rect的大小,可能比textureRect的大小更大一些.
        Texture2D texture = new Texture2D((int)sp.rect.width, (int)sp.rect.height);
        
        //這裏是因爲texture默認不是透明的,灰色的,這裏給他一個透明填充。
        //有其他更加效率高的寫法再替換掉.
        Color[] xxx = new Color[texture.width* texture.height];
        for (int i = 0; i < xxx.Length; i++)
        {
            xxx[i] = ConstClearColor;
        }
        texture.SetPixels(xxx);

        //這裏是獲取到對應在Atlas裏的紋理.
        Color[] pixelBuffer = sp.texture.GetPixels((int)sp.textureRect.x, (int)sp.textureRect.y, (int)sp.textureRect.width, (int)sp.textureRect.height);

        //這個blockWidth和Height的用法是自己猜的,沒找到相關的帖子,官網文檔寫的也含糊不是很懂.
        //自己猜測就是在這個位置填充這個大小的紋理
        texture.SetPixels((int)sp.textureRectOffset.x, (int)sp.textureRectOffset.y, (int)sp.textureRect.width, (int)sp.textureRect.height, pixelBuffer, 0);
        texture.Apply();

        return texture;
    }

調用:

nowSpriteRenderer1 = nowTrans1.GetComponent<SpriteRenderer>();
        Sprite sp = nowSpriteRenderer1.sprite;// NowSpriteAtlas.GetSprite(nowTrans1.name);
        Texture2D txt = CDrawTexture.CopyTextureFrom(sp);
        sp = Sprite.Create(txt, new Rect(0,0,txt.width,txt.height), new Vector2(0.5f, 0.5f)); //
        nowSpriteRenderer1.sprite = sp;

有紕漏歡迎留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章