title: Unity Editor Pipeline Scripting: how to Pass Sprite to SpriteRenderer in OnPostprocessSprites() Event?
body: <p>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.</p>
  
  <pre><code>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;
          string ResAddress = importer.assetPath.Remove(importer.assetPath.Length - 4, 4).Replace("Assets/Resources/", "");
      Debug.Log("Resource Address = " + ResAddress);//  Images/Objects/car      acceptable for Resource.load
  
          //GO.AddComponent&lt;SpriteRenderer&gt;().sprite = sp;// not works
          GO.AddComponent&lt;SpriteRenderer&gt;().sprite = (Sprite)Resources.Load(ResAddress);// not works
  
          GO.AddComponent&lt;BoxCollider2D&gt;();
  
          Object prefab = PrefabUtility.CreateEmptyPrefab(string.Format("Assets/Resources/X_Temp/{0}.prefab", GO.name));
         if (prefab != null)PrefabUtility.ReplacePrefab(GO, prefab, ReplacePrefabOptions.ConnectToPrefab);
  
      }
  </code></pre>
  
  <p>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 none or Missing.</p>
  
  <p>how can i fix this? 
  also asked in Unity Community And No luck.</p>
  
  <p><a href="https://answers.unity.com/questions/1434068/how-to-pass-sprite-to-spriterenderer-in-onpostproc.html" rel="nofollow noreferrer">https://answers.unity.com/questions/1434068/how-to-pass-sprite-to-spriterenderer-in-onpostproc.html</a></p>
  
  <p><a href="https://forum.unity.com/threads/how-to-pass-sprite-to-spriterenderer-in-onpostprocesssprites-event.505638/" rel="nofollow noreferrer">https://forum.unity.com/threads/how-to-pass-sprite-to-spriterenderer-in-onpostprocesssprites-event.505638/</a></p>
  
  <p>====== UPDATE And NOT RECOMMENDED ANSWER ======</p>
  
  <pre><code>void OnPostprocessSprites(Texture2D texture, Sprite[] sprites)
  {
      TextureImporter importer = (TextureImporter)assetImporter;
      if (AssetDatabase.LoadAssetAtPath&lt;Sprite&gt;(importer.assetPath)==null)
      {
          AssetDatabase.Refresh();
          return;
      }
  
      foreach (Sprite sp in sprites)
      {
          importer.textureType = TextureImporterType.Sprite;
  
          GameObject GO = new GameObject();
          GO.name = sp.name;
  
          Sprite sprite = AssetDatabase.LoadAssetAtPath&lt;Sprite&gt;(importer.assetPath);
          GO.AddComponent&lt;SpriteRenderer&gt;().sprite = sprite;
  
          string address = string.Format("Assets/Resources/Prefabs/{0}.prefab", GO.name);
  
          PrefabUtility.CreatePrefab(address, GO);
          PrefabUtility.ReconnectToLastPrefab(GO);
      }
  }
  </code></pre>
  
  <p>not recommended Because Of the following Crash Error :</p>
  
  <pre><code> A default asset was created for 'Assets/Resources/Images/Objects/car.png' because the asset importer crashed on it last time.You can select the asset and use the 'Assets -&gt; Reimport' menu command to try importing it again, or you can replace the asset and it will auto import again.UnityEditor.AssetDatabase:Refresh()
  </code></pre>