Search Unity

The alphamap is miss when terrainData is save use CreateAsset

Discussion in 'World Building' started by chena_cpp, Dec 19, 2019.

  1. chena_cpp

    chena_cpp

    Joined:
    Oct 27, 2014
    Posts:
    24
    I want get a copy terrainData B of a exist terrainData A.

    Code (CSharp):
    1.  
    2. TerrainData terrainData = new TerrainData();                  
    3. // copy terrain data
    4. terrainData.RefreshPrototypes();
    5. Terrain workTerrain = (Terrain)Terrain.CreateTerrainGameObject(terrainData).GetComponent(typeof(Terrain));
    6. workTerrain.Flush();
    7.  
    The terrain render correctly, but if i save the terrainData B, it's alphamap is miss, don't render correctly.
    Code (CSharp):
    1. AssetDatabase.CreateAsset(terrainData, newAssetPath);
    Why?
    Thanks.
     
  2. chena_cpp

    chena_cpp

    Joined:
    Oct 27, 2014
    Posts:
    24
    I call SyncTexture on the terrainData, it report a argumentexception:
    ArgumentException: Unrecognized terrain texture name: "SplatAlpha 0"

    Code (CSharp):
    1.  
    2. for (int j = 0; j < terrainData.alphamapTextureCount; j++)
    3. {
    4.     terrainData.SyncTexture(terrainData.alphamapTextures[j].name);
    5. }
    6.  
     
  3. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    You have to use the function with a hardcoded string to indicate you want to sync a splatmap.
    Code (CSharp):
    1. terrainData.SyncTexture(TerrainData.AlphamapTextureName);
    Only have to call this once, it will sync all the splatmaps the terrain contains. You can find the source code reference here: https://github.com/Unity-Technologi...Modules/Terrain/Public/TerrainData.GPUCopy.cs
     
    chena_cpp likes this.
  4. chena_cpp

    chena_cpp

    Joined:
    Oct 27, 2014
    Posts:
    24
    Thanks.
    SyncTexture work now, but if i call CreateAsset, the alphamap is still missed.
     
    RatherGood likes this.