- Home /
Show only readable textures when selecting a texture with objectified
Hi everyone, I'm currently trying to do an EditorWindow where you can do stuff with textures(not important) the point is that the textures there have to be readable. I know i can make the textures readable with TextureImporter etc. but i don't want to change the textures otherwise i only want to show textures that are already readable when selecting it with EditorGUILayout.objectfield.
If it's not clear what i mean, I mean where it says select Textures2D in the following image. Is it possible to only show readable textures? and if so, could you guys please lead me in the right direction on how to achieve this.
Edit This is my Set Default UI EditorWindow:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
public class SetDefaultUI : EditorWindow
{
[MenuItem("Manager/UI/Set Default")]
public static void Init()
{
SetDefaultUI myWindow = (SetDefaultUI)EditorWindow.GetWindow (typeof(SetDefaultUI));
myWindow.Show ();
}
Texture2D m_frontSideImage;
Texture2D m_backSideImage;
Texture2D m_backImage;
Texture2D m_playImage;
Vector2 scrollPos;
bool showLevelButton = false;
void OnGUI()
{
EditorGUILayout.LabelField ("Set the default UI here", EditorStyles.boldLabel);
scrollPos = EditorGUILayout.BeginScrollView (scrollPos);
showLevelButton = EditorGUILayout.Foldout (showLevelButton, "Level Button:");
if (showLevelButton)
{
m_frontSideImage = EditorGUILayout.ObjectField ("Front side", m_frontSideImage, typeof(Texture2D), false) as Texture2D;
m_backSideImage = EditorGUILayout.ObjectField ("Back side", m_backSideImage, typeof(Texture2D), false) as Texture2D;
m_backImage = EditorGUILayout.ObjectField ("Back image", m_backImage, typeof(Texture2D), false) as Texture2D;
m_playImage = EditorGUILayout.ObjectField ("Play image", m_playImage, typeof(Texture2D), false) as Texture2D;
}
EditorGUILayout.BeginHorizontal ();
if (GUILayout.Button ("Get Level Button's UI"))
GetLevelButtonUI ();
if (GUILayout.Button ("Set Level Button's UI"))
SetLevelButtonUI ();
EditorGUILayout.EndHorizontal ();
EditorGUILayout.EndScrollView ();
}
void GetLevelButtonUI()
{
List<Texture2D> textures = SaveLoad.LoadDefaultUI ();
if (textures [0] != null)
m_frontSideImage = textures [0];
else
m_frontSideImage = null;
if (textures [1] != null)
m_backSideImage = textures [1];
else
m_backSideImage = null;
if (textures [2] != null)
m_backImage = textures [2];
else
m_backImage = null;
if (textures [3] != null)
m_playImage = textures [3];
else
m_playImage = null;
}
void SetLevelButtonUI()
{
if (DefaultLevelUI.current != null)
{
if (DefaultLevelUI.current.FrontSideImage != m_frontSideImage ||
DefaultLevelUI.current.BackSideImage != m_backSideImage ||
DefaultLevelUI.current.BackImage != m_backImage ||
DefaultLevelUI.current.PlayImage != m_playImage)
{
SaveLevelButtonUI ();
}
} else
{
DefaultLevelUI.current = new DefaultLevelUI ();
SaveLevelButtonUI ();
}
}
void SaveLevelButtonUI()
{
if (EditorUtility.DisplayDialog ("Overwrite Level Button default UI", "Some or all of the sprites provided don't match the current default Level Button UI, are you sure you want to overwrite them?", "Ok", "Cancel"))
{
DefaultLevelUI.current.FrontSideImage = m_frontSideImage;
DefaultLevelUI.current.BackSideImage = m_backSideImage;
DefaultLevelUI.current.BackImage = m_backImage;
DefaultLevelUI.current.PlayImage = m_playImage;
SaveLoad.SaveDefaultUI (GetTexturesInList());
}
}
List<Texture2D> GetTexturesInList()
{
List<Texture2D> texList = new List<Texture2D> ();
texList.Add (DefaultLevelUI.current.FrontSideImage);
texList.Add (DefaultLevelUI.current.BackSideImage);
texList.Add (DefaultLevelUI.current.BackImage);
texList.Add (DefaultLevelUI.current.PlayImage);
return texList;
}
}
As you can see on line 29-32 i have the object fields, i think i should do there something but honestly I have no idea how to do it.
Thanks in advance -Jorge
Answer by M-Hanssen · Apr 18, 2016 at 01:28 PM
To check if a texture (or any file is readable, you can use something like this:
public Texture Texture;
protected void Start()
{
Debug.Log("Texture " + Texture.name + " readable = " + CheckTextureReadable(Texture));
}
protected bool CheckTextureReadable(Texture texture)
{
bool result = false;
string texturePath = AssetDatabase.GetAssetPath(texture);
if (File.Exists(texturePath))
{
result = File.OpenRead(texturePath).CanWrite;
}
return result;
}
I actually know how to check if a texture is readable, but how can i modify the selection window, in better words how can i apply the CheckTextureReadable function to each Texture in the Select Texture2D window
Since I'm not going to write you a full editorWindow script, you'll have to show me some code you already made ;-)
I edited the question providing my Editorwindow script :) hope it helps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Combine Array of Sprites to Form One Sprite 0 Answers
Problem when loading texture from folder 0 Answers
Checking for null against Texture2D 0 Answers