Skip to content

Commit 9fb253b

Browse files
Merge pull request #1764 from Zod-/keyboard_atlas_references
Automatic update for keyboard atlas references
2 parents ed0534b + 10f1c57 commit 9fb253b

15 files changed

+273
-38
lines changed

Assets/HoloToolkit/UX/Textures/KeyboardAtlas.spriteatlas

+10-38
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,18 @@ SpriteAtlas:
2626
allowAlphaSplitting: 0
2727
enableRotation: 0
2828
enableTightPacking: 0
29-
packedHash:
30-
serializedVersion: 2
31-
Hash: b579d9a28ce9cc7d8109d453a20bac11
3229
variantMultiplier: 1
33-
bindAsDefault: 1
34-
finalFormat: 4
35-
hashString: b579d9a28ce9cc7d8109d453a20bac11
36-
totalSpriteSurfaceArea: 2359296
3730
packables:
38-
- {fileID: 102900000, guid: ebf9a4ed0c92cc24483bdd4cca5078c2, type: 3}
39-
packedSpriteRenderDataKeys:
40-
- 1dde69ccd9583fa4f8dfab2b590119ee: 21300000
41-
- 161fc5872226601449a8143014ece469: 21300000
42-
- 73830216a21a45e499598b2624ef723e: 21300000
43-
- 5eb0b332d494ca84d9c38cf77ba0e12d: 21300000
44-
- ec8b4073237c9724ba8422f71fda3694: 21300000
45-
- 11e4524f21fc6434abbbba83d70d0040: 21300000
46-
- 7029cbbbce5f0ac4dbc4e93267bfddf4: 21300000
47-
- b575d7b7d87461b4ea9965b50e367249: 21300000
48-
- f6f44dc3b387f9d48bcaaee0038969c4: 21300000
31+
- {fileID: 21300000, guid: 7029cbbbce5f0ac4dbc4e93267bfddf4, type: 3}
32+
- {fileID: 21300000, guid: 2775a1cee10328748889e019ab01d248, type: 3}
33+
- {fileID: 21300000, guid: b575d7b7d87461b4ea9965b50e367249, type: 3}
34+
- {fileID: 21300000, guid: 5eb0b332d494ca84d9c38cf77ba0e12d, type: 3}
35+
- {fileID: 21300000, guid: ec8b4073237c9724ba8422f71fda3694, type: 3}
36+
- {fileID: 21300000, guid: 11e4524f21fc6434abbbba83d70d0040, type: 3}
37+
- {fileID: 21300000, guid: 73830216a21a45e499598b2624ef723e, type: 3}
38+
bindAsDefault: 0
4939
m_MasterAtlas: {fileID: 0}
50-
m_PackedSprites:
51-
- {fileID: 21300000, guid: 1dde69ccd9583fa4f8dfab2b590119ee, type: 3}
52-
- {fileID: 21300000, guid: 161fc5872226601449a8143014ece469, type: 3}
53-
- {fileID: 21300000, guid: 73830216a21a45e499598b2624ef723e, type: 3}
54-
- {fileID: 21300000, guid: 5eb0b332d494ca84d9c38cf77ba0e12d, type: 3}
55-
- {fileID: 21300000, guid: ec8b4073237c9724ba8422f71fda3694, type: 3}
56-
- {fileID: 21300000, guid: 11e4524f21fc6434abbbba83d70d0040, type: 3}
57-
- {fileID: 21300000, guid: 7029cbbbce5f0ac4dbc4e93267bfddf4, type: 3}
58-
- {fileID: 21300000, guid: b575d7b7d87461b4ea9965b50e367249, type: 3}
59-
- {fileID: 21300000, guid: f6f44dc3b387f9d48bcaaee0038969c4, type: 3}
60-
m_PackedSpriteNamesToIndex:
61-
- KeyboardKeyGlyphs_Shift_Symbols
62-
- KeyboardKeyGlyphs_Down
63-
- KeyboardKeyGlyphs_Up
64-
- KeyboardKeyGlyphs_Backspace
65-
- KeyboardKeyGlyphs_Left
66-
- KeyboardKeyGlyphs_Right
67-
- KeyboardKeyGlyphs_Close
68-
- KeyboardKeyGlyphs_Dictation
69-
- KeyboardKeyGlyphs_UnShift_Symbols
40+
m_PackedSprites: []
41+
m_PackedSpriteNamesToIndex: []
7042
m_Tag: KeyboardAtlas
7143
m_IsVariant: 0

