- Home /
Loading sprite by script shows weird icon
Hi all,
I have some code the loads an image from a folder. In this case it's an apple.
I load the file like this:
private Texture2D LoadTexture(string filePath)
{
if (File.Exists(filePath)) {
var fileData = File.ReadAllBytes(filePath);
var texture = new Texture2D(64, 64, TextureFormat.ARGB32, false);
texture.LoadImage(fileData);
texture.name = Path.GetFileNameWithoutExtension(filePath);
return texture;
}
return null;
}
I want this apple to show in the Property Inspector (I've made a custom inspector drawer). So, the texture is converted to a sprite and then shown in the Property Inspector.
var spriteTexture = LoadTexture(_loadedFiles[i]);
var newSprite = Sprite.Create(spriteTexture, new Rect(0, 0, spriteTexture.width, spriteTexture.height),new Vector2(0,0), 100.0f);
newSprite.name = Path.GetFileNameWithoutExtension(_loadedFiles[i]);
newSprite.texture.Apply();
proxy.selectedSprite = newSprite;
Then I want I show it in the Property Inspector like this:
proxy.selectedSprite = (Sprite) EditorGUILayout.ObjectField("Item " + i, proxy.selectedSprite, typeof(Sprite), false);
This works however it looks like this in Unity. Now when I switch selecting a gameobject in the hierarchy and then select back the gameobject with the sprite property, the correct image shows up, but it's still not good, because when I click it, it won't lit up in the project browser.
But, when I add the sprite from the folder (by dragging) to the property it all works fine. What am I missing here, because I'm wondering for a few hours now ;)
p.s. Using Unity 2019.4.2f1
Your answer
Follow this Question
Related Questions
Customizing Inspector variables. 2 Answers
Property Drawer To Rename Variable and Make Serializable 0 Answers
EditorGUILayout.Foldout not working properly - results in argument exceptions 1 Answer
Reset a SerializedProperty to it's default value. 2 Answers
Serialize Custom Abstract Class (No-MonoBehaviour / No-ScriptableObject) 1 Answer