-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
166 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Reflection; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace SpriteDicing | ||
{ | ||
public class TextureSettings | ||
{ | ||
private const string defaultPlatformId = "default"; | ||
private static readonly string[] platformIds = GetPlatformIds(); | ||
private readonly TextureImporterSettings @base = new TextureImporterSettings(); | ||
private readonly Dictionary<string, TextureImporterPlatformSettings> platforms = | ||
new Dictionary<string, TextureImporterPlatformSettings>(); | ||
|
||
public void TryImportExisting (Texture texture) | ||
{ | ||
if (!TryGetTextureImporter(texture, out var importer)) return; | ||
importer.ReadTextureSettings(@base); | ||
foreach (var platformId in platformIds) | ||
platforms[platformId] = importer.GetPlatformTextureSettings(platformId); | ||
platforms[defaultPlatformId] = importer.GetDefaultPlatformTextureSettings(); | ||
} | ||
|
||
public void ApplyExistingOrDefault (TextureImporter importer) | ||
{ | ||
if (platforms.Count == 0) ApplyDefault(importer); | ||
else ApplyExisting(importer); | ||
} | ||
|
||
private bool TryGetTextureImporter (Texture texture, out TextureImporter importer) | ||
{ | ||
var assetPath = AssetDatabase.GetAssetPath(texture); | ||
importer = AssetImporter.GetAtPath(assetPath) as TextureImporter; | ||
return importer != null; | ||
} | ||
|
||
private void ApplyDefault (TextureImporter importer) | ||
{ | ||
importer.textureType = TextureImporterType.Default; | ||
importer.alphaIsTransparency = true; | ||
importer.mipmapEnabled = false; | ||
importer.textureCompression = TextureImporterCompression.Uncompressed; | ||
} | ||
|
||
private void ApplyExisting (TextureImporter importer) | ||
{ | ||
importer.SetTextureSettings(@base); | ||
foreach (var kv in platforms) | ||
if (kv.Key == defaultPlatformId || kv.Value.overridden) | ||
importer.SetPlatformTextureSettings(kv.Value); | ||
} | ||
|
||
[SuppressMessage("ReSharper", "PossibleNullReferenceException")] | ||
private static string[] GetPlatformIds () | ||
{ | ||
// https://github.com/Unity-Technologies/UnityCsReference/blob/2019.4/Editor/Mono/BuildPipeline/BuildPlatform.cs | ||
|
||
Type platformsType = default, platformType = default; | ||
foreach (var type in Assembly.GetAssembly(typeof(Editor)).GetTypes()) | ||
if (type.Name == "BuildPlatforms") platformsType = type; | ||
else if (type.Name == "BuildPlatform") platformType = type; | ||
else if (platformsType != null && platformType != null) break; | ||
var platformsInstance = platformsType.GetProperty("instance").GetValue(null); | ||
var validPlatforms = (IEnumerable<object>)platformsType | ||
.GetMethod("GetValidPlatforms", Array.Empty<Type>()) | ||
.Invoke(platformsInstance, null); | ||
return validPlatforms.Select(p => (string)platformType.GetField("name").GetValue(p)).ToArray(); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/SpriteDicing/Editor/Processors/TextureSettings.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"name": "com.elringus.spritedicing", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"displayName": "SpriteDicing", | ||
"description": "Unity extension for reusing sprite texture areas.", | ||
"description": "Allows dicing sprite textures into atlases to eliminate identical parts.", | ||
"unity": "2019.4", | ||
"author": { | ||
"name": "Elringus", | ||
"url": "https://elringus.me" | ||
"url": "https://github.com/Elringus" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"dependencies": { | ||
"com.unity.modules.imgui": "1.0.0", | ||
"com.unity.ugui": "1.0.0", | ||
"com.unity.2d.sprite": "1.0.0", | ||
"com.unity.test-framework": "1.1.30", | ||
"com.unity.ide.rider": "3.0.13", | ||
"com.unity.test-framework": "1.1.31", | ||
"com.unity.testtools.codecoverage": "1.1.1", | ||
"com.unity.ide.rider": "3.0.12" | ||
"com.unity.ugui": "1.0.0", | ||
"com.unity.modules.imgui": "1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.