How to reference a sprite created in the Editor with the source sprite in the Project folder?

In my Editor it is possible to define the PPUs to use for sprites via a field in the inspector.
When I drag a sprite from the project folder into an Object field I use this code:

string path = AssetDatabase.GetAssetPath(mySprite);
Sprite sprite = AssetDatabase.LoadAssetAtPath(path, (typeof(Sprite))) as Sprite;
Texture2D myText = sprite.texture;
Sprite newSprite = Sprite.Create(myText, new Rect(0.0f, 0.0f, myText.width, myText.height), new Vector2(0.5f, 0.0f), pixelxUnit);

The code works fine, but this way I lose the connection with the original sprite.
I know one way would be to save the new sprite to disk, but I can’t.
For now I add a script to the sprite where I save the path to the original sprite, but what happens if the user moves it to another folder?
It seems to me that there is no other way to change the PPUs of a sprite other than to create a new one, right?

Do you have suggestions?

What is your intention with the first two lines?

You already have a sprite. You could get its texture.
Instead, you get that sprite’s asset path, then load the asset from disk. What you get back is the sprite you already have. … Or null, if mySprite happens not to be an asset file.

Anyhow, if you want to change import settings, you need to duplicate the asset and then modify the import settings, otherwise this change would also apply to the original sprite. You can hook into the Postprocessor to modify import settings: Unity - Scripting API: AssetPostprocessor.OnPostprocessTexture(Texture2D)

In reality this is not the case. The field in the inspector where I drag the sprite is of type Object. This is because the same field accepts both sprites and 3D objects. Then in the code I check whether it is Texture2D or GameObject. For this reason I use the two lines you refer to.
I may be taking the long way around, but I haven’t found a quicker way.
Thanks for your reply, I’ll take a look at the link.

Have you tried both variants?

If mySprite is a GameObject it would be a prefab asset. LoadAssetAtPath may actually load the first Sprite instance inside, not sure of that, but it would break at the latest when the prefab has no sprite. Or if it has multiple sprites you get the „first“ one found and there is no guarantee that „first“ will always remain the same sprite.

Normally you would call mySprite.GetComponent() (or: SpriteRenderer) to get the sprite component from an object.

I think I have solved the problem.
To be specific, when I build a new sprite on the fly with a given PPU (using a sprite loaded from the project folder), if I select the sprite in the hierarchy and look in the Sprite Renderer the field is empty.
it’s reasonable, the sprite is only in memory and as long as the scene exists the sprite also exists.

At the end of the work in the Editor, I needed to save the sprite as a prefab, but the prefab did not contain the sprite.
For this reason I thought I needed the path of the original sprite, but that’s not the case.

I said I couldn’t save the sprite to disk but I meant only when dragging the sprite into the Object field. This is because while using the Editor I could use 100 sprites but at the end of the work I might want to save only 10 as prefabs. I would therefore find 90 useless sprites on disk.

The solution I adopted is simple: before saving the prefab, I save the sprite on disk in a certain folder, reload it and assign it to the new prefab, then save the prefab in the same folder.