Unity3D UGUI生成并播放序列帧动画prefab
# Editor脚本自动生成Prefab
SpriteAnimationInspector.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace LuaFramework
{
[CustomEditor(typeof(UGUISpriteAnimation))]
public class SpriteAnimationInspector : Editor
{
private static string PREFAB_PATH = "Assets/Res/ViewEffects";
private static string SPRITES_PATH = PREFAB_PATH+"/sprites";
[MenuItem("Assets/Create/Sprite To Prefab _%&_u")]
static void CreatToPrefab()
{
UnityEngine.Object[] selectedGameObjects = Selection.GetFiltered<UnityEngine.Object>(SelectionMode.Assets);
if (selectedGameObjects.Length > 0)
{
CreatePrefab(selectedGameObjects[0].name);
}
}
[MenuItem("Assets/Create/Sprite To Prefab _%&_u", true)]
static bool CheckPrefabPath()
{
UnityEngine.Object[] selectedGameObjects = Selection.GetFiltered<UnityEngine.Object>(SelectionMode.Assets);
if (selectedGameObjects.Length > 0)
{
string name = selectedGameObjects[0].name;
string[] directoryEntries = System.IO.Directory.GetDirectories(SPRITES_PATH);
for (int i = 0; i < directoryEntries.Length; i++)
{
string path = directoryEntries[i];
path = path.Replace('\\','/');
if (path.EndsWith(name))
{
return true;
}
}
}
return false;
}
static private void CreatePrefab(string prefabname)
{
string createPath = PREFAB_PATH + "/" + prefabname + ".prefab";
string searchPath = SPRITES_PATH + "/" + prefabname;
Debug.Log("CreatPrefab : " + createPath);
if (AssetDatabase.LoadAssetAtPath<GameObject>(createPath) == null)
{
GameObject prefab = new GameObject();
prefab.name = prefabname;
UGUISpriteAnimation comp = prefab.GetComponent<UGUISpriteAnimation>();
if (comp == null)
{
comp = prefab.AddComponent<UGUISpriteAnimation>();
}
comp.aniname = prefab.name;
string[] names = GetSprites(searchPath);
//Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(selectPath).OfType<Sprite>().ToArray();
for (int i = 0; i < names.Length; i++)
{
string path = searchPath + "/" + names[i];
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
if (i == 0)
{
prefab.GetComponent<Image>().sprite = sprite;
prefab.GetComponent<Image>().SetNativeSize();
}
comp.SpriteFrames.Add(sprite);
}
PrefabUtility.CreatePrefab(PREFAB_PATH + "/" + prefab.name + ".prefab", prefab);
DestroyImmediate(prefab);
Refresh();
}
else
{
EditorUtility.DisplayDialog("错误!", "Prefab名字重复,请重命名!", "OK");
}
}
void SetSprites()
{
if (spriteAnimation != null)
{
string searchPath = SPRITES_PATH + "/" + spriteAnimation.aniname;
string[] names = GetSprites(searchPath);
for (int i = 0; i < names.Length; i++)
{
string path = searchPath + "/" + names[i];
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
if (i == 0)
{
spriteAnimation.GetComponent<Image>().sprite = sprite;
spriteAnimation.GetComponent<Image>().SetNativeSize();
}
spriteAnimation.SpriteFrames.Add(sprite);
}
}
Refresh();
}
void ClearSprites()
{
if (spriteAnimation != null)
{
spriteAnimation.SpriteFrames.Clear();
}
Refresh();
}
static string AssetPath()
{
string path = Application.dataPath.Replace("/Assets", "");
return path;
}
static string[] GetSprites(string searchPath)
{
List<string> names = new List<string>();
if (Directory.Exists(searchPath))
{
DirectoryInfo dirs = new DirectoryInfo(searchPath);
FileInfo[] files = dirs.GetFiles();
for (int i = 0; i < files.Length; i++)
{
string filename = files[i].Name.ToLower();
if (filename.EndsWith(".jpg") || filename.EndsWith(".png"))
{
names.Add(files[i].Name);
}
}
names.Sort();
}
return names.ToArray();
}
static private void Refresh()
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorApplication.SaveScene();
}
private UGUISpriteAnimation spriteAnimation;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
spriteAnimation = (UGUISpriteAnimation) target;
GUILayout.Space(10);
GUILayout.BeginHorizontal(GUI.skin.box);
if (GUILayout.Button("Set Sprites"))
{
SetSprites();
}
if (GUILayout.Button("Clear Sprites"))
{
ClearSprites();
}
GUILayout.EndHorizontal();
}
}
}
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 序列帧播放控制脚本
UGUISpriteAnimation.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using System;
namespace LuaFramework
{
[RequireComponent(typeof(Image))]
public class UGUISpriteAnimation : MonoBehaviour
{
[Serializable]
public class CompleteEvent : UnityEvent { }
[FormerlySerializedAs("onComplete")]
[SerializeField]
private CompleteEvent m_onComplete = new CompleteEvent();
private Image ImageSource;
private int mCurFrame = 0;
private float mDelta = 0;
public string aniname = "";
public float FPS = 25;
public List<Sprite> SpriteFrames = new List<Sprite>();
public bool IsPlaying = false;
public bool Foward = true;
public bool AutoPlay = true;
public bool Loop = true;
public bool AutoDestory = false;
public CompleteEvent onComplete
{
get
{
return m_onComplete;
}
set
{
m_onComplete = value;
}
}
public int FrameCount
{
get
{
return SpriteFrames.Count;
}
}
void Awake()
{
ImageSource = GetComponent<Image>();
}
void Start()
{
if (AutoPlay)
{
Play();
}
else
{
IsPlaying = false;
}
}
private void SetSprite(int idx)
{
ImageSource.sprite = SpriteFrames[idx];
ImageSource.SetNativeSize();
}
public void Play()
{
IsPlaying = true;
Foward = true;
}
public void PlayReverse()
{
IsPlaying = true;
Foward = false;
}
void Update()
{
if (!IsPlaying || 0 == FrameCount)
{
return;
}
mDelta += Time.deltaTime;
if (mDelta > 1 / FPS)
{
mDelta = 0;
if (Foward)
{
mCurFrame++;
}
else
{
mCurFrame--;
}
if (mCurFrame >= FrameCount)
{
if (Loop)
{
mCurFrame = 0;
m_onComplete.Invoke();
}
else
{
IsPlaying = false;
m_onComplete.Invoke();
if (AutoDestory)
{
Destroy(gameObject);
}
return;
}
}
else if (mCurFrame < 0)
{
if (Loop)
{
mCurFrame = FrameCount - 1;
}
else
{
IsPlaying = false;
return;
}
}
SetSprite(mCurFrame);
}
}
public void Pause()
{
IsPlaying = false;
}
public void Resume()
{
if (!IsPlaying)
{
IsPlaying = true;
}
}
public void Stop()
{
mCurFrame = 0;
SetSprite(mCurFrame);
IsPlaying = false;
}
public void Rewind()
{
mCurFrame = 0;
SetSprite(mCurFrame);
Play();
}
}
}
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
上次更新: 2023/10/16, 17:53:06 访问次数: 0