123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using XGame.Framework.Asset;
- using XGame.Framework.Asset.Addressable.Data;
- using UnityEditor;
- using UnityEngine;
- namespace XGame.Editor.Asset.Addressable
- {
- /// <summary>
- /// 文件
- /// </summary>
- public class AddressableAssetFile : AddressableAssetEntry
- {
- private readonly long addressableId;
- private string addressableName;
- private readonly string assetGUID;
- public string AssetGUID => assetGUID;
- //private AddressableAssetType assetType;
- private string relativePath;
- private Object asset;
- /// <summary>
- /// 刷新名字的监听事件
- /// </summary>
- private event System.Func<ApplyEvnetContext, bool> onApply;
- internal event System.Func<ApplyEvnetContext, bool> OnApply
- {
- add => onApply += value;
- remove => onApply -= value;
- }
- /// <summary>
- /// 移除资源的监听事件
- /// </summary>
- private event System.Action<string> onRemove;
- public event System.Action<string> OnRemove
- {
- add => onRemove += value;
- remove => onRemove -= value;
- }
- internal FileSign Sign { set; get; }
- public override EntryType EnType => EntryType.File;
- public AddressableAssetFile(string path, AssetInfo assetInfo) : base(path)
- {
- addressableId = assetInfo.addressableId;
- addressableName = assetInfo.addressableName;
- assetGUID = assetInfo.assetGUID;
- //assetType = assetInfo.assetType;
- relativePath = assetInfo.relativePath;
- //var path = AssetDatabase.GUIDToAssetPath(assetGUID);
- if (string.IsNullOrEmpty(path))
- {
- Debug.LogError($"AddressableAssetInfo Error: {assetInfo}");
- return;
- }
- asset = AssetDatabase.LoadMainAssetAtPath(path);
- }
-
-
- protected override void OnSetParent()
- {
-
- }
- protected override void OnGUI()
- {
- //开始垂直排序
- EditorGUILayout.BeginVertical("Box");
- IsFoldout = EditorGUILayout.Foldout(IsFoldout, ShowName);
- if (IsFoldout)
- {
- //展示区域,禁止操作
- EditorGUI.BeginDisabledGroup(true);
- var assetContent = new GUIContent("Asset", "Asset Object");
- EditorGUILayout.ObjectField(assetContent, asset, typeof(Object), false);
- EditorGUILayout.LongField("Addressable ID", addressableId);
- //EditorGUILayout.EnumFlagsField("Asset Type", assetType);
- relativePath = EditorGUILayout.TextField("Relative Path", relativePath);
- EditorGUI.EndDisabledGroup();
- //可编辑区域
- var nameContent = new GUIContent("Addressable Name", "资源名字,只允许使用小写字母, 数字和下划线的组合");
- addressableName = EditorGUILayout.TextField(nameContent, addressableName);
- //按钮
- DrawButtons();
- }
- //结束垂直排序
- EditorGUILayout.EndVertical();
- }
- protected override void OnClear()
- {
- asset = null;
- onApply = null;
- onRemove = null;
- }
- private void DrawButtons()
- {
- //按钮横板排序
- EditorGUILayout.BeginHorizontal();
- if (Sign == FileSign.Invalid)
- {
- // 无效资源
- bool isRemove = GUILayout.Button("Remove", GUILayout.Width(60));
- if (isRemove)
- {
- onRemove?.Invoke(this.assetGUID);
- }
- }
- else
- {
- string fileName = string.Empty;
- // 有效资源
- bool isApply = GUILayout.Button("Apply", GUILayout.Width(60));
- if (isApply)
- {
- fileName = addressableName;
- }
- if (asset != null)
- {
- bool useFileName = GUILayout.Button("Use File Name", GUILayout.Width(100));
- if (useFileName)
- {
- fileName = AddressableHelper.GetFileNameIncludeI18n(AssetDatabase.GetAssetPath(asset));
- }
- }
- if (!string.IsNullOrEmpty(fileName))
- {
- if (onApply?.Invoke(new ApplyEvnetContext()
- {
- assetGUID = assetGUID,
- addressableName = fileName,
- fileSign = Sign,
- assetEntry = this
- }) ?? false)
- {
- addressableName = fileName;
- }
- }
- }
- //结束按钮的横板排序
- EditorGUILayout.EndHorizontal();
- }
- }
- }
|