2015-08-28 72 views
0

我在Unity創建了一個簡單的Texture2D如下(簡化的例子):在Unity地形上設置生成的Texture2D?

Texture2D texture2d = new Texture2D(256,256); 
    for (int h = 0; h < texture2d.height; h++) { 
     for (int w = 0; w < texture2d.width ; w++) { 
      texture2d.SetPixel(w,h, Color.green); 
     } 
    } 

我怎樣才能設置此創建的Texture2D在現有的地形,如地面紋理?

回答

1

由於內置的​​Terrain着色器沒有_MainTexture屬性,因此您必須使用自定義屬性: http://wiki.unity3d.com/index.php/TerrainFourLayerToon(在.shader文件的地方)

從那裏,你只需要適當的代碼

//your code here 
texture2d.Apply(); //missing in your code! 

Material mat = new Material(Shader.Find("Terrain/Four Layer Toon Compat")); //from link 
mat.SetTexture("_MainTex" , texture2d); //set the texture properties of the shader 
mat.SetTexture("_MainTex2", texture2d); 
mat.SetTexture("_MainTex3", texture2d); 
mat.SetTexture("_MainTex4", texture2d); 

Terrain terr = this.gameObject.GetComponent<Terrain>(); //get the terrain 
terr.materialType = Terrain.MaterialType.Custom; //tell the terrain you want to manually assign its material 
terr.materialTemplate = mat; //finally assign the material (and texture) 

此代碼應團結奮鬥5

如果你是好奇了任何的這些功能做的,檢查https://docs.unity3d.com/Manual/SL-Reference.htmlShaderLabCG.shader文件類型

http://docs.unity3d.com/ScriptReference/UnityEngine下 - >ClassesGameObject,Material,Shader,TerrainTexture2D