usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
publicclassPlaneScript:MonoBehaviour{
//获得墙和子弹的纹理图片
public Texture2D bulletTexture;
public Texture2D wallTexture;
//墙面图片副本
Texture2D wallTextureCopy;
//墙面和子弹的宽、高
float wall_width;
float wall_height;
float bullet_width;
float bullet_height;
//获取射线信息
RaycastHit hit;
//定义一个泛型队列来存储像素点信息
Queue
voidStart( ){
uvQueues=new Queue
//获取墙面的贴图
wallTexture=GetComponent
//备份墙面贴图
wallTextureCopy=Instantiate(wallTexture);
//将备份的材质赋值回去,作为修改的对象
GetComponent
wall_height=wallTextureCopy.height;
wall_width=wallTextureCopy.width;
bullet_height=bulletTexture.height;
bullet_width=bulletTexture.width;
}
voidUpdate( ){
//点击鼠标左键发射射线
if(Input.GetMouseButtonDown(0)){
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),outhit)){
if(hit.collider.name=="Plane"){
Vector2 uv=hit.textureCoord;
uvQueues.Enqueue(uv);//入队列
//遍历子弹图片覆盖的区域
for(int i=0;i for(int j=0;j //得到墙面上子弹覆盖区域对应的每个像素点 float w=uv.x*wall_width-bullet_width/2+i; float h=uv.y*wall_height-bullet_height/2+j; //获得墙上每一个像素点的颜色 Color wallColor=wallTextureCopy.GetPixel((int)w,(int)h); //子弹对应的每一个像素点的颜色 Color bulletColor=bulletTexture.GetPixel(i,j); wallTextureCopy.SetPixel((int)w,(int)h,wallColor*bulletColor); } } wallTextureCopy.Apply( ); Invoke("ClearWall",2);//2秒后调用方法,清除墙面上弹痕 } } } } void ClearWall( ){ //获取队列中要取出队列的信息(弹痕) Vector2uv=uvQueues.Dequeue( ); for(inti=0;i for(intj=0;j float w=uv.x*wall_width-bullet_width/2+i; float h=uv.y*wall_height-bullet_height/2+j; Color wallColor=wallTexture.GetPixel((int)w,(int)h); wallTextureCopy.SetPixel((int)w,(int)h,wallColor); } } wallTextureCopy.Apply(); } }