using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using AtlasUtility; using UnityEditor; using UnityEngine; public class SpriteUtilityWindow : EditorWindow { #region Config protected SpriteUtility Instance; protected SerializedProperty Path; protected SerializedProperty Name; protected SerializedProperty Sprite; protected SerializedObject SerializedObject; #endregion [MenuItem("DashGame/SpriteUtility")] protected static void ShowWindow() { Type inspectorType = Type.GetType("UnityEditor.InspectorWindow,UnityEditor.dll"); SpriteUtilityWindow window = GetWindow(inspectorType); window.titleContent = new GUIContent("SpriteUtility"); window.Show(); } private void OnEnable() { Instance = InstanceManager.SearchInstance(); SerializedObject = new SerializedObject(Instance); Path = SerializedObject.FindProperty("Path"); Name = SerializedObject.FindProperty("Name"); Sprite = SerializedObject.FindProperty("Sprite"); } private void OnGUI() { SerializedObject.Update(); EditorGUILayout.PropertyField(Sprite, new GUIContent("Sprite")); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(Path, new GUIContent("Path")); if (GUILayout.Button("UseSelectedPath", GUILayout.MaxWidth(120))) { AtlasUtilityWindow.GetSelectedPath(ref Instance.Path); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(Name, new GUIContent("Name")); if (GUILayout.Button("UseSelectedName", GUILayout.MaxWidth(120))) { AtlasUtilityWindow.GetSelectedName(ref Instance.Name); } EditorGUILayout.EndHorizontal(); if (GUILayout.Button("Generate")) { Generate(); } SerializedObject.ApplyModifiedProperties(); } private void Generate() { if (Instance.Sprite == null) { throw new Exception("Sprite未赋值"); } Mesh mesh = SpriteUtility.CreateMesh(Instance.Sprite); AssetDatabase.CreateAsset(mesh, GetFullPath()); } private string GetFullPath() { string directory = Instance.Path.TrimEnd('/', '\\') + "/"; if (directory.Length < 6 || directory.Substring(0, 6).ToLower() != "assets") { throw new Exception("Path必须位置Assets目录内"); } if (!Directory.Exists(directory)) { throw new Exception("文件夹不存在"); } if (string.IsNullOrEmpty(Instance.Name) || Instance.Name.Any(System.IO.Path.GetInvalidFileNameChars().Contains)) { throw new Exception("无效的名字"); } string nameWithFormat = Instance.Name + ".asset"; return directory + nameWithFormat; } }