![](/img/trans.png)
[英]How to Make a Clickable Sprite in Unity 2D, detect if the cursor is hovering over a sprite, and how to change scene with code
[英]Unity - How to change sprite size to fixed resoltion in code
我有一組具有不同分辨率和縱橫比的精靈。 我想根據原始精靈創建新的精靈,但分辨率固定為 20 x 20。
使用位圖時,我所做的是: Bitmap newImage = new Bitmap(oldImage, new Size(20, 20));
但是 Unity 的 Sprite 沒有任何構造函數。 我怎樣才能做到這一點?
不確定您想如何調整圖像的大小(使其更小,或剪切它的一部分),但您可以使用Sprite.Create
方法
有兩種方法可以從圖像或原始精靈創建精靈。 從下面的圖像代碼..
string url = "";//image url;
WWW image = new WWW(url);
yield return image;
Texture2D texture = new Texture2D(1, 1);
image.LoadImageIntoTexture(texture);
Sprite newSprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0), 1);
RectTransform rt = newSprite.GetComponent<RectTransform>();
rt.sizeDelta = new Vector2(20, 20);//make 20px * 20px sprite
克隆下面的精靈代碼..
Sprite cloneSprite = Instantiate(originalSprite);
RectTransform rt = cloneSprite.GetComponent<RectTransform>();
rt.sizeDelta = new Vector2(20, 20);//make 20px * 20px sprite
我希望它適用於您的項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.