原理:实现两张图片的融合,即将其中一张图片的的像素点放到另外一张图片上。
1、需要两张图片,一张墙壁的图片,一张弹痕的图片,要实现用鼠标点击墙壁图片时,以点击的位置为中心点,将弹痕图片的像素点设置在墙壁图片上。


2、创建Unity工程,这两张图片拖到工程后,做以下修改设置
①需要设置这两张图片的TextureType为Advanced;
②这两张图片都要勾选上Read/WriteEnable属性,允许图片进行像素编辑;
③设置弹痕图片的MaxSize为64,Format格式为:RGBA32 bit;
④设置wall图片的MaxSize为2048,Format格式为RGBA32 bit;


3、创建场景,直接将墙壁图片托放在Plane上即可

4、创建脚本PlaneScr,在脚本中实现使用鼠标点击墙壁时能够生成相应的弹痕,将脚本挂载在Plane上
1、在脚本中添加如下字段

2、在Start方法中初始化数据

3、在Update方法中实现当鼠标点击时,出现弹痕

4、添加在3s后墙壁恢复成之前的状态

注意:
1、鼠标点击出现弹痕是通过摄像机发射一条射线,射线产生的碰撞信息hit.textureCoord是uv坐标,该坐标是一个0~1的范围的值;
2、这里3s后弹痕消失,使用的原理是将之前保存的图片信息中的像素点信息取出后再次进行替换。
网友评论