using XGame.Framework.Asset;
using XGame.Framework.Asset.Addressable.Data;
using UnityEditor;
using UnityEngine;
namespace XGame.Editor.Asset.Addressable
{
///
/// 文件
///
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;
///
/// 刷新名字的监听事件
///
private event System.Func onApply;
internal event System.Func OnApply
{
add => onApply += value;
remove => onApply -= value;
}
///
/// 移除资源的监听事件
///
private event System.Action onRemove;
public event System.Action 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();
}
}
}