Skip to content

Conversation

@arkms
Copy link
Contributor

@arkms arkms commented May 17, 2025

Search sub-assets now works with Sprite Atlas. This means enabled search sub assets is going to search also references to sprites that are part of the SpriteAtlas

Search sub-assets now works with Sprite Atlas
@yasirkula
Copy link
Owner

It makes sense. But we can simplify the codebase; and I believe your code won't fetch Sprites from a folder if that folder is added to SpriteAtlas. To accomplish both of these tasks, we can use an internal Unity function that directly returns the Sprites that are added to a SpriteAtlas. This internal function is already used once in the codebase:

Sprite[] packedSprites = spriteAtlasPackedSpritesGetter( (SpriteAtlas) result );

Can you make it so that spriteAtlasPackedSpritesGetter is internal static, move it outside of #if ASSET_USAGE_ADDRESSABLES and use it to simplify your code?

@yasirkula
Copy link
Owner

BTW I now target Unity 2021.3.41f1 so you can remove #if UNITY_2017_1_OR_NEWER safely.

@arkms
Copy link
Contributor Author

arkms commented May 18, 2025

Sounds good, let me update the code and let you know.

@arkms
Copy link
Contributor Author

arkms commented May 19, 2025

You are right, my version doesn't handle if the reference is a folder, but using the function you recommended, it works and the code is cleaner.

Copy link
Owner

@yasirkula yasirkula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. I've requested a few final changes, please have a look.

}

// If is an Atlas we get sprites directly as GetSubAssets return the final texture
if ( target is UnityEngine.U2D.SpriteAtlas spriteAtlas )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can you add a using statement for SpriteAtlas instead?
  • Since the new code's style is almost the same as the overall coding style of the plugin, I'll ask you to remove the space between if and the opening parantheses to match the style exactly.

subAssets.Add( new SubAsset( packedSprites[i], shouldSearchChildren ?? true ) );
}
}
return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return makes the code slightly faster but I'd like it to be removed for safety. Imagine SpriteAtlases having sub-assets in the future. That would not work with the return statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the reason I add the return is because the current add sub assets add the atlas texture and an object with no name to the sub assets list.

{
for( int i = 0; i < packedSprites.Length; i++ )
{
if ( currentSubAssets.Add( packedSprites[i] ) )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found out that the returned Sprite can be null if it's deleted while the SpriteAtlas hasn't been repacked/rebuilt yet. So a null check is necessary here.

@arkms
Copy link
Contributor Author

arkms commented May 19, 2025

All changes done

Comment on lines 107 to 111
if( packedSprites[i] )
{
if ( currentSubAssets.Add( packedSprites[i] ) )
subAssets.Add( new SubAsset( packedSprites[i], shouldSearchChildren ?? true ) );
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes.

For simplicity's sake, I'd like these if conditions to be combined. I can do it myself after the merge but Blame'ing those lines would then show my commit rather than yours. Which one would you prefer?

PS. I used to do if( packedSprites[i] ) a lot but I'm now using if( packedSprites[i] != null ) because I believe it's easier to understand. So if you were to combine these ifs, could you also modify this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it easier to understand and how this run on the Editor I don't thing is an issue, I used to do a null check without explicit != null because Unity said is faster and safer, but that's apply for runtime, on Editor I don't thing we care about that.

Copy link
Owner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That blog post is valid and it talks about the IsNativeObjectAlive function that CompareBaseObjects uses. Both implicit bool operator and explicit == operator call the same function so there can't be a performance difference between them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, and by the way thanks for great tool.

@yasirkula yasirkula merged commit 5104901 into yasirkula:master May 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants