Unity3D looking for setPixels example to paint with texture in C#

江枫思渺然 提交于 2019-12-03 21:42:02
alaslipknot

Nailed it !!

This code work perfectly as expected, it's heavily commented and it shows everything i did, it doesn't allow to dynamically paint an object since am still looking for how to get texture coordinate after clicking on the object, however, you can manually enter your coordinate and choose the target texture which with you want to paint your original object, i hope this will be useful for someone else in the future, Code :

using UnityEngine;
using System.Collections;

public class BasicPainting : MonoBehaviour
{

    // the texture that i will paint with and the original texture (for saving)
    public Texture2D targetTexture, originalTexture ;
    //temporary texture
    public Texture2D tmpTexture;

    void Start ()
    {
            //setting temp texture width and height 
            tmpTexture = new Texture2D (originalTexture.width, originalTexture.height);
            //fill the new texture with the original one (to avoid "empty" pixels)
            for (int y =0; y<tmpTexture.height; y++) {
                    for (int x = 0; x<tmpTexture.width; x++) {
                            tmpTexture.SetPixel (x, y, originalTexture.GetPixel (x, y));
                    }
            }
            print (tmpTexture.height);
            //filling a part of the temporary texture with the target texture 
            for (int y =0; y<tmpTexture.height-40; y++) {
                    for (int x = 0; x<tmpTexture.width; x++) {
                            tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));

                    }
            }
            //Apply 
            tmpTexture.Apply ();
            //change the object main texture 
            renderer.material.mainTexture = tmpTexture;
    }

}

You need two Asset Kit;

1- https://www.assetstore.unity3d.com/#/content/2682 for paint (brush vs)
or
https://www.assetstore.unity3d.com/#/content/11735 for paint (airbrush vs)

2- https://www.assetstore.unity3d.com/#/content/908 for sprite works (cut,add,delete)

Use this to find out which texture pixel are corresponding to the screen's position:

http://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html

However,the game you wanna clone look just a 2D GAME,it seem that no need to deal with 3D.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!