工作中发现以前学的编程知识都过于表面。学校里更过的关注数据结构,还有其他知识的教学,知识拆分过于仔细,教数据结构就是数据结构,从不会穿插数据结构在什么情况下使用,网络就是网络,也没有用数据结构来搭建网络什么的。没有详细的做法。总的来说,实践太少。数据结构固然重要,但是在功能实现过程中,没有一个完整的软件编程思想贯穿整个工程是不够的。从单机,到电脑连接电脑,到电脑连接网络的程序或者说是软件编写出来后才知道,学计算机没有一个大局观和一种从01到现实世界的世界观是真不行。浅薄认知,大神勿喷!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.IO;
public class PaintView : MonoBehaviour
{
#region 属性
//绘图shader&material
[SerializeField]
private Shader _paintBrushShader;
private Material _paintBrushMat;
//清理renderTexture的shader&material
[SerializeField]
private Shader _clearBrushShader;
private Material _clearBrushMat;
//默认笔刷RawImage
[SerializeField]
private RawImage _defaultBrushRawImage;
//默认笔刷&笔刷合集
[SerializeField]
private Texture _defaultBrushTex;
//renderTexture
private RenderTexture _renderTex;
//默认笔刷RawImage
[SerializeField]
private Image _defaultColorImage;
//绘画的画布
[SerializeField]
private RawImage _paintCanvas;
//笔刷的默认颜色&颜色合集
[SerializeField]
private Color _defaultColor;
//笔刷大小的slider
private Text _brushSizeText;
//笔刷的大小
private float _brushSize;
//屏幕的宽高
private int _screenWidth;
private int _screenHeight;
//笔刷的间隔大小
private float _brushLerpSize;
//默认上一次点的位置
private Vector2 _lastPoint;
//生成透明图片
public RawImage RawImageObject;
bool isCreate=false ;
public Canvas Canvas_Paint;
//获取鼠标点
float dx;
float dy;
public int speed = 50;
int i=0;//保存图片的名字序号,每发送一次加一
string _name="";
#endregion
void Start()
{
InitData();
}
private void Update()
{
//将ui坐标转换成屏幕坐标,将鼠标坐标转换成屏幕坐标,然和进行坐标处理
Vector2 start = RectTransformUtility.WorldToScreenPoint(Camera.main ,RawImageObject.GetComponent <RectTransform >().position );
//Vector2 end = Input.GetTouch(0).position;
Vector2 end = Input.mousePosition ;
if (isCreate == true)
{
// if(Input.GetTouch (0).phase ==TouchPhase .Moved ){
//僵硬的移动
/*
RawImageObject.GetComponent<Animator>().SetBool("move",true );
RawImageObject.GetComponent<Animator>().SetBool("move", false);
* */
//if(RawImageObject.GetComponent<Animator >().GetCurrentAnimatorStateInfo(0).normalizedTime>=1.0f){
RawImageObject . transform.Translate(Vector3.Normalize(end-start )*(Vector3.Distance(start ,end )/(speed /Time.deltaTime )));//speed越大就移动越慢越柔顺
//}
//RawImageObject.transform.position = Vector3.MoveTowards(start, end, 10 * Time.deltaTime);
//float step=speed*Time.deltaTime ;
// RawImageObject.transform.localPosition=new Vector3 (Mathf.Lerp(start.x,end.x,step ),Mathf.Lerp (start.y,end.y,step ),0);
// }
}
}
#region 外部接口
public void SetBrushSize(float size)
{
_brushSize = size;
_paintBrushMat.SetFloat("_Size", _brushSize);
}
public void SetBrushTexture(Texture texture)
{
_defaultBrushTex = texture;
_paintBrushMat.SetTexture("_BrushTex", _defaultBrushTex);
_defaultBrushRawImage.texture = _defaultBrushTex;
}
public void SetBrushColor(Color color)
{
_defaultColor = color;
_paintBrushMat.SetColor("_Color", _defaultColor);
_defaultColorImage.color = _defaultColor;
}
/// <summary>
/// 选择颜色
/// </summary>
/// <param name="image"></param>
public void SelectColor(Image image)
{
SetBrushColor(image.color);
}
/// <summary>
/// 选择笔刷
/// </summary>
/// <param name="rawImage"></param>
public void SelectBrush(RawImage rawImage)
{
SetBrushTexture(rawImage.texture);
}
/// <summary>
/// 设置笔刷大小
/// </summary>
/// <param name="value"></param>
public void BrushSizeChanged(Slider slider)
{
// float value = slider.maxValue + slider.minValue - slider.value;
SetBrushSize(Remap(slider.value,300.0f,30.0f));
if (_brushSizeText == null)
{
_brushSizeText=slider.transform.Find("Background/Text").GetComponent<Text>();
}
_brushSizeText.text = slider.value.ToString("f2");
}
/// <summary>
/// 拖拽
/// </summary>
public void DragUpdate()
{
if (_renderTex && _paintBrushMat)
{
if (Input.GetMouseButton(0))
LerpPaint(Input.mousePosition);
}
}
/// <summary>
/// 拖拽结束
/// </summary>
public void DragEnd()
{
if (Input.GetMouseButtonUp(0))
_lastPoint = Vector2.zero;
}
#endregion
#region 内部函数
//初始化数据
void InitData()
{
_brushSize = 180.0f;
_brushLerpSize = (_defaultBrushTex.width + _defaultBrushTex.height) / 2.0f / _brushSize;
_lastPoint = Vector2.zero;
if (_paintBrushMat == null)
{
UpdateBrushMaterial();
}
if(_clearBrushMat==null)
_clearBrushMat = new Material(_clearBrushShader);
if (_renderTex == null)
{
_screenWidth = Screen.width;
_screenHeight = Screen.height;
_renderTex = RenderTexture.GetTemporary(_screenWidth, _screenHeight, 24);
_paintCanvas.texture = _renderTex;
}
Graphics.Blit(null, _renderTex, _clearBrushMat);
}
//更新笔刷材质
private void UpdateBrushMaterial()
{
_paintBrushMat = new Material(_paintBrushShader);
_paintBrushMat.SetTexture("_BrushTex", _defaultBrushTex);
_paintBrushMat.SetColor("_Color", _defaultColor);
_paintBrushMat.SetFloat("_Size", _brushSize);
}
//插点
private void LerpPaint(Vector2 point)
{
Paint(point);
if (_lastPoint == Vector2.zero)
{
_lastPoint = point;
return;
}
float dis = Vector2.Distance(point, _lastPoint);
if (dis > _brushLerpSize)
{
Vector2 dir = (point - _lastPoint).normalized;
int num = (int)(dis / _brushLerpSize);
for (int i = 0; i < num; i++)
{
Vector2 newPoint = _lastPoint + dir * (i + 1) * _brushLerpSize;
Paint(newPoint);
}
}
_lastPoint = point;
}
//画点
private void Paint(Vector2 point)
{
if (point.x < 0 || point.x > _screenWidth || point.y < 0 || point.y > _screenHeight)
return;
Vector2 uv = new Vector2(point.x / (float)_screenWidth,
point.y / (float)_screenHeight);
_paintBrushMat.SetVector("_UV", uv);
Graphics.Blit(_renderTex, _renderTex, _paintBrushMat);
}
/// <summary>
/// 重映射 默认 value 为1-100
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <param name="minValue"></param>
/// <returns></returns>
private float Remap(float value, float startValue, float enValue)
{
float returnValue = (value - 1.0f) / (100.0f - 1.0f);
returnValue = (enValue - startValue) * returnValue + startValue;
return returnValue;
}
void Clear(RenderTexture destTexture)
{
Graphics.SetRenderTarget(destTexture);
GL.PushMatrix();
GL.Clear(true, true, Color.clear);
GL.PopMatrix();
}
public void OnClickClear()
{
Clear(_renderTex);
}
void Create(RenderTexture destTexture) {
//将这个透明抠图传值给目标iamge
Graphics.SetRenderTarget(destTexture);
int width = Screen.width;
int heigth = Screen.height;
//创建一个屏幕大小的纹理,Rgb32
Texture2D tex = new Texture2D(width ,heigth ,TextureFormat.ARGB32 ,false );
tex = renderTtoTexture2dT(destTexture);
//读取书写内容自定义纹理
tex.ReadPixels(new Rect(0,0,width, heigth),0,0);
//保存前面对纹理的修改
tex.Apply();
//编码成PNG格式
byte[] bytes = tex.EncodeToPNG();
//销毁没用的图片纹理
Destroy(tex);
//将字节保存为图片,路径只有在PC端对图片进行读写
File.WriteAllBytes(Application.dataPath + "/签名"+ i +".png",bytes );
//这个是将图片存在手机沙盒中,手机读写
File.WriteAllBytes(Application.dataPath +"/onMekey.png",bytes );
// RawImageObject.texture = destTexture;
//让后清除写字板上的
/*GL.PushMatrix();
GL.Clear(true, true, Color.clear);
GL.PopMatrix();*/
}
public void OnClickCresate()
{
Create(_renderTex);
i = i + 1;//让序号加一
isCreate = true;
}
public Texture2D renderTtoTexture2dT(RenderTexture destTexture)
{
if(destTexture ==null )
return null ;
int width = destTexture.width ;
int height = destTexture.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
RenderTexture.active = destTexture;
tex.ReadPixels(new Rect(0,0,width,height ),0,0);
tex.Apply();
RenderTexture.active = null;
return tex;
}
#endregion
}
unity有个很大的区别就是,光有代码,没有场景,你很难看出什么。致敬初学的你。有需要留言发源文件!