欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

unity實現(xiàn)繪畫功能

 更新時間:2021年04月23日 11:33:31   作者:水墨_  
這篇文章主要為大家詳細介紹了unity實現(xiàn)繪畫功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了unity實現(xiàn)繪畫功能的具體代碼,供大家參考,具體內(nèi)容如下

直接先上效果:

gif里面有些顏色不一樣是gif功能導(dǎo)致的,繪制出來的都是同一個顏色。
原理其實也簡單,通過一些列的坐標(biāo)轉(zhuǎn)換得到當(dāng)前繪制的坐標(biāo),然后根據(jù)畫筆的寬度計算像素數(shù)量,最后填充像素塊顏色。

備注:

紋理必須在導(dǎo)入設(shè)置中設(shè)置了 Is Readable 標(biāo)志
Texture2D.SetPixels :設(shè)置像素顏色塊。
Texture2D.Apply :實際應(yīng)用任何先前的 SetPixels 更改。

直接上代碼吧:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Draw : MonoBehaviour
{
    public static Color Pen_Colour = Color.red;
    public static int Pen_Width = 3;
     
    public LayerMask Drawing_Layers;
     
    private Sprite drawable_sprite;
    private Texture2D drawable_texture;
     
    private Vector2 previous_drag_position;
    private Color[] clean_colours_array;
    private Collider2D[] rayResult = new Collider2D[2];
    private Color32[] cur_colors;
     
    private bool no_drawing_on_current_drag = false;
    private bool mouse_was_previously_held_down = false;
     
    void Awake()
    {
        drawable_sprite = this.GetComponent<SpriteRenderer>().sprite;
        drawable_texture = drawable_sprite.texture;
 
        clean_colours_array = new Color[(int)drawable_sprite.rect.width * (int)drawable_sprite.rect.height];
        clean_colours_array = drawable_texture.GetPixels();
    }
     
    void Update()
    {
        bool mouse_held_down = Input.GetMouseButton(0);
        if (mouse_held_down && !no_drawing_on_current_drag)
        {
            Vector2 mouse_world_position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
            Collider2D hit = Physics2D.OverlapPoint(mouse_world_position, Drawing_Layers.value);
            if (hit != null && hit.transform != null)
            {
                PenBrush(mouse_world_position);
                //current_brush(mouse_world_position);
            }
            else
            {
                previous_drag_position = Vector2.zero;
                if (!mouse_was_previously_held_down)
                {
                    no_drawing_on_current_drag = true;
                }
            }
        }
        else if (!mouse_held_down)
        {
            previous_drag_position = Vector2.zero;
            no_drawing_on_current_drag = false;
        }
        mouse_was_previously_held_down = mouse_held_down;
    }
     
    protected void OnDestroy()
    {
        ResetCanvas();
    }
     
    /// <summary>
    ///  重置畫布
    /// </summary>
    private void ResetCanvas()
    {
        drawable_texture.SetPixels(clean_colours_array);
        drawable_texture.Apply();
    }
     
    /// <summary>
    ///  筆刷
    /// </summary>
    public void PenBrush(Vector2 world_point)
    {
        Vector2 pixel_pos = WorldToPixelCoordinates(world_point);
         
        cur_colors = drawable_texture.GetPixels32();
         
        if (previous_drag_position == Vector2.zero)
        {
            MarkPixelsToColour(pixel_pos, Pen_Width, Pen_Colour);
        }
        else
        {
            ColourBetween(previous_drag_position, pixel_pos, Pen_Width, Pen_Colour);
        }
        ApplyMarkedPixelChanges();
        
        previous_drag_position = pixel_pos;
    }
     
    private Vector2 WorldToPixelCoordinates(Vector2 world_position)
    {
        Vector3 local_pos = transform.InverseTransformPoint(world_position);
 
        float pixelWidth = drawable_sprite.rect.width;
        float pixelHeight = drawable_sprite.rect.height;
        float unitsToPixels = pixelWidth / drawable_sprite.bounds.size.x * transform.localScale.x;
 
        float centered_x = local_pos.x * unitsToPixels + pixelWidth / 2;
        float centered_y = local_pos.y * unitsToPixels + pixelHeight / 2;
 
        Vector2 pixel_pos = new Vector2(Mathf.RoundToInt(centered_x), Mathf.RoundToInt(centered_y));
 
        return pixel_pos;
    }
     
    private void ColourBetween(Vector2 start_point, Vector2 end_point, int width, Color color)
    {
        float distance = Vector2.Distance(start_point, end_point);
        Vector2 direction = (start_point - end_point).normalized;
 
        Vector2 cur_position = start_point;
        float lerp_steps = 1 / distance;
 
        for (float lerp = 0; lerp <= 1; lerp += lerp_steps)
        {
            cur_position = Vector2.Lerp(start_point, end_point, lerp);
            MarkPixelsToColour(cur_position, width, color);
        }
    }
     
    private void MarkPixelsToColour(Vector2 center_pixel, int pen_thickness, Color color_of_pen)
    {
        int center_x = (int)center_pixel.x;
        int center_y = (int)center_pixel.y;

        for (int x = center_x - pen_thickness; x <= center_x + pen_thickness; x++)
        {
            if (x >= (int)drawable_sprite.rect.width || x < 0)
                continue;
 
            for (int y = center_y - pen_thickness; y <= center_y + pen_thickness; y++)
            {
                MarkPixelToChange(x, y, color_of_pen);
            }
        }
    }
    private void MarkPixelToChange(int x, int y, Color color)
    {
        int array_pos = y * (int)drawable_sprite.rect.width + x;
 
        if (array_pos > cur_colors.Length || array_pos < 0)
            return;
 
        cur_colors[array_pos] = color;
    }
     
    private void ApplyMarkedPixelChanges()
    {
        drawable_texture.SetPixels32(cur_colors);
        drawable_texture.Apply();
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C# 解析XML和反序列化的示例

    C# 解析XML和反序列化的示例

    這篇文章主要介紹了C# 解析XML和反序列化的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • 基于Silverlight打印的使用詳解,是否為微軟的Bug問題

    基于Silverlight打印的使用詳解,是否為微軟的Bug問題

    本篇文章對Silverlight打印的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • windows中使用C# 調(diào)用 C語言生成的dll

    windows中使用C# 調(diào)用 C語言生成的dll

    本文給大家介紹的是在Windows系統(tǒng)中使用C#調(diào)用C語言生成的DLL文件的一種思路,非常的簡單實用,有需要的小伙伴可以參考下
    2016-11-11
  • 基于TCP異步Socket模型的介紹

    基于TCP異步Socket模型的介紹

    本篇文章小編將為大家介紹,基于TCP異步Socket模型的介紹,需要的朋友參考下
    2013-04-04
  • C#自定義Attribute值的獲取與優(yōu)化技巧

    C#自定義Attribute值的獲取與優(yōu)化技巧

    C#自定義Attribute值的獲取是開發(fā)中會經(jīng)常用到的,大家通常使用反射進行獲取的,代碼也很簡單,今天通過本文給大家講解C#?Attribute值獲取方法,感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • C#的File類實現(xiàn)文件操作實例詳解

    C#的File類實現(xiàn)文件操作實例詳解

    這篇文章主要介紹了C#的File類實現(xiàn)文件操作的方法,非常實用,需要的朋友可以參考下
    2014-07-07
  • C#連接加密的Sqlite數(shù)據(jù)庫的方法

    C#連接加密的Sqlite數(shù)據(jù)庫的方法

    對數(shù)據(jù)加密分兩種,一種是對數(shù)據(jù)庫本身進行加密,另一種是對數(shù)據(jù)表中的數(shù)據(jù)進行加密,下面通過本文給大家介紹C#連接加密的Sqlite數(shù)據(jù)庫的方法,感興趣的朋友一起看看吧
    2017-08-08
  • C#生成PDF文件流

    C#生成PDF文件流

    這篇文章主要為大家詳細介紹了C#生成PDF文件流的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 深入c# 類和結(jié)構(gòu)的區(qū)別總結(jié)詳解

    深入c# 類和結(jié)構(gòu)的區(qū)別總結(jié)詳解

    本篇文章是對c#中類和結(jié)構(gòu)的區(qū)別進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C#實現(xiàn)洗牌算法

    C#實現(xiàn)洗牌算法

    洗牌算法的要求是這樣的:將N個數(shù)亂序后輸出.由于和撲克牌的洗牌過程比較相似所以我也就稱為洗牌算法了.很多地方都不自覺的需要這個算法的支持.也可以將這個算法擴展為從N個數(shù)中取出M個不重復(fù)的數(shù)(0<M<=N).今天我們看下如何用C#來實現(xiàn)
    2015-03-03

最新評論