#Type mismatch when copying asset with subassets

1 messages · Page 1 of 1 (latest)

small schooner
#

I have this SO with a gradient field that generates a Texture2D that I add into the AssetDatabase as a subAsset of the SO. Problem is that only sometimes will some these assets have a "Type mismatch" in the texture field for some odd reason. When I try to log the texture field to check its type, they're always the same Texture2D but sometimes I see logs coming from a "worker0", "worker1", etc. instead of the usual name of the SO, which I suspect is when those type mismatch happen.

Any idea what could going on?

gentle path
small schooner
# gentle path can you show the code for how you're generating and saving it? The Type Mismatch...

sure thing, here you go

public class ColorPalette : ScriptableObject
{
    public Gradient m_gradient = DefaultGradient();

    [SerializeField, Range(2, 256)]
    private int m_resolution = 256;

    [SerializeField, ReadOnly]
    private Texture2D m_gradientTexture;

    public Texture2D GradienTexture => m_gradientTexture;

    public static Gradient DefaultGradient()
    {
        // ...
    }

    private void OnEnable()
    {
        if (m_gradientTexture == null)
        {
            UpdateGradientTexture();
            AssetDatabase.AddObjectToAsset(m_gradientTexture, this);
        }
    }

    private void OnValidate()
    {
        if (m_gradientTexture != null)
            m_gradientTexture.name = name;

        UpdateGradientTexture();
    }

    private void UpdateGradientTexture()
    {
        var subAssets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(this));

        if (subAssets != null && subAssets[0] != m_gradientTexture)
        {
            m_gradientTexture = subAssets[0] as Texture2D;
        }

        if (m_gradientTexture == null || !AssetDatabase.IsSubAsset(m_gradientTexture))
        {
            m_gradientTexture = new Texture2D(m_resolution, 1, TextureFormat.RGBA32, false)
            {
                name = name,
                wrapMode = TextureWrapMode.Clamp,
                filterMode = FilterMode.Point,
            };
        }

        for (int x = 0; x < m_resolution; x++)
            m_gradientTexture.SetPixel(x, 0, m_gradient.Evaluate((float)x / (m_resolution - 1)));
        m_gradientTexture.Apply();
    }
}
gentle path
#

Have you debugged this?
It looks like it's never:

  1. Saving the asset
  2. adding the texture to the asset if it's not there already
small schooner
#

oh, forgot I left that part about checking the subassets... that was a quick fix after I renamed the gradient field and was getting mismatch then

#

hard to debug because I can't consistantly reproduce it, seems to happen randomly

gentle path
#

Oh wait now I see the AssetDatabase.AddObjectToAsset(m_gradientTexture, this); call - but that seems like it shouldn't be running on every OnEnable right?

gentle path
small schooner
#

it only runs once as far as I know, if it ran multiple times it would be giving out errors like I've had happen before

gentle path
#

runs once when

#

every time you load the project probably/possibly

small schooner
#

when the file gets imported/saved to disk, I'd assume

#

hmm haven't tried reloading yet

gentle path
#

Let's not make assumptions 😉

#

Debug.Log!

small schooner
#

either way, the texture should be serialized, no?

#

so it wouldn't run again as long as it has a non-null texture

gentle path
#

sure but if it doesn't save and then there's a domain reload, it will go away

small schooner
#

hmm, OnEnable is called twice immediately after the file is saved to disk

#

reloading the project runs once on each ColorPalette asset and texture isn't null

#

can't have it mismatch anymore even though the only code change are debug logs... I swear this thing knows it's getting observed

#

finally removed the debug call and they all turned into type mismatch after the domain reload

#

copying an asset with a type mismatch logs there's already a texture in that copy, with the texture already assigned to the new name 🤔 and it's called 3 times

small schooner
#

hmm... well here's something interesting:
if I create an asset, save to disk and rename it, the subasset is also renamed, but the texture field continues showing the old asset name until the next domain reload