I can’t assign the sprite at the end of the script and i have no idea why, can anybody help?
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class Sprite2Canvas : EditorWindow
{
[MenuItem("Assets/Sprite to Canvas...", false, 35)]
public static void SpriteToCanvas()
{
Object selection = Selection.activeObject;
if (GameObject.Find("Canvas") == null)
{
EditorApplication.ExecuteMenuItem("GameObject/UI/Canvas");
}
GameObject canvas = GameObject.Find("Canvas");
GameObject sprite = new(selection.name, typeof(Image));
sprite.transform.SetParent(canvas.transform);
sprite.GetComponent<Image>().sprite = selection as Sprite;
}
}
Somehow it works with a RawImage but if i use an Image type it doesn’t work.
This would only work if the Selection.activeObject was cast-able to a Sprite.
Is it? Find that out first. Assign it to a temporary variable and print it out.
If you just select the Texture2D the Sprite came from, that’s not a Sprite, as any number of sprites could come from a particular texture.
1 Like
Kurt-Dekker:
This would only work if the Selection.activeObject was cast-able to a Sprite.
Is it? Find that out first. Assign it to a temporary variable and print it out.
If you just select the Texture2D the Sprite came from, that’s not a Sprite, as any number of sprites could come from a particular texture.
Thanks Kurt, it works now i’ ll leave the code here if anyone is Interested.
It puts the sprite automatically in the canvas from the Project view.
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class Sprite2Canvas : EditorWindow
{
[MenuItem("Assets/Sprite to Canvas...", false, 35)]
public static void SpriteToCanvas()
{
Object[] selection = Selection.objects;
if (GameObject.Find("Canvas") == null)
{
EditorApplication.ExecuteMenuItem("GameObject/UI/Canvas");
}
GameObject canvas = GameObject.Find("Canvas");
foreach (var selected in selection)
{
string path = AssetDatabase.GetAssetPath(selected);
Object[] sprites = AssetDatabase.LoadAllAssetsAtPath(path);
if (sprites != null)
{
foreach (Object obj in sprites)
{
if (obj.GetType() == typeof(Sprite))
{
//Create sprite
GameObject sprite = new(obj.name, typeof(Image));
sprite.transform.SetParent(canvas.transform);
sprite.GetComponent<Image>().sprite = obj as Sprite;
//Set position
RectTransform rectTrans = sprite.GetComponent<RectTransform>();
rectTrans.anchoredPosition = Vector2.zero;
sprite.GetComponent<RectTransform>().anchoredPosition = rectTrans.anchoredPosition;
//Set Size
sprite.GetComponent<Image>().SetNativeSize();
}
}
}
}
}
}
Nad_B
March 30, 2024, 12:48am
4
if (obj.GetType() == typeof(Sprite))
can be abbreviated to just if (obj is Sprite)
or even better if (obj is Sprite selectedSprite)
, which will give you the selected sprite directly, without the need to cast the object (like you do in obj as Sprite
). You could also use LINQ:
Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(path).OfType<Sprite>().ToArray();
There’s also Selection.GetFiltered<Sprite>(SelectionMode.Unfiltered)
that should return directly all selected sprites, so no need to do an AssetDatabase.LoadAllAssetsAtPath.
1 Like
Nad_B:
if (obj.GetType() == typeof(Sprite))
can be abbreviated to just if (obj is Sprite)
or even better if (obj is Sprite selectedSprite)
, which will give you the selected sprite directly, without the need to cast the object (like you do in obj as Sprite
). You could also use LINQ:
Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(path).OfType<Sprite>().ToArray();
There’s also Selection.GetFiltered<Sprite>(SelectionMode.Unfiltered)
that should return directly all selected sprites, so no need to do an AssetDatabase.LoadAllAssetsAtPath.
I tried Selection.GetFiltered(SelectionMode.Unfiltered) but it doesn’t seem to work, if i use AssetDatabase.LoadAllAssetsAtPath it returns all sprites under a single texture.