|
3 | 3 | using UnityEditor; |
4 | 4 | using System.IO; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using EmojiUI; |
6 | 7 |
|
7 | 8 | public static class CreateSpriteAsset |
8 | 9 | { |
9 | 10 | [MenuItem("Assets/Create/Sprite Asset",false,10)] |
10 | | - static void main() |
| 11 | + static void CreateSpriteAssets() |
11 | 12 | { |
12 | | - Object target = Selection.activeObject; |
13 | | - if (target == null || target.GetType() != typeof(Texture2D)) |
14 | | - return; |
| 13 | + string[] guids = Selection.assetGUIDs; |
| 14 | + for(int i =0; i < guids.Length;++i) |
| 15 | + { |
| 16 | + string guid = guids[i]; |
| 17 | + string filepath = AssetDatabase.GUIDToAssetPath(guid); |
| 18 | + GenerateSpriteInfo(filepath); |
| 19 | + } |
15 | 20 |
|
16 | | - Texture2D sourceTex = target as Texture2D; |
17 | | - //整体路径 |
18 | | - string filePathWithName = AssetDatabase.GetAssetPath(sourceTex); |
19 | | - //带后缀的文件名 |
20 | | - string fileNameWithExtension = Path.GetFileName(filePathWithName); |
21 | | - //不带后缀的文件名 |
22 | | - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePathWithName); |
23 | | - //不带文件名的路径 |
24 | | - string filePath = filePathWithName.Replace(fileNameWithExtension, ""); |
| 21 | + AssetDatabase.Refresh(); |
| 22 | + } |
25 | 23 |
|
26 | | - SpriteAsset spriteAsset = AssetDatabase.LoadAssetAtPath(filePath + fileNameWithoutExtension + ".asset", typeof(SpriteAsset)) as SpriteAsset; |
27 | | - bool isNewAsset = spriteAsset == null ? true : false; |
28 | | - if (isNewAsset) |
| 24 | + static void GenerateSpriteInfo(string unitypath) |
| 25 | + { |
| 26 | + Texture2D sourceTex = AssetDatabase.LoadAssetAtPath<Texture2D>(unitypath); |
| 27 | + if(sourceTex != null) |
29 | 28 | { |
| 29 | + string Extensionname = Path.GetExtension(unitypath); |
| 30 | + string filePath = unitypath.Replace(Extensionname, ".asset"); |
| 31 | + |
| 32 | + SpriteAsset spriteAsset = AssetDatabase.LoadAssetAtPath(filePath, typeof(SpriteAsset)) as SpriteAsset; |
| 33 | + int tag = 0; |
| 34 | + if (spriteAsset != null) |
| 35 | + { |
| 36 | + tag = spriteAsset.ID; |
| 37 | + } |
| 38 | + |
| 39 | + //replace |
30 | 40 | spriteAsset = ScriptableObject.CreateInstance<SpriteAsset>(); |
31 | | - spriteAsset.TexSource = sourceTex; |
32 | | - spriteAsset.ListSpriteGroup = GetAssetSpriteInfor(sourceTex); |
33 | | - AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset"); |
| 41 | + spriteAsset.ID = tag; |
| 42 | + spriteAsset.AssetName = sourceTex.ToString(); |
| 43 | + spriteAsset.texSource = sourceTex; |
| 44 | + spriteAsset.listSpriteGroup = GetAssetSpriteInfor(sourceTex); |
| 45 | + AssetDatabase.CreateAsset(spriteAsset, filePath); |
34 | 46 | } |
| 47 | + |
35 | 48 | } |
36 | 49 |
|
37 | | - public static List<SpriteInforGroup> GetAssetSpriteInfor(Texture2D tex) |
| 50 | + static List<SpriteInfoGroup> GetAssetSpriteInfor(Texture2D tex) |
38 | 51 | { |
39 | | - List<SpriteInforGroup> _listGroup = new List<SpriteInforGroup>(); |
40 | | - string filePath = UnityEditor.AssetDatabase.GetAssetPath(tex); |
| 52 | + string filePath = AssetDatabase.GetAssetPath(tex); |
| 53 | + Object[] objects = AssetDatabase.LoadAllAssetsAtPath(filePath); |
41 | 54 |
|
42 | | - Object[] objects = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(filePath); |
| 55 | + List<SpriteInfoGroup> _listGroup = new List<SpriteInfoGroup>(); |
| 56 | + List<SpriteInfo> allSprites = new List<SpriteInfo>(); |
43 | 57 |
|
44 | | - List<SpriteInfor> _tempSprite = new List<SpriteInfor>(); |
45 | | - |
46 | | - Vector2 _texSize = new Vector2(tex.width, tex.height); |
| 58 | + Vector2 texelSize = new Vector2(tex.width, tex.height); |
47 | 59 | for (int i = 0; i < objects.Length; i++) |
48 | 60 | { |
49 | | - if (objects[i].GetType() != typeof(Sprite)) |
50 | | - continue; |
51 | | - SpriteInfor temp = new SpriteInfor(); |
52 | | - Sprite sprite = objects[i] as Sprite; |
53 | | - temp.Id = i; |
54 | | - temp.Name = sprite.name; |
55 | | - temp.Pivot = sprite.pivot; |
56 | | - temp.Rect = sprite.rect; |
57 | | - temp.Sprite = sprite; |
58 | | - temp.Tag = sprite.name; |
59 | | - temp.Uv = GetSpriteUV(_texSize, sprite.rect); |
60 | | - _tempSprite.Add(temp); |
61 | | - } |
| 61 | + Sprite sprite = objects[i] as Sprite; |
62 | 62 |
|
63 | | - for (int i = 0; i < _tempSprite.Count; i++) |
64 | | - { |
65 | | - SpriteInforGroup _tempGroup = new SpriteInforGroup(); |
66 | | - _tempGroup.Tag = _tempSprite[i].Tag; |
67 | | - //_tempGroup.Size = 24.0f; |
68 | | - //_tempGroup.Width = 1.0f; |
69 | | - _tempGroup.ListSpriteInfor = new List<SpriteInfor>(); |
70 | | - _tempGroup.ListSpriteInfor.Add(_tempSprite[i]); |
71 | | - for (int j = i+1; j < _tempSprite.Count; j++) |
| 63 | + if (sprite != null) |
72 | 64 | { |
73 | | - if ( _tempGroup.Tag == _tempSprite[j].Tag) |
| 65 | + SpriteInfo seeked = allSprites.Find(p => p.tag == sprite.name); |
| 66 | + if(seeked == null) |
74 | 67 | { |
75 | | - _tempGroup.ListSpriteInfor.Add(_tempSprite[j]); |
76 | | - _tempSprite.RemoveAt(j); |
77 | | - j--; |
| 68 | + SpriteInfo temp = new SpriteInfo(); |
| 69 | + temp.sprite = sprite; |
| 70 | + temp.tag = sprite.name; |
| 71 | + temp.uv = GetSpriteUV(texelSize, sprite.rect); |
| 72 | + allSprites.Add(temp); |
78 | 73 | } |
79 | 74 | } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + for (int i = 0; i < allSprites.Count; i++) |
| 79 | + { |
| 80 | + SpriteInfo info = allSprites[i]; |
| 81 | + |
| 82 | + SpriteInfoGroup _tempGroup = new SpriteInfoGroup(); |
| 83 | + _tempGroup.tag = info.tag; |
| 84 | + _tempGroup.size = Mathf.Max(info.sprite.rect.size.x, info.sprite.rect.size.y); |
| 85 | + _tempGroup.width =1; |
| 86 | + _tempGroup.spritegroups.Add(info); |
| 87 | + |
80 | 88 | _listGroup.Add(_tempGroup); |
81 | | - _tempSprite.RemoveAt(i); |
82 | | - i--; |
83 | 89 | } |
84 | 90 |
|
85 | 91 | return _listGroup; |
|
0 commit comments