Unity图片水印 Unity实现图片水印生成
liu_sanad 人气:0用于图片分享时添加logo水印的功能,之前用来做你画我猜的方法,核心是用Texture2D中的SetPixels方法
具体实现如下
效果图:
上代码,比较简单不多说了
using UnityEngine; using System.Collections; using UnityEngine.UI; public class WaterMarkAdd : MonoBehaviour { public Image targetImage; public Sprite logoSprite; public Sprite imageSprite; // Use this for initialization void Start () { Texture2D t = AddLogo(imageSprite.texture, logoSprite.texture); Sprite s = new Sprite(); s=Sprite.Create(t, new Rect(0,0,t.width, t.height),new Vector2(0.5f,0.5f)); targetImage.sprite = s; } private Texture2D AddLogo(Texture2D image,Texture2D logo) { Texture2D logoTexture = new Texture2D(image.width,image.height); Color[] colors = image.GetPixels(); for (int i = 0; i < logo.width; i++) { for (int j = 0; j < logo.height; j++) { Color c = logo.GetPixel(i, j); if (c.a != 0) { colors[logoTexture.width * j + i] = c; } } } logoTexture.SetPixels(0, 0, image.width, image.height, colors); logoTexture.Apply(); return logoTexture; } }
加载全部内容