在统一编辑器中单击您的纹理
将“纹理类型”从“纹理”更改为“高级”
选中“启用读/写”复选框
将“格式”从“自动压缩”改为“RGBA 32 位”
我将此组件附加到原始图像(您可以将其附加到其他东西,只需更改“原始图像图像”部分)
这将在图像的位置 10,10 处创建一个尺寸为 100x100 的孔,因此请确保您的纹理至少为 110x110 大。
using UnityEngine;
using UnityEngine.UI;
public class HoleInImage : MonoBehaviour
{
public RawImage image;
void Start()
{
// this will change the original:
Texture2D texture = image.texture as Texture2D;
// use this to change a copy (and not the original)
//Texture2D texture = Instantiate(image.mainTexture) as Texture2D;
//image.mainTexture = texture;
Color[] colors = new Color[100*100];
for( int i = 0; i < 100*100; ++i )
colors[i] = Color.clear;
texture.SetPixels( 10, 10, 100, 100, colors );
texture.Apply(false);
}
}
编辑:
由一个或多个精灵定义的洞:
对这些精灵做同样的事情:(高级,读/写启用,RGBA 32 位)
例如:如果精灵是白色的,而洞是用黑色定义的:
for( int i = 0; i < 100*100; ++i )
colors[i] = Color.clear;
改为:
Texture2D holeTex; // set this in editor, it must have same dimensions (100x100 in my example)
Color[] hole = holeTex.GetPixels();
for( int i = 0; i < 100*100; ++i )
{
if( hole[i] == Color.black ) // where sprite is black, there will be a hole
colors[i] = Color.clear;
}