Files
Main/Assets/Launcher/ExternalLibs/UIAssetData/UIFormAssetDataScript.cs
2025-01-25 04:38:09 +08:00

308 lines
11 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using System;
using UnityEngine.Playables;
using UnityEngine.Timeline;
namespace Thousandto.Launcher.ExternalLibs
{
/// <summary>
/// 窗体资源数据脚本
/// </summary>
[ExecuteInEditMode]
public class UIFormAssetDataScript : MonoBehaviour
{
#region//记录数据
[SerializeField]
public UIAtlas[] RefAtlasList = null;
[SerializeField]
public string[] RefAtlasNameList = null;
[SerializeField]
public UIFont[] RefFontList = null;
[SerializeField]
public string[] RefFontNameList = null;
[SerializeField]
public AnimationClip[] RefAnimClipArray = null;
[SerializeField]
public RuntimeAnimatorController[] RefAnimControllerArray = null;
[SerializeField]
public PlayableAsset[] RefPlayableAssetArray = null;
[SerializeField]
public List<AtlasInfo> RefAtlasInfoList = null;
[SerializeField]
public List<FontInfo> RefFontInfoList = null;
//这个是用在编辑器状态刷新使用,在正式的打包过程中,不需要使用这个
public bool IsRefresh = false;
#endregion
#region//运行时
//删除关联的一些空壳资源
public void DestoryAssets()
{
if (RefAtlasList != null)
{
for (int i = 0; i < RefAtlasList.Length; i++)
{
//对于这里的图集不做资源删除,是因为这里的图集是空的图集
GameObject.DestroyImmediate(RefAtlasList[i].gameObject, true);
}
RefAtlasList = null;
RefAtlasNameList = null;
}
if (RefFontList != null)
{
for (int i = 0; i < RefFontList.Length; i++)
{
//对于这里的字体不做资源删除,是因为这里的字体是空的字体
GameObject.DestroyImmediate(RefFontList[i].gameObject, true);
}
RefFontList = null;
RefFontNameList = null;
}
if (RefAnimClipArray != null)
{
var cnt = RefAnimClipArray.Length;
for (int i = 0; i < cnt; i++)
{
Resources.UnloadAsset(RefAnimClipArray[i]);
}
}
if (RefAnimControllerArray != null)
{
var cnt = RefAnimClipArray.Length;
for (int i = 0; i < cnt; i++)
{
Resources.UnloadAsset(RefAnimControllerArray[i]);
}
}
if (RefPlayableAssetArray != null)
{
var cnt = RefPlayableAssetArray.Length;
for (int i = 0; i < cnt; i++)
{
Resources.UnloadAsset(RefPlayableAssetArray[i]);
}
}
}
//预加载资源
public void PreLoadAssets(Action<string, bool> preLoadAtlasOrFontFunc)
{
if (preLoadAtlasOrFontFunc != null)
{
//加载所有图集
for (int i = 0; i < RefAtlasList.Length; i++)
{
var atlas = RefAtlasList[i];
atlas.OwnerForm = this.gameObject;
var name = RefAtlasNameList[i];
preLoadAtlasOrFontFunc(name, true);
}
//加载所有的字体
for (int i = 0; i < RefFontList.Length; i++)
{
var font = RefFontList[i];
font.OwnerForm = this.gameObject;
var name = RefFontNameList[i];
preLoadAtlasOrFontFunc(name, false);
}
}
}
#endregion
#region//编辑器状态下使用
#if UNITY_EDITOR
private void Update()
{
if (IsRefresh)
{
ToCollection();
IsRefresh = false;
}
}
#endif
//在打包之前进行采集工作
public void ToCollection()
{
//IsRefresh = true;
{
//收集图集
var _atlasList = new List<UIAtlas>();
var _atlasName = new List<string>();
RefAtlasInfoList = new List<AtlasInfo>();
var comps = GetComponentsInChildren<UISprite>(true);
for (int i = 0; i < comps.Length; i++)
{
if (comps[i].atlas == null) continue;
var atlas = comps[i].atlas;
var idx = _atlasList.IndexOf(atlas);
if (idx < 0)
{
_atlasList.Add(atlas);
var arr = atlas.name.Split(new char[] { '/' });
string name = arr[arr.Length - 1];
_atlasName.Add(name);
if (!IsRefresh)
{
atlas.ClearSerializeFields();
}
if (IsRefresh)
{
RefAtlasInfoList.Add(new AtlasInfo() { Atlas = atlas, Name = name, Sprites = new List<UISprite>() });
idx = RefAtlasInfoList.Count - 1;
}
}
if (IsRefresh && idx >= 0)
{
RefAtlasInfoList[idx].Sprites.Add(comps[i]);
}
}
RefAtlasList = _atlasList.ToArray();
RefAtlasNameList = _atlasName.ToArray();
}
{
//收集字体
var _fontList = new List<UIFont>();
var _fontName = new List<string>();
RefFontInfoList = new List<FontInfo>();
var comps = GetComponentsInChildren<UILabel>(true);
for (int i = 0; i < comps.Length; i++)
{
if (comps[i].bitmapFont == null) continue;
var font = comps[i].bitmapFont;
var idx = _fontList.IndexOf(font);
if (idx < 0)
{
_fontList.Add(font);
var arr = font.name.Split(new char[] { '/' });
string name = arr[arr.Length - 1];
_fontName.Add(name);
if (!IsRefresh)
{
font.ClearSerializeFields();
}
if (IsRefresh) {
RefFontInfoList.Add(new FontInfo() { Font = font, Name = name, Labels = new List<UILabel>() });
idx = RefFontInfoList.Count - 1;
}
}
if (IsRefresh && idx >= 0)
{
RefFontInfoList[idx].Labels.Add(comps[i]);
}
}
RefFontList = _fontList.ToArray();
RefFontNameList = _fontName.ToArray();
}
{
//收集AnimationClip
var _animators = GetComponentsInChildren<Animator>(true);
if (_animators != null)
{
var _animaClipList = new List<AnimationClip>();
var _animatorsControllerList = new List<RuntimeAnimatorController>();
var cnt = _animators.Length;
for (int i = 0; i < cnt; i++)
{
var _animator = _animators[i];
var _animatorController = _animator.runtimeAnimatorController;
if (_animatorController)
{
_animatorsControllerList.Add(_animatorController);
var animationClips = _animatorController.animationClips;
if (animationClips != null)
{
var clipsCnt = animationClips.Length;
for (int j = 0; j < clipsCnt; j++)
{
_animaClipList.Add(animationClips[i]);
}
}
}
}
RefAnimClipArray = _animaClipList.ToArray();
RefAnimControllerArray = _animatorsControllerList.ToArray();
}
}
{
//收集Timeline资源
var _playableDirectors = GetComponentsInChildren<PlayableDirector>(true);
if (_playableDirectors != null)
{
var _animaClipList = new List<AnimationClip>();
var _playableAssetList = new List<PlayableAsset>();
var cnt = _playableDirectors.Length;
for (int i = 0; i < cnt; i++)
{
var _playableAsset = _playableDirectors[i].playableAsset;
foreach (var item in _playableAsset.outputs)
{
var _animationTrack = item.sourceObject as AnimationTrack;
if (_animationTrack)
{
var clips = _animationTrack.GetClips();
foreach (var clipsItem in clips)
{
var _animationClip = clipsItem.animationClip;
if (_animationClip)
{
_animaClipList.Add(_animationClip);
}
}
}
}
}
RefAnimClipArray = _animaClipList.ToArray();
RefPlayableAssetArray = _playableAssetList.ToArray();
}
}
//IsRefresh = false;
}
#endregion
#region//子类数据信息
[Serializable]
public class AtlasInfo
{
[SerializeField]
public UIAtlas Atlas;
[SerializeField]
public string Name;
[SerializeField]
public List<UISprite> Sprites;
}
[Serializable]
public class FontInfo
{
[SerializeField]
public UIFont Font;
[SerializeField]
public string Name;
[SerializeField]
public List<UILabel> Labels;
}
#endregion
}
}