Files
AppleHillsProduction/Assets/Editor/PrefabEditorUtility.cs

74 lines
2.7 KiB
C#

using UnityEditor;
using UnityEngine;
using System.IO;
namespace Editor
{
public static class PrefabEditorUtility
{
public static string SanitizeFileName(string fileName)
{
foreach (char c in Path.GetInvalidFileNameChars())
fileName = fileName.Replace(c, '_');
return fileName;
}
public static string SelectFolder(string currentPath, string defaultSubfolder)
{
string selected = EditorUtility.OpenFolderPanel("Select Folder", Path.Combine(Application.dataPath, defaultSubfolder), "");
if (!string.IsNullOrEmpty(selected))
{
if (selected.Replace("\\", "/").Contains(Application.dataPath.Replace("\\", "/")))
{
return "Assets" + selected.Replace("\\", "/").Substring(Application.dataPath.Replace("\\", "/").Length);
}
else
{
EditorUtility.DisplayDialog("Invalid Folder", "Please select a folder inside the Assets directory.", "OK");
}
}
return currentPath;
}
public static T CreateScriptableAsset<T>(string assetName, string folderPath) where T : ScriptableObject
{
if (!AssetDatabase.IsValidFolder(folderPath))
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), folderPath));
AssetDatabase.Refresh();
}
string folder = !string.IsNullOrEmpty(folderPath) ? folderPath : "Assets";
string path = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(folder, assetName + ".asset"));
var asset = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(asset, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Selection.activeObject = asset;
return asset;
}
public static void DrawScriptableObjectEditor(ref UnityEditor.Editor soEditor, ScriptableObject so)
{
if (so == null) return;
if (soEditor == null || soEditor.target != so)
soEditor = UnityEditor.Editor.CreateEditor(so);
if (soEditor != null)
soEditor.OnInspectorGUI();
}
public static void AddOrGetComponent<T>(GameObject go) where T : Component
{
if (go.GetComponent<T>() == null)
go.AddComponent<T>();
}
public static void RemoveComponent<T>(GameObject go) where T : Component
{
var comp = go.GetComponent<T>();
if (comp != null)
Object.DestroyImmediate(comp, true);
}
}
}