- Home /
how to Pass Sprite to SpriteRenderer in OnPostprocessSprites() Event?!
after importing sprites inside unity , i want to make a Prefab out Of them and assign them a SpriteRenderer and BoxCollider2D component , all automaticly. every thing is good exept i cant pass the imported Sprite to the SpriteRenderer component any how. i dont knoow what im missing here. any help will be appritiated.
void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
{
Sprite sp = sprites[0];
TextureImporter importer = (TextureImporter)assetImporter;
importer.textureType = TextureImporterType.Sprite;
GameObject GO = new GameObject();
GO.name = sp.name;
GO.AddComponent<SpriteRenderer>().sprite = sp;// not works
GO.AddComponent<SpriteRenderer>().sprite = (Sprite)Resources.Load(importer.assetPath);// not works
GO.AddComponent<BoxCollider2D>();
Object prefab = PrefabUtility.CreateEmptyPrefab(string.Format("Assets/Resources/X_Temp/{0}.prefab", GO.name));
if (prefab != null)PrefabUtility.ReplacePrefab(GO, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
there is no Error or messages , it makes the prefab with BoxCollider And SpriteRendere Components But SpriteRenderer's Sprite Field Has No Sprite, and set to missing(sprite).
how can i fix this?
also asked in forum here
@androgendo @crazy$$anonymous$$night @$$anonymous$$ikeNewall @Naphier sorry for Tag You - I saw You Folowing These kind Of Question , Any Idea On how to fix this? its driving me Crazy.
@$$anonymous$$rjmCyberman While I don't approve tagging like that, I feel your pain.
It's been a while since I gave up on editor automation out of frustration, but try OnPostprocessAllAssets. IIRC it's called after OnPostprocessSprites and you can use something like AssetDatabase.LoadAssetAtPath or Resources.Load to get the new asset. Good luck and please post an answer if you succeed.
Your answer
![](https://koobas.hobune.stream/wayback/20220612143137im_/https://answers.unity.com/themes/thub/images/avi.jpg)