1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| namespace HaruoYaguchi.Editor { [Serializable] struct TexturesData { public byte[] data; public TexturesData(byte[] data) { this.data = data; } public readonly string ToJson() { return JsonUtility.ToJson(this); } }
public sealed class TextureToJson : EditorWindow { public List<Texture2D> textures2D = new List<Texture2D>(); SerializedProperty texturesProperty; SerializedObject texturesObject; StringBuilder pathBuilder = new StringBuilder(); string _jsonStoragePath;
void OnEnable() { texturesObject = new SerializedObject(this); texturesProperty = texturesObject.FindProperty("textures2D"); }
[MenuItem("HaruoYaguchi/TextureToJson", false, 2)] public static void Internal_TextureToJson() { if (EditorApplication.isCompiling || EditorApplication.isPlaying) { EditorUtility.DisplayDialog("警告", "不允许在游戏运行时或代码编译时进行转换", "确定"); return; } TextureToJson textureToJson = GetWindow<TextureToJson>(); textureToJson.Show(); } private void OnGUI() { GUILayout.Label("将需要转换的纹理放入下面的列表中"); EditorGUILayout.PropertyField(texturesProperty);
GUILayout.Label("转换后Json文件存储的位置(不填写则在列表中第一个纹理的父级目录下创建一个新的文件夹, 文件夹名为'TextureToJsonFolder'), 以该文件夹为存储位置"); GUILayout.Label("路径填写标准: './Assets/.../' 或 'C:/.../' (相对路径或绝对路径, 需要保证该路径存在)"); _jsonStoragePath = EditorGUILayout.TextField("存储路径: ", _jsonStoragePath);
if (GUILayout.Button("将纹理转换为Json文件")) { try { if (EditorGUI.EndChangeCheck()) { texturesObject.ApplyModifiedProperties(); }
if (_jsonStoragePath == null || _jsonStoragePath == string.Empty) { _jsonStoragePath = AssetDatabase.GetAssetPath(textures2D[0]); _jsonStoragePath = Directory.GetParent(_jsonStoragePath).Parent.FullName; pathBuilder.Append(_jsonStoragePath + "/TextureToJsonFolder/"); Directory.CreateDirectory(pathBuilder.ToString()); } else { pathBuilder = new StringBuilder(_jsonStoragePath); }
for (int i = 0; i < textures2D.Count; ++i) {
byte[] data = DeCompress(textures2D[i]).EncodeToPNG();
pathBuilder.Append(textures2D[i].name + ".json");
TexturesData texturesData = new TexturesData(data); data = Encoding.UTF8.GetBytes(texturesData.ToJson()); File.WriteAllBytes(pathBuilder.ToString(), data);
pathBuilder.Remove(pathBuilder.Length - textures2D[i].name.Length - 5, textures2D[i].name.Length + 5); }
_jsonStoragePath = null; pathBuilder.Clear(); Debug.Log("纹理转换Json成功!!!!!!"); } catch (System.Exception e) { _jsonStoragePath = null; pathBuilder.Clear(); Debug.LogError(e); } } }
public Texture2D DeCompress(Texture2D source) { RenderTexture renderTex = RenderTexture.GetTemporary( source.width, source.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex); RenderTexture previous = RenderTexture.active; RenderTexture.active = renderTex; Texture2D readableTexture = new Texture2D(source.width, source.height); readableTexture.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0); readableTexture.Apply(); RenderTexture.active = previous; RenderTexture.ReleaseTemporary(renderTex); return readableTexture; }
} }
|