unity 实现划线功能

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

实现划线功能并保存成图片:

使用UGUI拓展中的UILineRender,该资源UnityUIExtensions-2019-6.unitypackage可以在其他人的分享中找到


效果:

一、实现思路

制作UILineRender预制体:

设置transform和UILineRender组件的颜色和线条粗细。

二、核心代码

1.划线

代码如下(示例):

 public List<Vector2> points = new List<Vector2>();
 Vector3 point;
 UILineRenderer currentLine;//当前line
 public bool isSave;//是否正在保存
 void Update()
 {
     if (isSave) return;
     //window 或编辑器
     if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
     {

         WindowDrawLine();


     }//安卓
     else if (Application.platform == RuntimePlatform.Android)
     {

     }
     
 }
/// <summary>
  /// window平台划线
  /// </summary>
  private void WindowDrawLine()
  {
      if (Input.GetMouseButton(0))
      {

          if (this.points.Count == 0)
          {
              currentLine = CreatLine();
          }
          if (currentLine!=null&&point != Input.mousePosition)
          {
              point = Input.mousePosition;
              points.Add(Input.mousePosition);
              currentLine.Points = this.points.ToArray();
          }
      }
      if (Input.GetMouseButtonUp(0))
      {
          if (this.currentLine != null)
          {
              this.point = Vector3.zero;
              this.points.Clear();
              this.currentLine = null;
          }
      }
  }

2.完整代码

代码如下(示例):

using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using UnityEngine;

using UnityEngine.UI.Extensions;

/// <summary>
/// 划线image
/// </summary>

public class DrawLineImage : MonoBehaviour
{
   
    public GameObject linePre;//线的预制体
    public Transform lineRoot;//线的根节点
    public List<UILineRenderer> lines= new List<UILineRenderer>();//所有线条
    public GameObject btns;//功能按钮
    public GameObject tip;//提示消息
    void Start()
    {
    
    }
 
    public List<Vector2> points = new List<Vector2>();
    Vector3 point;
    UILineRenderer currentLine;//当前line

    public bool isSave;//是否正在保存
    void Update()
    {
        if (isSave) return;
        //window 或编辑器
        if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
        {

            WindowDrawLine();


        }//安卓
        else if (Application.platform == RuntimePlatform.Android)
        {

        }
        
    }
    /// <summary>
    /// window平台划线
    /// </summary>
    private void WindowDrawLine()
    {
        if (Input.GetMouseButton(0))
        {

            if (this.points.Count == 0)
            {
                currentLine = CreatLine();
            }
            if (currentLine!=null&&point != Input.mousePosition)
            {
                point = Input.mousePosition;
                points.Add(Input.mousePosition);
                currentLine.Points = this.points.ToArray();
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            if (this.currentLine != null)
            {
                this.point = Vector3.zero;
                this.points.Clear();
                this.currentLine = null;
            }
        }
    }
    /// <summary>
    /// 创建line
    /// </summary>
    /// <returns></returns>
    private UILineRenderer CreatLine()
    {
        GameObject go=Instantiate(linePre,this.lineRoot);
        go.SetActive(true);
        UILineRenderer ui=go.GetComponent<UILineRenderer>();
        lines.Add(ui);
        return ui;
    }

   
    /// <summary>
    /// 将Texture2D转png
    /// </summary>
    public void SavePng()
    {
        StartCoroutine(StartSavePng());
    }
    IEnumerator StartSavePng()
    { 
        this.isSave = true;
        this.btns.SetActive(false);
        yield return new WaitForEndOfFrame();
        Texture2D tex = ScreenCapture.CaptureScreenshotAsTexture();
        byte[] bs= tex.EncodeToPNG();
        //保存
        this.tip.SetActive(true);

        using (FileStream fs=new FileStream(Application.streamingAssetsPath+"/签名"+DateTime.Now.Ticks+".png",FileMode.CreateNew,FileAccess.Write,FileShare.Read))
        {
            fs.Write(bs);
        }
        this.btns.SetActive(true);
        this.tip.SetActive(false);
        this.isSave = false;
#if UNITY_EDITOR
        AssetDatabase.Refresh();
#endif
    }
    /// <summary>
    /// 清除
    /// </summary>
    public void OnClearLine()
    {
        this.currentLine=null;
        this.points.Clear();
        this.point = Vector3.zero;
        foreach (var item in lines)
        {
            DestroyImmediate(item.gameObject);
        }
        lines.Clear();
    }
}


总结

以上划线功能实现简单,需要用到UI拓展,功能也比较简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御键

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值