欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity实现图片水印生成

程序员文章站 2022-05-26 10:05:49
本文实例为大家分享了unity实现图片水印生成的具体代码,供大家参考,具体内容如下用于图片分享时添加logo水印的功能,之前用来做你画我猜的方法,核心是用texture2d中的setpixels方法具...

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

用于图片分享时添加logo水印的功能,之前用来做你画我猜的方法,核心是用texture2d中的setpixels方法

具体实现如下

效果图:

Unity实现图片水印生成

上代码,比较简单不多说了

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;
 }
 
 
}

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