Assets/HoloToolkit/UX/Textures/KeyboardAtlas.spriteatlas.meta

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_PrefabParentObject: {fileID: 0}
7+
m_PrefabInternal: {fileID: 0}
8+
m_GameObject: {fileID: 0}
9+
m_Enabled: 1
10+
m_EditorHideFlags: 0
11+
m_Script: {fileID: 11500000, guid: c54e0bfe5b1c3b6498e6b71642081d47, type: 3}
12+
m_Name: KeyboardAtlasReference
13+
m_EditorClassIdentifier:
14+
Prefabs:
15+
- {fileID: 146120, guid: c4e389e44d96da64ea974ab237ce1a9a, type: 2}
16+
Atlas: {fileID: 4343727234628468602, guid: f22af9763a172c64d893dd21dcb4105a, type: 2}

Assets/HoloToolkit/UX/Textures/KeyboardAtlasReference.asset.meta

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/HoloToolkit/Utilities/Scripts/AtlasReferenceUpdater.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.U2D;
6+
7+
namespace HoloToolkit.Unity
8+
{
9+
public class AtlasPrefabReference : ScriptableObject
10+
{
11+
[SerializeField] public List<GameObject> Prefabs;
12+
[SerializeField] public SpriteAtlas Atlas;
13+
}
14+
}

Assets/HoloToolkit/Utilities/Scripts/AtlasReferenceUpdater/AtlasPrefabReference.cs.meta

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/HoloToolkit/Utilities/Scripts/AtlasReferenceUpdater/Editor.meta

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using UnityEditor;
6+
using UnityEngine;
7+
using UnityEngine.UI;
8+
9+
namespace HoloToolkit.Unity
10+
{
11+
[InitializeOnLoad]
12+
public class AtlasReferenceUpdater : UnityEditor.AssetModificationProcessor
13+
{
14+
private static readonly List<AtlasPrefabReference> References = new List<AtlasPrefabReference>();
15+
16+
static AtlasReferenceUpdater()
17+
{
18+
FindAtlasPrefabReferences();
19+
UpdateAllReferences();
20+
PrefabUtility.prefabInstanceUpdated += PrefabInstanceUpdated;
21+
}
22+
23+
private static void FindAtlasPrefabReferences()
24+
{
25+
References.Clear();
26+
foreach (var reference in FindAllAssets<AtlasPrefabReference>())
27+
{
28+
if (reference.Atlas == null)
29+
{
30+
Debug.LogWarning("No sprite atlas referenced: " + reference.name);
31+
continue;
32+
}
33+
if (reference.Prefabs.Any(o => o == null))
34+
{
35+
Debug.LogWarning("One or more prefab references are null: " + reference.name);
36+
continue;
37+
}
38+
References.Add(reference);
39+
}
40+
}
41+
42+
private static void UpdateAllReferences()
43+
{
44+
foreach (var atlasPrefabReference in References)
45+
{
46+
UpdateAtlasReferences(atlasPrefabReference);
47+
}
48+
}
49+
50+
private static void PrefabInstanceUpdated(GameObject instance)
51+
{
52+
var prefab = PrefabUtility.GetPrefabParent(instance) as GameObject;
53+
foreach (var atlasPrefabReference in References)
54+
{
55+
if (atlasPrefabReference.Prefabs.Contains(prefab))
56+
{
57+
UpdateAtlasReferences(atlasPrefabReference);
58+
}
59+
}
60+
}
61+
private static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions options)
62+
{
63+
var atlasReference = AssetDatabase.LoadAssetAtPath<AtlasPrefabReference>(assetPath);
64+
if (atlasReference != null)
65+
{
66+
EditorApplication.delayCall += FindAtlasPrefabReferences;
67+
return AssetDeleteResult.DidNotDelete;
68+
}
69+
70+
var deletedSprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
71+
if (deletedSprite != null)
72+
{
73+
foreach (var reference in References.Where(_ref => _ref.Atlas.ContainsSprite(deletedSprite)))
74+
{
75+
var localRef = reference;
76+
EditorApplication.delayCall += () => UpdateAtlasReferences(localRef);
77+
}
78+
return AssetDeleteResult.DidNotDelete;
79+
}
80+
81+
return AssetDeleteResult.DidNotDelete;
82+
}
83+
84+
private static string[] OnWillSaveAssets(string[] paths)
85+
{
86+
if (paths.Select(AssetDatabase.LoadAssetAtPath<AtlasPrefabReference>).Any(_ref => _ref != null))
87+
{
88+
EditorApplication.delayCall += FindAtlasPrefabReferences;
89+
EditorApplication.delayCall += UpdateAllReferences;
90+
}
91+
return paths;
92+
}
93+
94+
private static IEnumerable<T> FindAllAssets<T>() where T : UnityEngine.Object
95+
{
96+
var type = typeof(T).FullName;
97+
foreach (var assetGuid in AssetDatabase.FindAssets("t:" + type))
98+
{
99+
var obj = AssetDatabase.LoadAssetAtPath<T>(AssetDatabase.GUIDToAssetPath(assetGuid));
100+
if (obj != null)
101+
{
102+
yield return obj;
103+
}
104+
}
105+
}
106+
107+
public static void UpdateAtlasReferences(AtlasPrefabReference reference)
108+
{
109+
var uniqueSprites = GetDistinctSprites(reference.Prefabs).ToList();
110+
reference.Atlas.SetSprites(uniqueSprites);
111+
}
112+
113+
private static IEnumerable<Sprite> GetDistinctSprites(IEnumerable<GameObject> prefabs)
114+
{
115+
return prefabs.SelectMany(p => p.GetComponentsInChildren<Image>())
116+
.Select(img => img.sprite).Where(img => img != null).Distinct();
117+
}
118+
}
119+
}

Assets/HoloToolkit/Utilities/Scripts/AtlasReferenceUpdater/Editor/AtlasReferenceUpdater.cs.meta

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/HoloToolkit/Utilities/Scripts/AtlasReferenceUpdater/Editor/Extensions.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using System.Collections.Generic;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace HoloToolkit.Unity
8+
{
9+
public static class SerializedPropertyExtensions
10+
{
11+
public static void SetObjects<T>(this SerializedProperty property, IList<T> objects) where T : Object
12+
{
13+
property.arraySize = objects.Count;
14+
for (var i = 0; i < objects.Count; i++)
15+
{
16+
property.GetArrayElementAtIndex(i).objectReferenceValue = objects[i];
17+
}
18+
}
19+
}
20+
}

Assets/HoloToolkit/Utilities/Scripts/AtlasReferenceUpdater/Editor/Extensions/SerializedPropertyExtensions.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using System.Collections.Generic;
4+
using UnityEditor;
5+
using UnityEngine;
6+
using UnityEngine.U2D;
7+
8+
namespace HoloToolkit.Unity
9+
{
10+
public static class SpriteAtlasExtensions
11+
{
12+
public const string SpritePackables = "m_EditorData.packables";
13+
14+
public static void SetSprites(this SpriteAtlas spriteAtlas, IList<Sprite> sprites)
15+
{
16+
var serializedObject = new SerializedObject(spriteAtlas);
17+
var packables = serializedObject.FindProperty(SpritePackables);
18+
packables.SetObjects(sprites);
19+
serializedObject.ApplyModifiedProperties();
20+
}
21+
22+
public static bool ContainsSprite(this SpriteAtlas spriteAtlas, Sprite sprite)
23+
{
24+
var serializedObject = new SerializedObject(spriteAtlas);
25+
var packables = serializedObject.FindProperty(SpritePackables);
26+
for (var i = 0; i < packables.arraySize; i++)
27+
{
28+
var containedSprite = packables.GetArrayElementAtIndex(i).objectReferenceValue as Sprite;
29+
if (sprite != containedSprite) { continue; }
30+
return true;
31+
}
32+
return false;
33+
}
34+
}
35+
}

Assets/HoloToolkit/Utilities/Scripts/AtlasReferenceUpdater/Editor/Extensions/SpriteAtlasExtensions.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)