- Home /
Question by
AlanGreyjoy · Nov 13, 2020 at 03:48 PM ·
texture2dsprites
Trying to assign a sprite, but getting Type missmatch
I am selecting all objects, then I filter out the textures, then I create a sprite, then I assign it. The problem I am having, is I am getting a "Type missmatch" error.
Code
public void StartBulkChangeIcons()
{
if (Selection.objects.Length == 0) return;
Texture2D[] textures = Selection.GetFiltered<Texture2D>(SelectionMode.Unfiltered);
GameObject[] gameObjects = Selection.GetFiltered<GameObject>(SelectionMode.Unfiltered);
Debug.LogFormat("Total selected Textures: {0}", textures.Length);
Debug.LogFormat("Total selected GameObjects: {0}", gameObjects.Length);
for (int i = 0; i < gameObjects.Length; i++)
{
Sprite sprite = GetThumbnail(textures, gameObjects[i].name);
if (sprite != null)
{
gameObjects[i].GetComponent<PieceBehaviour>().Icon = sprite;
}
}
}
public Sprite GetThumbnail(Texture2D[] textures, string target)
{
Sprite sprite = null;
for (int i = 0; i < textures.Length; i++)
{
if (textures[i].name.Replace("_thumbnail", "") == target)
{
sprite = Sprite.Create(textures[i], new Rect(0.0f, 0.0f, textures[i].width, textures[i].height), new Vector2(0.5f, 0.5f), 100.0f);
sprite.name = textures[i].name;
return sprite;
}
}
return sprite;
}
capture1.png
(22.1 kB)
Comment
Answer by AlanGreyjoy · Nov 13, 2020 at 04:02 PM
AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(textures[i]));
Your answer
Follow this Question
Related Questions
Minimizing Texture Size iOS 2 Answers
Image is crushed --- Sprite 2 Answers
"alphaHitTestMinimumThreshold" and "Sprite.Create" Issue 1 Answer
Convert Sprite Image to Texture 6 Answers
Sprite Based Score Rendering 1 Answer