Unity3d 是否可以直接向相机显示图像?

Unity3d 是否可以直接向相机显示图像?,unity3d,Unity3d,我需要能够在运行时逐个像素地编辑图像,并拥有一台将其视图替换为图像的相机。有可能吗?肯定有可能。结合使用和将纹理设置为,可以写入纹理并操纵数据。我不知道你说的是什么意思 是否已将其视图替换为图像 以下是我如何处理你的问题 // color we are setting pixels to [SerializeField] private Color clr = Color.white; // our source UI image - it can be a raw image or spri

我需要能够在运行时逐个像素地编辑图像,并拥有一台将其视图替换为图像的相机。有可能吗?

肯定有可能。结合使用和将纹理设置为,可以写入纹理并操纵数据。我不知道你说的是什么意思

是否已将其视图替换为图像

以下是我如何处理你的问题

// color we are setting pixels to
[SerializeField] private Color clr = Color.white;

// our source UI image - it can be a raw image or sprite renderer, I just used UI image
[SerializeField] private Image img = null;

// the size of our 'brush'
[Range(1, 100)]
[SerializeField] private int BrushSize = 1;

// the texture we are going to manipulate
private Texture2D tex2D = null;

private void Awake()
{
    Sprite imgSprite = img.sprite;

    // create a new instance of our texture to not write to it directly and overwrite it
    tex2D = new Texture2D((int)imgSprite.rect.width, (int)imgSprite.rect.height);
    var pixels = imgSprite.texture.GetPixels((int)imgSprite.textureRect.x,
                                            (int)imgSprite.textureRect.y,
                                            (int)imgSprite.textureRect.width,
                                            (int)imgSprite.textureRect.height);
    tex2D.SetPixels(pixels);
    tex2D.Apply();

    // assign this new texture to our image by creating a new sprite
    img.sprite = Sprite.Create(tex2D, img.sprite.rect, img.sprite.pivot);
}

public void OnDrag(PointerEventData eventData)
{
    Vector2 localCursor;

    // convert the position click to a local position on our rect
    if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(img.rectTransform, eventData.position, eventData.pressEventCamera, out localCursor))
        return;

    // convert this position to pixel coordinates on our texture
    int px = Mathf.Clamp(0, (int)((localCursor.x - img.rectTransform.rect.x) * tex2D.width / img.rectTransform.rect.width), tex2D.width);
    int py = Mathf.Clamp(0, (int)((localCursor.y - img.rectTransform.rect.y) * tex2D.height / img.rectTransform.rect.height), tex2D.height);

    // debugging - you can remove this
    print(px + ", " + py);


    // if our brush size is greater than 1, then we need to grab neighbors
    if (BrushSize > 1)
    {
        // create an array for our colors
        Color[] colorArray = new Color[BrushSize * BrushSize];

        // fill this with our color
        for(int x = 0; x < BrushSize * BrushSize; ++x)
            colorArray[x] = clr;

        tex2D.SetPixels(px, py, BrushSize, BrushSize, colorArray);
    }
    else
    {
        // set our color at our position
        tex2D.SetPixel(px, py, clr);
    }

    // apply the changes
    tex2D.Apply();

    // set our sprite to the new texture data
    img.sprite = Sprite.Create(tex2D, img.sprite.rect, img.sprite.pivot);
}
//我们正在设置像素的颜色
[SerializeField]专用颜色clr=Color.white;
//我们的源UI图像-它可以是原始图像或精灵渲染器,我刚刚使用的UI图像
[SerializeField]私有映像img=null;
//我们的“刷子”的大小
[射程(1100)]
[SerializeField]私有int-BrushSize=1;
//我们要处理的纹理
私有纹理2d tex2D=null;
私人空间
{
精灵imgSprite=img.Sprite;
//创建纹理的新实例,以避免直接写入并覆盖它
tex2D=新纹理2d((int)imgSprite.rect.width,(int)imgSprite.rect.height);
var pixels=imgSprite.texture.GetPixels((int)imgSprite.texturepright.x,
(int)imgSprite.y,
(int)imgSprite.structure.width,
(int)imgSprite.高度);
tex2D.SetPixels(像素);
tex2D.Apply();
//通过创建新的精灵,将此新纹理指定给我们的图像
img.sprite=sprite.Create(tex2D、img.sprite.rect、img.sprite.pivot);
}
public void OnDrag(PointerEventData事件数据)
{
矢量2局部光标;
//将单击位置转换为矩形上的本地位置
如果(!RectTransformUtility.ScreenPointToLocalPointInRectangle(img.rectTransform、eventData.position、eventData.pressEventCamera、out localCursor))
返回;
//将此位置转换为纹理上的像素坐标
int px=Mathf.Clamp(0,(int)((localCursor.x-img.rectcransform.rect.x)*tex2D.width/img.rectcransform.rect.width),tex2D.width);
int py=Mathf.Clamp(0,(int)((localCursor.y-img.rectcransform.rect.y)*tex2D.height/img.rectcransform.rect.height),tex2D.height);
//调试-您可以删除此选项
打印(像素+“,”+py);
//如果我们的笔刷大小大于1,那么我们需要抓取邻居
如果(笔刷大小>1)
{
//为我们的颜色创建一个数组
颜色[]colorArray=新颜色[BrushSize*BrushSize];
//用我们的颜色填充这个
用于(int x=0;x
下面是正在运行的代码片段。我没有考虑一些错误处理,例如使用
SetPixels
时笔刷笔划超出纹理边界。当前笔刷也是左下对齐的,可以更改为居中。由于您没有添加任何自己的进度,我将留给您清理实现并根据您的用例需要对其进行更改

为清晰起见,以下是纹理的导入设置


我对此不太了解,但你不能只添加一个带有图像的画布,否则就行不通了。