您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 5浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity使用Mesh Render画线

程序员正茂 发布时间:2018-10-07 23:07:46 ,浏览量:5

 

using System.Collections.Generic;
using UnityEngine;

public class TestScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
        GameObject obj = new GameObject("cube");
        MeshFilter mf = obj.AddComponent();
        MeshRenderer mr = obj.AddComponent();
        mr.sharedMaterial = Resources.Load("Mat1");

        Vector3[] ptsArr1 = new Vector3[5];
        ptsArr1[0].Set(0.0f, 0.0f, 0.0f);
        ptsArr1[1].Set(0.0f, 1.0f, 0.0f);
        ptsArr1[2].Set(3.0f, 1.0f, 0.0f);
        ptsArr1[3].Set(3.0f, 0.0f, 0.0f);
        ptsArr1[4].Set(0.0f, 0.0f, 0.0f);

        Vector3[] ptsArr2 = new Vector3[6];
        ptsArr2[0].Set(4.0f, 0.0f, 0.0f);
        ptsArr2[1].Set(4.0f, 2.0f, 0.0f);
        ptsArr2[2].Set(6.0f, 2.0f, 0.0f);
        ptsArr2[3].Set(10.0f, -1.0f, 0.0f);
        ptsArr2[4].Set(8.0f, -0.5f, 0.0f);
        ptsArr2[5].Set(4.0f, 0.0f, 0.0f);

        List indices1 = new List();
        CalIndices(ptsArr1, 0, indices1);
        Debug.Log(indices1);

        List indices2 = new List();
        CalIndices(ptsArr2, ptsArr1.Length, indices2);
        Debug.Log(indices2);

        List indicesTotal = new List();
        indicesTotal.AddRange(indices1);
        indicesTotal.AddRange(indices2);

        List ptsTotal = new List();
        ptsTotal.AddRange(ptsArr1);
        ptsTotal.AddRange(ptsArr2);

        mf.mesh.vertices = ptsTotal.ToArray();
        mf.mesh.SetIndices(indicesTotal.ToArray(), MeshTopology.Lines, 0);
    }

    void CalIndices(Vector3[] ptsArr, int startIndex, List indiceArr)
    {
        //int[] indiceArr1 = new int[2 * ptsArr.Length];
        int k = 0;
        for (int i = startIndex; i < startIndex + ptsArr.Length - 1; i++)
        {
            indiceArr.Add(i);
            indiceArr.Add(i+1);
        }

        indiceArr.Add(startIndex + ptsArr.Length - 1);
        indiceArr.Add(startIndex);
    }
}

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.1457s