Firstborn/Assets/ModTool/ModTool.Editor.Exporting/Asset.cs
Schaken-Mods 7502018d20 Adding Mod Support
There is an asset in the store I grabbed. the coding is WAY above my head, I got about half of it and integrated and adapted what I can to it. im going as far as I can with it and ill come back in a few month when I understand t better.
2023-05-13 22:01:48 -05:00

117 lines
3.0 KiB
C#

using System;
using System.IO;
using UnityEngine;
using UnityEditor;
namespace ModTool.Editor.Exporting
{
[Serializable]
public class Asset
{
public static readonly string backupDirectory = "Backup";
public string name
{
get
{
return Path.GetFileNameWithoutExtension(assetPath);
}
set
{
string result = AssetDatabase.RenameAsset(assetPath, value);
if (string.IsNullOrEmpty(result))
_assetPath = Path.Combine(Path.GetDirectoryName(assetPath), value + Path.GetExtension(assetPath));
}
}
public string assetPath
{
get
{
return _assetPath;
}
set
{
string result = AssetDatabase.MoveAsset(assetPath, value);
if (string.IsNullOrEmpty(result))
_assetPath = value;
}
}
public string originalPath
{
get
{
return _originalPath;
}
}
public string backupPath
{
get
{
return _backupPath;
}
}
[SerializeField]
private string _assetPath;
[SerializeField]
private string _originalPath;
[SerializeField]
private string _backupPath;
public Asset(string assetPath)
{
_assetPath = assetPath;
_originalPath = assetPath;
}
public void Backup()
{
_backupPath = originalPath.Replace("Assets", Asset.backupDirectory);
string backupDirectory = Path.GetDirectoryName(backupPath);
if (!Directory.Exists(backupDirectory))
Directory.CreateDirectory(backupDirectory);
File.Copy(assetPath, backupPath);
File.Copy(assetPath + ".meta", backupPath + ".meta");
_originalPath = assetPath;
}
public void Restore()
{
if (!File.Exists(backupPath))
return;
AssetDatabase.DeleteAsset(assetPath);
_assetPath = originalPath;
File.Copy(backupPath, assetPath, true);
File.Copy(backupPath + ".meta", assetPath + ".meta", true);
}
public void SetAssetBundle(string assetBundleName, string assetBundleVariant = "")
{
AssetImporter importer = AssetImporter.GetAtPath(assetPath);
importer.assetBundleName = assetBundleName;
if (!string.IsNullOrEmpty(assetBundleName))
importer.assetBundleVariant = assetBundleVariant;
}
public void Delete()
{
//Note: AssetDatabase.DeleteAsset updates the asset database, which can trigger an unwanted compilation/script reload
File.Delete(assetPath);
File.Delete(assetPath + ".meta");
}
}
}