feat(TextureAtlas): Automatically regenerate
This commit is contained in:
parent
c361ad2945
commit
d8a57c3817
3 changed files with 189 additions and 106 deletions
67
TextureAtlasGenerator/TextureAtlasAutoRegen.cs
Normal file
67
TextureAtlasGenerator/TextureAtlasAutoRegen.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
|
||||
namespace gay.lilyy.TextureAtlasGenerator
|
||||
{
|
||||
public class TextureAtlasAutoRegen : AssetPostprocessor
|
||||
{
|
||||
private const string MenuPath = "LillithRosePup/Import Regens/Texture Atlas";
|
||||
private const string PrefKey = "TextureAtlasGenerator.AutoRegen";
|
||||
|
||||
[MenuItem(MenuPath)]
|
||||
private static void Toggle()
|
||||
{
|
||||
bool enabled = !EditorPrefs.GetBool(PrefKey, true);
|
||||
EditorPrefs.SetBool(PrefKey, enabled);
|
||||
Menu.SetChecked(MenuPath, enabled);
|
||||
}
|
||||
|
||||
[MenuItem(MenuPath, true)]
|
||||
private static bool ToggleValidate()
|
||||
{
|
||||
Menu.SetChecked(MenuPath, EditorPrefs.GetBool(PrefKey, true));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsEnabled()
|
||||
{
|
||||
return EditorPrefs.GetBool(PrefKey, true);
|
||||
}
|
||||
static void OnPostprocessAllAssets(
|
||||
string[] importedAssets,
|
||||
string[] deletedAssets,
|
||||
string[] movedAssets,
|
||||
string[] movedFromAssetPaths)
|
||||
{
|
||||
if (!IsEnabled())
|
||||
return;
|
||||
|
||||
if (importedAssets.Length == 0)
|
||||
return;
|
||||
|
||||
var atlases = AssetDatabase
|
||||
.FindAssets("t:TextureAtlasSettings")
|
||||
.Select(guid =>
|
||||
AssetDatabase.LoadAssetAtPath<TextureAtlasSettings>(
|
||||
AssetDatabase.GUIDToAssetPath(guid)))
|
||||
.Where(s => s && s.textures != null && s.textures.Length > 0)
|
||||
.ToArray();
|
||||
|
||||
if (atlases.Length == 0)
|
||||
return;
|
||||
|
||||
foreach (string path in importedAssets)
|
||||
{
|
||||
var tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
|
||||
if (!tex) continue;
|
||||
|
||||
foreach (var atlas in atlases)
|
||||
{
|
||||
if (atlas.textures.Contains(tex))
|
||||
TextureAtlasEditor.GenerateAtlas(atlas);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
TextureAtlasGenerator/TextureAtlasAutoRegen.cs.meta
Normal file
11
TextureAtlasGenerator/TextureAtlasAutoRegen.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f62a8a2b00eb0fe4598d1b45218ddfad
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -77,6 +77,11 @@ namespace gay.lilyy.TextureAtlasGenerator
|
|||
private void GenerateAtlas()
|
||||
{
|
||||
TextureAtlasSettings settings = (TextureAtlasSettings)target;
|
||||
GenerateAtlas(settings);
|
||||
}
|
||||
|
||||
internal static void GenerateAtlas(TextureAtlasSettings settings)
|
||||
{
|
||||
if (settings.textures == null || settings.textures.Length == 0)
|
||||
{
|
||||
Debug.LogError("No textures assigned!");
|
||||
|
|
@ -108,7 +113,7 @@ namespace gay.lilyy.TextureAtlasGenerator
|
|||
}
|
||||
|
||||
|
||||
private Texture2D ResizeTexture(Texture2D texture, int width, int height)
|
||||
private static Texture2D ResizeTexture(Texture2D texture, int width, int height)
|
||||
{
|
||||
RenderTexture rt = new RenderTexture(width, height, 32);
|
||||
Graphics.Blit(texture, rt);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue