Texture on Terrain Change OR Swap


i have searched alot on this issue and i find nothing satisfying enough
so i am postion this question that how can i cahnge the texture of a terrain
i want to do it with code in editor mode

so far i achive some thing like this

using UnityEngine;
using System.Collections;

public class TerrainTextureChanger : MonoBehaviour {

 public Terrain terrain;
 public Texture2D MyTexture;
    void Update()
        if (Input.GetKeyDown(KeyCode.Space))
            //switch all painted in texture 1 to texture 2
            UpdateTerrainTexture(terrain.terrainData, 1, 2);
        if (Input.GetKeyUp(KeyCode.Space))
            //switch all painted in texture 2 to texture 1
            UpdateTerrainTexture(terrain.terrainData, 2, 1);
		if (Input.GetKeyDown(KeyCode.LeftControl))
	 void textureChange(){
    SplatPrototype[] splats = terrain.GetComponent<Terrain>().terrainData.splatPrototypes;
    for(int i=0;i<splats.Length;i++)
			Debug.Log (splats[1].texture);
    splats*.texture = MyTexture;*

// (Texture2D)

  • }*

static void UpdateTerrainTexture(TerrainData terrainData, int textureNumberFrom, int textureNumberTo)
//get current paint mask
float[, ,] alphas = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);
// make sure every grid on the terrain is modified

  •  Debug.Log (alphas);*

for (int i = 0; i < terrainData.alphamapWidth; i++)
for (int j = 0; j < terrainData.alphamapHeight; j++)
//for each point of mask do:
//paint all from old texture to new texture (saving already painted in new texture)
alphas[i, j, textureNumberTo] = Mathf.Max(alphas[i, j, textureNumberFrom], alphas[i, j, textureNumberTo]);
//set old texture mask to zero
alphas[i, j, textureNumberFrom] = 0f;
// apply the new alpha
terrainData.SetAlphamaps(0, 0, alphas);
This script is working and has no errors and the messages are appropriate but it dose not doing the work as defined i dnt knw why may b i am missing something
plz help me guys
the UpdateTerrainTexture which is texture swaping on button press is working fine
but textureChange method seems to have a problem it dose not chainging the texture plz
point out any thing i have missed
any help would be so helpful
Best Regards

problem solved i just tried another way to assing the spaltmaps and it worked
i was assing it worng its not the way to do it
i searched and figure out a way to do it
here is the code

 TerrainData terrainData = Terrain.activeTerrain.terrainData;
        float[, ,] splatmapData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers];
            for (int y = 0; y < terrainData.alphamapHeight; y++)
    for (int x = 0; x < terrainData.alphamapWidth; x++)
    // read the height at this location
    float height = terrainData.GetHeight(y,x);
    // determine the mix of textures 1, 2 & 3 to use
    // (using a vector3, since it can be lerped & normalized)
         Vector3 splat = new Vector3(1,0,0);
    if (height >10) {
    splat = Vector3.Lerp(splat, new Vector3(0,0,1), (height-1.0f)*1.0f );
    } else {
    splat = Vector3.Lerp(splat, new Vector3(0,1,0), height*2.23f );
    // now assign the values to the correct location in the array
    splatmapData[x, y, 0] = splat.x;
    splatmapData[x, y, 1] = splat.y;
    splatmapData[x, y, 2] = splat.z;