zoukankan      html  css  js  c++  java
  • Unity3D NGUI刮刮卡效果

    线上效果 

    确保你的纹理的read/write 是勾选的,纹理格式是 RGBA32的

    //代码

    using UnityEngine;
    
    [RequireComponent(typeof(UITexture))]
    public class ChangeTexturePixel : MonoBehaviour {
    	private UITexture mUITex;
    	private Texture2D MyTex;
    	public int Radius = 10;
    	public Color Col = new Color(0,0,0,0);
    	void Awake()
    	{
    		mUITex = GetComponent<UITexture>();
    		var tex = mUITex.mainTexture as Texture2D;
    		MyTex = new Texture2D(tex.width, tex.height, tex.format, false);
    		MyTex.SetPixels(tex.GetPixels());
    		MyTex.Apply();
    		mUITex.mainTexture = MyTex;
    	}
    
    	void ChangePixelColorByCircle(int x, int y, int radius, Color col)
    	{
    		for (int i = -Radius; i < Radius; i++)
    		{
    			var py = y + i;
    			if (py < 0 || py >= MyTex.height)
    			{
    				continue;
    			}
    
    			for (int j = -Radius; j < Radius; j++)
    			{
    				var px = x + j;
    				if (px < 0 || px >= MyTex.width)
    				{
    					continue;
    				}
    				if (new Vector2(px - x, py - y).magnitude > Radius)
    				{
    					continue;
    				}
    				MyTex.SetPixel(px, py, Col);
    			}
    		}
    		MyTex.Apply();
    	}
    
    	int[] WorldPos2Pix(Vector3 worldPos)
    	{
    		var temp = transform.InverseTransformPoint(worldPos);
    		var pos = new Vector2(temp.x+mUITex.width/2,temp.y+mUITex.height/2);
    		float rateX = mUITex.width / (float)MyTex.width;
    		float rateY = mUITex.height / (float)MyTex.height;
    
    		return new []
    		{
    			(int)(pos.x / rateX), (int)(pos.y/rateY)
    		};
    		
    	}
    
    	void Update ()
    	{
    		if (Input.GetMouseButton(0))
    		{
    			Vector3 worldPos = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
    			var posA = WorldPos2Pix(worldPos);
    			ChangePixelColorByCircle(posA[0], posA[1], Radius, Col);
    		}
    	}
    }
    

      

  • 相关阅读:
    20210304. 3. 通讯协议及事件处理机制
    20210304. 2. 数据类型与底层数据结构
    20210304. 1. 缓存原理 & 设计
    20210304. 0.3. Redis Cluster 搭建
    20210304. 0.2. Redis 哨兵模式搭建
    20210304. 0.1. Redis 安装
    20210208. Neo4j
    20210207. MongoDB
    20210203 8. 运维和第三方工具
    Global Brain Dynamics Embed the Motor Command Sequence of Caenorhabditis elegans
  • 原文地址:https://www.cnblogs.com/mrblue/p/5447323.html
Copyright © 2011-2022 走看看