久久精品国产精品亚洲精品_日日爽爽_国产免费不卡av_男人的网站你懂得_少妇和子乱视频_超碰碰人人

  • 您的位置:首頁 > 新聞動態 > Unity3D

    UNITY3D繪制可調整行列數的網格

    2019/6/12??????點擊:

    直接上代碼@@


    //PlaneBuilder.cs
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    #region Editor
    
    #if UNITY_EDITOR
    
    using UnityEditor;
    
    [CustomEditor(typeof(PlaneBuilder))]
    public class PlaneBuilderEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            PlaneBuilder builder = (PlaneBuilder)target;
    
            EditorGUI.BeginChangeCheck();
    
            base.OnInspectorGUI();
    
            if (EditorGUI.EndChangeCheck())
            {
                builder.UpdateMesh();
            }
    
            if (GUILayout.Button("更新網格"))
            {
                builder.UpdateMesh();
            }
        }
    }
    
    #endif
    
    #endregion Editor
    
    [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    public class PlaneBuilder : MonoBehaviour
    {
        [SerializeField]
        private MeshFilter _meshFilter;
    
        [SerializeField]
        private MeshRenderer _meshRenderer;
    
        ////// 單元格大小
        ///[SerializeField]
        private Vector2 _cellSize = new Vector2(1, 1);
    
        ////// 網格大小
        ///[SerializeField]
        private Vector2Int _gridSize = new Vector2Int(2, 2);
    
        public MeshRenderer MeshRenderer
        {
            get
            {
                return _meshRenderer;
            }
        }
    
        public MeshFilter MeshFilter
        {
            get
            {
                return _meshFilter;
            }
        }
    
        private void Awake()
        {
            _meshFilter = GetComponent();
            _meshRenderer = GetComponent();
            UpdateMesh();
        }
    
        public void UpdateMesh()
        {
            Mesh mesh = new Mesh();
    
            //計算Plane大小
            Vector2 size;
            size.x = _cellSize.x * _gridSize.x;
            size.y = _cellSize.y * _gridSize.y;
    
            //計算Plane一半大小
            Vector2 halfSize = size / 2;
    
            //計算頂點及UV
            Listvertices = new List();
            Listuvs = new List();
    
            Vector3 vertice = Vector3.zero;
            Vector2 uv = Vector3.zero;
    
            for (int y = 0; y < _gridSize.y + 1; y++)
            {
                vertice.z = y * _cellSize.y - halfSize.y;//計算頂點Y軸
                uv.y = y * _cellSize.y / size.y;//計算頂點紋理坐標V
    
                for (int x = 0; x < _gridSize.x + 1; x++)
                {
                    vertice.x = x * _cellSize.x - halfSize.x;//計算頂點X軸
                    uv.x = x * _cellSize.x / size.x;//計算頂點紋理坐標U
    
                    vertices.Add(vertice);//添加到頂點數組
                    uvs.Add(uv);//添加到紋理坐標數組
                }
            }
    
            //頂點序列
            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;
            int startIndex = 0;
            int[] indexs = new int[_gridSize.x * _gridSize.y * 2 * 3];//頂點序列
            for (int y = 0; y < _gridSize.y; y++)
            {
                for (int x = 0; x < _gridSize.x; x++)
                {
                    //四邊形四個頂點
                    a = y * (_gridSize.x + 1) + x;//0
                    b = (y + 1) * (_gridSize.x + 1) + x;//1
                    c = b + 1;//2
                    d = a + 1;//3
    
                    //計算在數組中的起點序號
                    startIndex = y * _gridSize.x * 2 * 3 + x * 2 * 3;
    
                    //左上三角形
                    indexs[startIndex] = a;//0
                    indexs[startIndex + 1] = b;//1
                    indexs[startIndex + 2] = c;//2
    
                    //右下三角形
                    indexs[startIndex + 3] = c;//2
                    indexs[startIndex + 4] = d;//3
                    indexs[startIndex + 5] = a;//0
                }
            }
    
            //
            mesh.SetVertices(vertices);//設置頂點
            mesh.SetUVs(0, uvs);//設置UV
            mesh.SetIndices(indexs, MeshTopology.Triangles, 0);//設置頂點序列
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            mesh.RecalculateTangents();
    
            _meshFilter.mesh = mesh;
        }
    
    #if UNITY_EDITOR
    
        private void OnValidate()
        {
            if (null == _meshFilter)
            {
                _meshFilter = GetComponent();
            }
            if (null == _meshRenderer)
            {
                _meshRenderer = GetComponent();
                if (null == _meshRenderer.sharedMaterial)
                {
                    _meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
                }
            }
        }
    
    #endif
    }


    主站蜘蛛池模板: 看女人毛片| 91精品一区二区三区久久久久久 | 成人午夜在线 | 伦一理一级一a一片 | 亚洲精品在线观看免费 | 宅男撸66国产精品 | 99久久精品一区二区成人 | 厨房少妇人妻好深太紧了 | 国产成人青青热久免费精品 | av在线中文| 成人午夜久久 | 久久精品99久久久久久2456 | 在线播放日本 | 日韩精品一区二区免费视频 | 精品一区2区三区 | 97碰成人国产免费公开视频 | 国内精品一级毛片 | 妺妺窝人体色777777 | 国产亚洲视频在线观看网址 | 自拍偷区亚洲国内自拍蜜臀 | 香蕉尹人综合在线观看 | 国产日韩欧美综合色视频在线 | 色视频在线 | 精品国产乱码久久久久久牛牛 | 国产丰满麻豆videossexhd 一级毛片免费 | 午夜精品成人 | av中文字幕在线观看 | 亚洲国产精品成人综合色在线婷婷 | 色久影院 | 高清欧美性猛交xxxx | 国产精品最新免费视频 | 国产香蕉精品视频老妇 | 精品国产31久久久久久 | 网站av在线 | 国产91丝袜香蕉在线播放 | 激情中国色综合 | 99久久综合国产精品 | "中文字幕 久热精品 视频在线" | 一区二区免费看 | 天天模夜夜肏狠狠的操 | 无码AⅤ最新AV无码专区 |