Unity3D UGUI生成并播放序列帧动画prefab 作者: rontian 时间: 2017-12-12 分类: Unity3D ### Editor脚本自动生成Prefab `SpriteAnimationInspector.cs` ```csharp 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(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(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(createPath) == null) { GameObject prefab = new GameObject(); prefab.name = prefabname; UGUISpriteAnimation comp = prefab.GetComponent(); if (comp == null) { comp = prefab.AddComponent(); } comp.aniname = prefab.name; string[] names = GetSprites(searchPath); //Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(selectPath).OfType().ToArray(); for (int i = 0; i < names.Length; i++) { string path = searchPath + "/" + names[i]; Sprite sprite = AssetDatabase.LoadAssetAtPath(path); if (i == 0) { prefab.GetComponent().sprite = sprite; prefab.GetComponent().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(path); if (i == 0) { spriteAnimation.GetComponent().sprite = sprite; spriteAnimation.GetComponent().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 names = new List(); 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(); } } } ``` ### 序列帧播放控制脚本 `UGUISpriteAnimation.cs` ```csharp 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 SpriteFrames = new List(); 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(); } 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(); } } } ``` 标签: none
评论已关闭