介绍
这篇文章将为大家详细讲解有关团结如何实现绘画功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
本文实例为大家分享了团结实现绘画功能的具体代码,具体内容如下
直接先上效果:
gif里面有些颜色不一样是gif功能导致的,绘制出来的都是同一个颜色。
原理其实也简单,通过一些列的坐标转换得到当前绘制的坐标,然后根据画笔的宽度计算像素数量,最后填充像素块颜色。
备注:
纹理必须在导入设置中设置了是可读的标志
Texture2D。SetPixels:设置像素颜色块。
Texture2D。应用:实际应用任何先前的SetPixels更改。
直接上代码吧:
using 系统; 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 颜色[],clean_colours_array; ,,,private Collider2D [], rayResult =, new Collider2D [2]; ,,,private Color32 [], cur_colors; ,,,, ,,,private bool no_drawing_on_current_drag =,假; ,,,private bool mouse_was_previously_held_down =,假; ,,,, ,,,void 清醒() ,,,{ ,,,,,,,drawable_sprite =, this.GetComponent() .sprite; ,,,,,,,drawable_texture =, drawable_sprite.texture; , ,,,,,,,clean_colours_array =, new 颜色[(int) drawable_sprite.rect.width *, (int) drawable_sprite.rect.height]; ,,,,,,,clean_colours_array =, drawable_texture.GetPixels (); ,,,} ,,,, ,,,void 更新() ,,,{ ,,,,,,,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 (Drawing_Layers.value mouse_world_position也); ,,,,,,,,,,,if (hit !=, null ,,, hit.transform !=, null) ,,,,,,,,,,,{ ,,,,,,,,,,,,,,,PenBrush (mouse_world_position); ,,,,,,,,,,,,,,,//current_brush (mouse_world_position); ,,,,,,,,,,,} ,,,,,,,,,,, ,,,,,,,,,,,{ ,,,,,,,,,,,,,,,previous_drag_position =, Vector2.zero; ,,,,,,,,,,,,,,,if (mouse_was_previously_held_down !) ,,,,,,,,,,,,,,,{ ,,,,,,,,,,,,,,,,,,,no_drawing_on_current_drag =,真的; ,,,,,,,,,,,,,,,} ,,,,,,,,,,,} ,,,,,,,} ,,,,,,,else if (mouse_held_down !) ,,,,,,,{ ,,,,,,,,,,,previous_drag_position =, Vector2.zero; ,,,,,,,,,,,no_drawing_on_current_drag =,假; ,,,,,,,} null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null 团结如何实现绘画功能