Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is there another way to iterate over subassets without loading everything via a path?

Discussion in 'Scripting' started by Xelnath, Jul 14, 2022.

  1. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Code (CSharp):
    1. UnityEngine.Object[] assets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(TheMainAsset));
    This works, but is causing weirdness when I am trying to pre-process an asset before it is deserialized.

    Is there a way to iterate from the scriptable object directly?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Only if you maintain the references to the sub assets in the scriptable object yourself, which is what I've always done.
     
  3. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    That doesn't work in this case as I'm trying to iterate over every object and delete the objects which no longer have scripts.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,915
    Those two statements completely contradicts each other. In order to access / delete the objects they have to be deserialized / exist as objects in the first place. How would you imagine this would work? That you get an object reference to an object that isn't loaded / does not exist yet and then delete it?

    It's not quite clear what you want to do here. Though be careful messing around with serialized assets. You may want to look into an AssetPostProcessor instead. Though it's not quite clear what kind of asset we're talking about. Did you mix an prefab (gameobject asset) with scriptable object assets? While this might be possible, it's not quite clear how you would use it or what's your actual issue / goal you try to reach.
     
  5. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Ah, I used the wrong word. Before it is serialized. I basically have parent object that needs to be correctly linked to the child objects. Specifically, the list and certain values in the child objects needs to be kept in sync with the parent.

    I have a scriptable object that a user configures to setup a series of high level actions they want executed. When this scriptable object is done, it generates all of the little pieces into subassets. This step has worked great and I am able to do it consistently.

    However, lets say someone deletes something in the editor from the parent. I now need to go through and clean-up the children. I don't want to delete any that are still relevant. So I just make a list of the ones that are valid and remove the rest, right?

    Okay, cool, no problem.

    NOW

    Let's imagine an engineer (or me) is refactoring the low-level asset bits. Some of the old classes are invalid / no longer necessary. So you delete them. Unity begins having a panic attack and the traditional references no longer work, as some of the classes were deleted/invalid and I have no desire to bring those classes back.

    I just want to iterate over the objects and DestroyImmediate them to clean-up the object. Unity will sometimes throw an error. When it does, it leaves a naked child object with a name and no scriptable object in it. There is no way for me to access it and delete it simply. I can't hit delete in editor, in fact, I often have to open up the asset in text mode and delete that object by hand.

    upload_2022-7-14_8-49-6.png

    upload_2022-7-14_8-49-25.png
     
  6. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    To complicate things slightly, for some necessary reasons I don't want to get into, because this pre-dates me and the architectural decisions here, the child assets can have extra information that needs to be preserved to function. (Exclude the classes intentionally destroyed here)

    So 'blowing it all away and rebuilding' - my first plan - would lead to a loss of hand-crafted designer data.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    I'm not following, does the parent object have existing links to the child objects or not? If they do,
    == null
    will cover missing script cases. What's wrong with iterating through that collection and modifying it as necessary?

    If they aren't already linked then you're SOoL as there kinda isn't a clean built in API solution for this. I believe the intention was for you, the user, to managed these references yourself.

    I'm curious as to why you were deleting classes without ensuring any usage of them was already cleaned up beforehand. Doing so that way would've saved you the headache you're currently experiencing.
     
  8. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Very much a lot of "would be nice if that was a practical option" statements in there. :)

    I don't feel like you've worked with the subassets a lot based on the fact that you haven't run into these issues yourself trying to maintain a body of code. This is a huge assumption on my part, of course and I'm a little sour over dealing with them but for example:

    > does the parent object have existing links to the child objects or not? If they do,
    == null will cover missing script cases.

    The objects still exist as sub assets, even tho they have no scriptable objects on them. Very weird to me that this is possible, but it happens frequently!

    What I am very much trying to do here is capture when these issues arise and clean-up the state of the subobjects as much as possible. This also very much indicates there's a need for an API addition by unity for this. :/

    This is what it looks like when this happen:
    upload_2022-7-15_15-29-17.png

    See how there are multiple 0 and 1s subobjects? When I select them , they are empty - no inspector, no inspector errors, no way to delete or modify them. They are just ghosts sitting around in the assets forever, cluttering up the data.
     
  9. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    I also get errors like this a lot:
    upload_2022-7-15_15-31-15.png
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    I have worked with them a good bit, and have made my own node editor with graph view for handling high level game logic, too, that stores nodes as sub asset scriptable objects:
    upload_2022-7-16_9-40-20.png

    Naturally I ran into some of these complications when setting this up, so they're now at the forefront of my mind when dealing with them, and I make them known to anyone I suggest this as a solution to.

    Thing is that the 'parent' object and the 'sub assets' are all just written flatly into the same asset file. Strictly speaking, there is technically only one object, and just a bunch of data that represents multiple objects. The parent asset has no markers indicating it has sub assets (aside from any references you maintain yourself).

    From your image, it almost looks like you have 'vestigial' objects with no type nor data. At this point you might be better of writing something to trawl through the yaml; not something I have experience with though.

    I agree that it'd be nice if there was an API solution, but I think with the way the data is situated at present, they would have to make some major changes which would then become breaking changes for many. It is what it is, as I say to annoy my boss.