Search Unity

Texture2D as well as Texture3D are ignored when saving prefabs.

Discussion in 'Prefabs' started by cubrman, Nov 25, 2018.

  1. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Hey, I am using 2018.3.b10

    I have a field like this:


    [HideInInspector][SerializeField]
    public Texture3D Texture3DSerialized;

    And it is basically being ignored as well as nullified when I save a prefab. On the other hand, it is being saved when I save the scene perfectly well (into the scene). I tried making the field visible in inspector and it did nothing. The same problem is with Texture2D. My ints work perfectly though. How can I understand where I am wrong?
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Is the texture an existing asset or are you generating it on the fly?

    References to existing asset should work fine. If you are generating a Texture you need to save it as an asset in order for the Prefab Asset to be able to reference it.

    The reason this is not needed for scenes is that Asset types that are not already saved as Assets are saved directly into a scene if they are being referenced from a scene object.
     
  3. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    I know this is not your department but man... Being able to save data assets (textures, meshes, scriptableobjects, etc) inside of prefabs would enable some really powerful workflows...
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Right. You can do that yourself via scripting currently, but we don't have workflows for it built into the editor at this time.

    We know it would be nice with integrated workflows too. Just see this hackweek project. Maybe one day...
     
    M_R and Lurking-Ninja like this.
  5. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    That hackweek project was exactly what I had in mind when I posted! Oh, and I see you were part of that team! :)
    Shame the video only has 500 views, this kind of feature would be so awesome.
     
    runevision likes this.
  6. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Wow, I thought my question was one of those stupid-enough-to-simply-be-ignored (c).

    Thanks for the response! I have already figured out just what you told be in your answer and I've made my 3D textures to save as assets into my project folders and get referenced and saved OK, BUT! Now the references get messed up when I build the project (they are nulls). I have and idea why this can be the case, but if you could think of a common problem here I would love to hear it.
     
  7. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Show us your assigning/referencing code. I have a feeling it might be that you aren't marking the object/scene as dirty when you assign the reference, so it will look like it works until you build it or restart unity (then your reference will become null)
     
  8. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Here is the relevant part of my code:

    I am converting a 2D LUT texture into 3D

    Code (CSharp):
    1.  
    2.                 var texture3D = new Texture3D(dim, dim, dim, TextureFormat.ARGB32, false);
    3.                 texture3D.SetPixels(newC);
    4.                 texture3D.Apply();
    5.                 texture3D.filterMode = FilterMode.Trilinear;
    6.                 texture3D.name = LUT2D.name;
    7.                 string path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(newTexture2D));
    8.                 AssetDatabase.CreateAsset(texture3D, path + "/" + texture3D.name + ".cube");
    9.  
    10.                 Texture3DSerialized = texture3D;

    I've managed to reproduce the problem in the editor. I have a
    #if UNITY_EDITOR 
    branch which, once disabled, reproduces the issue. But the problem seems non-trivial. I think I have an idea what is happening: I am saving my converted 3d assets as "*.cube" files and afterwards I cannot assign them to the Texture3DSerialized slot (at least manually through inspector window). Can it be the reason? What should the extension be?
     
  9. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
  10. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    It did not help. I can now assign the created asset, but sometimes the reference is saved during build while other times it is lost. I am assigning the Texture3DSerialized field through script, but once I build the project the field is null. On the other hand, if I assign it manually through - it gets saved. Maybe I need to call some method in my script after I assign the value?

    Yeah, I've just confirmed it - if I save it through code, my changes are ignored while everything I do manually is saved.
     
    Last edited: Nov 29, 2018
  11. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Ok I made it work:


    Code (CSharp):
    1.  
    2. AssetDatabase.CreateAsset(texture3D, path + "/" + texture3D.name + ".cubemap");
    3.  
    4. //Texture3DSerialized = texture3D;
    5.  
    6. var otherSerializedObj = new SerializedObject(this);
    7. otherSerializedObj.FindProperty("Texture3DSerialized").objectReferenceValue = texture3D;
    8. otherSerializedObj.ApplyModifiedProperties();
    This looks weird... Maybe it's fine somehow and I simply never encountered this before... But this does look really frickin' weird.

    Redo does not work properly with this system... This is beyond weird...

    This line:

    Code (CSharp):
    1. var otherSerializedObj = new SerializedObject(this);
    Constantly throws errors:

    ArgumentException: Object at index 0 is null

    I really hope there is a more sane way of doing this...

    I am really really sad... Nothing works just nothing at all... it just throws errors all the time... I could never have expected such a trap from Unity...
     
    Last edited: Nov 29, 2018