73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using System;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.Framework
|
|
{
|
|
public static class ResourceKit
|
|
{
|
|
public static string SaveSprite(Sprite sprite)
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (!sprite) return "";
|
|
|
|
// 先读Psd的路径
|
|
string psdPath = AssetDatabase.GetAssetPath(sprite);
|
|
var psdResPath = GetResourcePath(psdPath);
|
|
// 去后缀
|
|
// 找到最后一个点的位置
|
|
int lastDotIndex = psdResPath.LastIndexOf('.');
|
|
// 如果没有点,说明没有扩展名
|
|
if (lastDotIndex == -1)
|
|
{
|
|
return psdResPath;
|
|
}
|
|
|
|
// 提取文件名(不包括扩展名)
|
|
var purePath = psdResPath.Substring(0, lastDotIndex);
|
|
|
|
// 添加后缀
|
|
return purePath + "/" + sprite.name;
|
|
#else
|
|
return null;
|
|
#endif
|
|
|
|
}
|
|
|
|
private static string GetResourcePath(string absolutePath)
|
|
{
|
|
const string resourcesFolderName = "Resources/";
|
|
int index = absolutePath.IndexOf(resourcesFolderName, StringComparison.Ordinal);
|
|
|
|
if (index >= 0)
|
|
{
|
|
// 获取 "Resources/" 之后的部分
|
|
return absolutePath.Substring(index + resourcesFolderName.Length);
|
|
}
|
|
else
|
|
{
|
|
return absolutePath;
|
|
}
|
|
}
|
|
|
|
public static Sprite LoadSpriteFromPsd(string path)
|
|
{
|
|
int lastIndex = path.LastIndexOf('/');
|
|
string spriteName = path.Substring(lastIndex + 1);
|
|
string resPath = path.Substring(0, lastIndex);
|
|
|
|
// 加载
|
|
var sprites = Resources.LoadAll<Sprite>(resPath);
|
|
|
|
foreach (var sprite in sprites)
|
|
{
|
|
if (sprite.name == spriteName)
|
|
{
|
|
return sprite;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |