unity实现画板功能

如题、公司想做早教方面的小游戏、文档中有连线需求

找了几个帖子、直接复制粘贴、运行、不是自己想要的功能、于是尝试改、

逻辑看的我头大、一个List<vector3> 就搞定的逻辑 

非要写vector3[]

然后又自己去封装数组的自增

public class DrawLine : MonoBehaviour {

    public Shader shader;

    private static Material m;

    private GameObject g;

    private List spppp;

    private GUIStyle labelStyle;

    private GUIStyle linkStyle;

    List> linesPoint;

    void Start () {

        labelStyle = new GUIStyle();

        labelStyle.normal.textColor = Color.black;

        linkStyle = new GUIStyle();

        linkStyle.normal.textColor = Color.blue;

        spppp = new List ();

        m = new Material(shader);

        g = new GameObject("g");

        linesPoint = new List> ();

    }

    /** Replace the Update function with this one for a click&drag drawing option */

    void Update() {

        Vector3 e;

        if(Input.GetMouseButtonDown(0)) {

            spppp = new List ();

            List points = new List ();

            linesPoint.Add (points);

        }

        if(Input.GetMouseButton(0)) {

            spppp.Add (GetNewPoint ());

            linesPoint [linesPoint.Count-1].Add (GetNewPoint());

        }

        if(Input.GetMouseButtonUp(0)) {

        }

    }

    Vector3 GetNewPoint() {

        return g.transform.InverseTransformPoint(

            Camera.main.ScreenToWorldPoint(

                new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z * -1.0f)

            )

        );

    }

    void OnPostRender() {

        m.SetPass(0);

        GL.PushMatrix();

        GL.MultMatrix(g.transform.transform.localToWorldMatrix);

        GL.Begin( GL.LINES );

        GL.Color( new Color(1,1,1,1) );

        for (int i = 0; i < spppp.Count-2; i++) {

            GL.Vertex3 (spppp [i].x, spppp [i].y, spppp [i].z);

            GL.Vertex3 (spppp [i+1].x, spppp [i+1].y, spppp [i+1].z);

        }

        for (int i = 0; i < linesPoint.Count; i++) {

            for (int j = 0; j < linesPoint[i].Count-1; j++) {

                GL.Vertex3 (linesPoint [i][j].x, linesPoint [i][j].y, linesPoint [i][j].z);

                GL.Vertex3 (linesPoint [i][j+1].x, linesPoint [i][j+1].y, linesPoint [i][j+1].z);

                //GL.Vertex3 (spppp [j+1].x, spppp [j+1].y, spppp [j+1].z);

            }

        }

        GL.End();

        GL.PopMatrix();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • thiele插值算法 1点插值算法 function [C,c]=thiele(X,Y,Z)%X为插值点横坐标,Y...
    00crazy00阅读 2,179评论 0 4
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,142评论 0 2
  • 1 着色器和程序(Shaders and Programs) 1.1 着色器语言(Language Overvie...
    RichardJieChen阅读 9,967评论 3 12
  • 小学算数理解一下交易的价值:某商品A原来单价10元,买进。次日涨到15元,卖出。第三日跌到7.5元再买进,此时总共...
    问基阅读 853评论 0 0
  • 前往竹海的路上,接近午饭时间,嫂嫂找了一户不错的农家,感受一下江南的农家乐。晒太阳的狗狗 午饭,四个菜:红烧土鸡,...
    bigboss妈咪阅读 174评论 0 0

友情链接更多精彩内容