這篇文章給大家介紹Unity3D中怎么動(dòng)態(tài)生成平面網(wǎng)格,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
青龍ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!實(shí)現(xiàn)腳本如下:
//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("更新網(wǎng)格")) { 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); ////// 網(wǎng)格大小 /// [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(); //計(jì)算Plane大小 Vector2 size; size.x = _cellSize.x * _gridSize.x; size.y = _cellSize.y * _gridSize.y; //計(jì)算Plane一半大小 Vector2 halfSize = size / 2; //計(jì)算頂點(diǎn)及UV List vertices = new List (); List uvs = 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;//計(jì)算頂點(diǎn)Y軸 uv.y = y * _cellSize.y / size.y;//計(jì)算頂點(diǎn)紋理坐標(biāo)V for (int x = 0; x < _gridSize.x + 1; x++) { vertice.x = x * _cellSize.x - halfSize.x;//計(jì)算頂點(diǎn)X軸 uv.x = x * _cellSize.x / size.x;//計(jì)算頂點(diǎn)紋理坐標(biāo)U vertices.Add(vertice);//添加到頂點(diǎn)數(shù)組 uvs.Add(uv);//添加到紋理坐標(biāo)數(shù)組 } } //頂點(diǎn)序列 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];//頂點(diǎn)序列 for (int y = 0; y < _gridSize.y; y++) { for (int x = 0; x < _gridSize.x; x++) { //四邊形四個(gè)頂點(diǎn) a = y * (_gridSize.x + 1) + x;//0 b = (y + 1) * (_gridSize.x + 1) + x;//1 c = b + 1;//2 d = a + 1;//3 //計(jì)算在數(shù)組中的起點(diǎn)序號(hào) 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);//設(shè)置頂點(diǎn) mesh.SetUVs(0, uvs);//設(shè)置UV mesh.SetIndices(indexs, MeshTopology.Triangles, 0);//設(shè)置頂點(diǎn)序列 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 }
關(guān)于Unity3D中怎么動(dòng)態(tài)生成平面網(wǎng)格就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。