首页 > 编程语言 > Unity实现图片水印生成
2020
09-27

Unity实现图片水印生成

本文实例为大家分享了Unity实现图片水印生成的具体代码,供大家参考,具体内容如下

用于图片分享时添加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;
 }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