Search Unity

Terrain Data Serialization (Textures)

Discussion in 'Scripting' started by Sean_Mcelholm, Jul 18, 2019.

  1. Sean_Mcelholm

    Sean_Mcelholm

    Joined:
    Jan 29, 2014
    Posts:
    20
    Hi,

    I'm attempting to write a system where I can create terrains in the Unity editor using the built-in terrain system to allow modders to do their magic.

    I've got the terrain heightmap saving and loading correctly, but I'm stumped when it comes to saving and applying textures correctly.

    Most answers for applying textures use the old splatmap system which has since been made obsolete (hence why I would prefer not to use it), I'm trying to save the texture data into a binary file, it saves without any script errors but it does not appear to load textures giving a UnityException

    Here is the error in question:
    Code (csharp):
    1. UnityException: LoadRawTextureData: not enough data provided (will result in overread).
    2. UnityEngine.Texture2D.LoadRawTextureData (System.Byte[] data) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Graphics/Texture.cs:580)
    3. Core.World.TerrainManager+<InternalLoadTerrain>d__12.MoveNext () (at Assets/Scripts/Core/World/TerrainManager.cs:85)
    4. UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

    Here is a gist showing the code in question:
    Code (csharp):
    1. gist.github.com/seanny/5cfe1d98ab371798d96589a02cdc2683
    Regards,
    Seanny.