Unity 2D:在图像中动态创建一个洞
Unity 2D: dynamically create a hole in a image
我正在使用 Unity 2D,我正在尝试实现这种简单的效果。
我正在制作类似 Candy Crush Saga 的游戏。
我有一个包含所有项目的网格。
在每个级别中,网格字段都可以有不同的形状,在运行时创建(背景中间的深灰色区域)。
背景是静止图像(蓝天和青山)。
当棋子(例如蓝色五边形)从顶部掉落时,必须将它们隐藏起来,直到它们进入网格区域(深灰色);所以在实践中,背景图像(天空和山丘)不再是背景,而是前景,有一个由灰色区域表示的洞。
灰色区域也由精灵 sheet.
中的图块组成
我准备了一张图片,可惜我还不能在这里加载它。
我不知道如何在Unity中实现这种效果。
你有什么想法吗?
最简单的解决方案是创建所有已经带有孔的静态关卡图形,但我不想创建它们,因为这既浪费时间又浪费内存,我希望能够在运行时创建此效果。
我正在考虑使用 sprite sheet 为孔形状创建动态位图遮罩。
然后使用此位图掩码作为 material 例如应用于前景中的图像并打孔。
我希望已经足够清楚了。
谢谢!
卡米罗
在统一编辑器中单击您的纹理
将 "texture type" 从 "texture" 更改为 "advanced"
选中 "read/write enabled" 复选框
将 "format" 从 "automatic compressed" 更改为 "RGBA 32 bit"
我将此组件附加到原始图像(您可以将其附加到其他东西,只需更改 "RawImage image" 部分)
这将在图像的位置 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);
}
}
编辑:
由一个或多个精灵定义的洞:
对这些精灵执行相同的操作:(高级,read/write 启用,RGBA 32 位)
例如:如果 sprite 是白色的,而洞是用黑色定义的:
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;
}
我正在使用 Unity 2D,我正在尝试实现这种简单的效果。 我正在制作类似 Candy Crush Saga 的游戏。
我有一个包含所有项目的网格。 在每个级别中,网格字段都可以有不同的形状,在运行时创建(背景中间的深灰色区域)。 背景是静止图像(蓝天和青山)。 当棋子(例如蓝色五边形)从顶部掉落时,必须将它们隐藏起来,直到它们进入网格区域(深灰色);所以在实践中,背景图像(天空和山丘)不再是背景,而是前景,有一个由灰色区域表示的洞。 灰色区域也由精灵 sheet.
中的图块组成我准备了一张图片,可惜我还不能在这里加载它。
我不知道如何在Unity中实现这种效果。 你有什么想法吗?
最简单的解决方案是创建所有已经带有孔的静态关卡图形,但我不想创建它们,因为这既浪费时间又浪费内存,我希望能够在运行时创建此效果。
我正在考虑使用 sprite sheet 为孔形状创建动态位图遮罩。 然后使用此位图掩码作为 material 例如应用于前景中的图像并打孔。
我希望已经足够清楚了。
谢谢! 卡米罗
在统一编辑器中单击您的纹理
将 "texture type" 从 "texture" 更改为 "advanced"
选中 "read/write enabled" 复选框
将 "format" 从 "automatic compressed" 更改为 "RGBA 32 bit"
我将此组件附加到原始图像(您可以将其附加到其他东西,只需更改 "RawImage image" 部分)
这将在图像的位置 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);
}
}
编辑:
由一个或多个精灵定义的洞:
对这些精灵执行相同的操作:(高级,read/write 启用,RGBA 32 位)
例如:如果 sprite 是白色的,而洞是用黑色定义的:
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;
}