Unity場景擴充套件,生成Mesh

2021-09-20 03:57:34 字數 2974 閱讀 1962

場景擴充套件:

案例1: onscenegui 實現如下效果

**:[customeditor(typeof(test))]  --函式作用是test物件能在編輯器啟用

public class myeditor : editor ;

string strpath = editorutility.openfilepanelwithfilters("載入區塊檔案", defaultpath, filters);

if (!string.isnullorempty(strpath))

regioneditor.load(strpath);

2、mesh生成

實現如下圖形

!!!注意:繪製網格三角形順序 需要嚴格按照順時針或者逆時針繪製

顏色最大值是255  m_colors[vertexidx + 0] = new color32(1, 1, 1, 255);

c#**

protected mesh _mesh;

public color color;

public float xwidth = 100;

public float ywidth = 100;

private void awake()

var meshfilter = getcomponent();

if(meshfilter == null)

meshfilter = gameobject.addcomponent();

if(_mesh == null)

_mesh = new mesh();

meshfilter.mesh = _mesh;

// use this for initialization

void start () ;

_mesh.vertices = vertices;

_mesh.********s = ********s;

updatecolor();

updateuvs(vertices);

protected void updatecolor()

color32 colors32 = new color32[_mesh.vertices.length];

for (int i = 0; i < colors32.length; i++)

colors32[i] = color;

_mesh.colors32 = colors32;

protected void updateuvs(vector3 vertices)

vector2 uvs = new vector2[vertices.length];

for (int i = 0; i < uvs.length; i++)

float u = (vertices[i].x+xwidth) / (500+2*xwidth) ;

float v = (vertices[i].y+ywidth) / (500+2*ywidth) ;

u = mathf.clamp(u,0, 1);

v = mathf.clamp(v,0, 1);

uvs[i] = new vector2(u, v);

_mesh.uv = uvs;

材質shader:

shader "los/basic"

cgprogram

#pragma vertex vert

#pragma fragment frag

#pragma target 3.0

uniform sampler2d _maintex;

uniform float _intensity;

struct vin }}

}// m_temppointlist 不包含字典key  刪除

foreach

(var key

innew

list(m_dynamictilemapdict.keys))}}

}目前載入地圖方式,通重載入prefab的名字實現

//////

根據格仔座標動態載入物體

///

///point

">

///public

,]grid"

, point.x, point.y);

gameobject prefab =

null

;try

catch

(exception e)

return

prefab;

}///

///根據格仔座標動態新增地圖

///

///pos

">

public

void

adddynamicmap(vector2int point)

}///

///根據格仔座標動態顯示已載入地圖

///

///pos

">

public

void

showdynamicmap(vector2int point)

}///

///根據格仔座標動態刪除地圖

///

///pos

">

public

void

removedynamicmap(vector2int point)

}///

///根據格仔座標動態隱藏地圖

///

///pos

">

public

void

}

Unity中mesh的合併

最近做的專案,美術給的資源很散,好多mesh需要用同乙個mesh collider,所以需要自己合併mesh,然後加乙個mesh collider 做了乙個外掛程式,在hierachy中選中物體,那麼它以及它的子物體的mesh,會合併成為乙個mesh,並且儲存到project中,可以多選,選擇幾個物...

通過Mesh生成網格

gameobject obj new gameobject go meshfilter mf obj.addcomponent meshrenderer mr obj.addcomponent 新增乙個材質球 mr.sharedmaterial resources.load material 第乙個...

Unity使用Mesh元件畫圓環

呼叫 畫圓環 依附物件 中心點 分割段數 內徑 厚度,向外擴的寬度 材質 public static void drawring gameobject gameobject,vector3 center,int segments,float innerradius,float thickness,m...