Живопись спрайта в единстве

Проблема:

Я хочу сделать прототип для чистки окон (я имею в виду чистку грязных окон) в единстве.

Я искал об этой теме и обнаружил, что я могу изменить пиксель поTexture2D.SetPixel().

Я пытаюсь сделать это этим методом. Сначала я включил чтение / запись текстуры и попробовал этот метод, но на моем спрайте ничего не произошло.

Поэтому я хочу спросить, можно ли изменить альфа-спрайт, который щелкается мышью или прикасается к нему, чтобы показать приведенный ниже спрайт оригинального!

Мой код:

    private RaycastHit2D hitInfo;
    private SpriteRenderer spriteRendererComponent;
    private Color zeroAlpha;

    // Use this for initialization
    void Start ()
    {
        spriteRendererComponent = transform.GetComponent<SpriteRenderer>();
        zeroAlpha = Color.blue;
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton(0))
        {
            MouseClick();
        }
    }

    public void MouseClick()
    {
        Vector2 mousePosition = Vector2.zero;
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        hitInfo = Physics2D.Raycast(mousePosition, Vector2.zero);
        if (hitInfo)
        {
            spriteRendererComponent.sprite.texture.SetPixel((int)hitInfo.point.x, (int)hitInfo.point.y, zeroAlpha);
            spriteRendererComponent.sprite.texture.Apply();
        }
    }

Ответ :

Ты можешь использоватьэта тема для оптимизации изменения пикселей спрайта

Я нашел ответ об изменении пикселей спрайта (Живопись)

public float radius;
public Color InitialColor;

private RaycastHit2D hitInfo;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (CustomInput.ControlStay())
    {
        hitInfo = CustomInput.ClickednTouched().hitInfo;
        if (hitInfo)
        {
            UpdateTexture();
        }
    }
}

public Texture2D CopyTexture2D(Texture2D copiedTexture2D)
{
    float differenceX;
    float differenceY;

    //Create a new Texture2D, which will be the copy
    Texture2D texture = new Texture2D(copiedTexture2D.width, copiedTexture2D.height);

    //Choose your filtermode and wrapmode
    texture.filterMode = FilterMode.Bilinear;
    texture.wrapMode = TextureWrapMode.Clamp;

    //Center of hit point circle 
    int m1 = (int)((hitInfo.point.x + 2.5f) / 5 * copiedTexture2D.width);
    int m2 = (int)((hitInfo.point.y + 2.5f) / 5 * copiedTexture2D.height);

    for (int x = 0; x < texture.width; x++)
    {
        for (int y = 0; y < texture.height; y++)
        {
            differenceX = x - m1;
            differenceY = y - m2;

            //INSERT YOUR LOGIC HERE
            if (differenceX * differenceX + differenceY * differenceY <= radius * radius)
            {
                //This line of code and if statement, turn all texture pixels within radius to zero alpha
                texture.SetPixel(x, y, InitialColor);
            }
            else
            {
                //This line of code is REQUIRED. Do NOT delete it. This is what copies the image as it was, without any change
                texture.SetPixel(x, y, copiedTexture2D.GetPixel(x, y));
            }
        }
    }

    //This finalizes it. If you want to edit it still, do it before you finish with Apply(). Do NOT expect to edit the image after you have applied.
    texture.Apply();

    return texture;
}

public void UpdateTexture()
{
    SpriteRenderer mySpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
    Texture2D newTexture2D = CopyTexture2D(mySpriteRenderer.sprite.texture);

    //Get the name of the old sprite
    string tempName = mySpriteRenderer.sprite.name;
    //Create a new sprite
    mySpriteRenderer.sprite = Sprite.Create(newTexture2D, mySpriteRenderer.sprite.rect, new Vector2(0.5f, 0.5f));
    //Name the sprite, the old name
    mySpriteRenderer.sprite.name = tempName;

    //Update the material
    //If you have multiple sprites, you will want to do this in a loop
    //mySpriteRenderer.material.mainTexture = newTexture2D;
    //mySpriteRenderer.material.shader = Shader.Find("Unlit/Transparent");

}

Другая проблема :

Нахождение пикселя на спрайте:

В Unity3d у нас естьRaycastHit.textureCoord но он больше не существует в 2D. Я много искал об этой проблеме, но ничего полезного не нашел.

Поэтому я хочу знать решение этой проблемы, и мне интересно, почему такой метод, как textureCoord в 3D, не существует в 2D.

Ответ :

Я нашел ответ снова, как вы видите в предыдущем коде для поиска пикселя на спрайте.

Нить :Поиск пикселя на спрайте в Unity

Ответы на вопрос(2)

Ваш ответ на вопрос