private static void OnSpritePrefabPostprocess( ) { EditorApplication.delayCall -= OnSpritePrefabPostprocess; try { Uni2DAssetPostprocessor.LockTo(false); foreach (string rGameObjectPrefabGUID in ms_oGameObjectGUIDsToPostProcess) { GameObject rGameObjectPrefab = Uni2DEditorUtils.GetAssetFromUnityGUID <GameObject>(rGameObjectPrefabGUID); if (rGameObjectPrefab != null) { //Debug.Log ( "Post processing game object prefab " + rGameObjectPrefabGUID ); Uni2DEditorSpriteBuilderUtils.OnPrefabPostProcess(rGameObjectPrefab); rGameObjectPrefab = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } } } finally { ms_oGameObjectGUIDsToPostProcess.Clear( ); Uni2DAssetPostprocessor.Unlock( ); Uni2DAssetPostprocessor.Enabled = true; } }
private void DisplayBuilder( ) { bool bCreateSprite = false; bool bCreatePhysicSprite = false; bool bCreateAnimationClip = false; EditorGUILayout.BeginHorizontal( ); { EditorGUI.BeginDisabledGroup(m_rSelectedObjects == null || m_rSelectedObjects.Length == 0); { bCreateSprite = GUILayout.Button(mc_oGUILabelCreateSprite); bCreatePhysicSprite = GUILayout.Button(mc_oGUILabelCreatePhysicSprite); bCreateAnimationClip = GUILayout.Button(mc_oGUILabelCreateAnimationClip); } EditorGUI.EndDisabledGroup( ); } EditorGUILayout.EndHorizontal( ); if (bCreateSprite == true || bCreatePhysicSprite == true) { if (m_rSelectedObjects != null) { List <GameObject> oCreatedGameObjects = new List <GameObject>(); foreach (Object rObject in m_rSelectedObjects) { Texture2D rTexture = (Texture2D)rObject; // Create sprite GameObject oSpriteMeshGameObject = CreateSprite(rTexture, ComputeCreateFromButtonPositionWorld(SceneView.lastActiveSceneView), bCreatePhysicSprite); oCreatedGameObjects.Add(oSpriteMeshGameObject); } // Update editor selection Selection.objects = oCreatedGameObjects.ToArray(); MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } } else if (bCreateAnimationClip) { List <Texture2D> oTexturesList = null; if (m_rSelectedObjects != null) { // Object -> Texture2D cast oTexturesList = m_rSelectedObjects.Cast <Texture2D>( ).ToList( ); } Uni2DAnimationClip rAnimationClip = Uni2DEditorSpriteBuilderWindow.CreateAnimationClip(oTexturesList); Selection.objects = new Object[1] { rAnimationClip.gameObject }; } }
// Apply settings public bool ApplySettings(bool a_bUpdateSprites = true) { bool bSuccess; Uni2DAssetPostprocessor.Enabled = false; { int iContainerCount = textures.Length; Texture2D[] oTexturesToPack = new Texture2D[iContainerCount]; for (int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex) { oTexturesToPack[iContainerIndex] = textures[iContainerIndex].Texture; } List <Uni2DTextureImporterSettingsPair> rTextureImporterSettings = Uni2DEditorSpriteBuilderUtils.TexturesProcessingBegin(oTexturesToPack); oTexturesToPack = null; // Look if the atlas is set properly regarding to texture sizes int iOversizedTextures = this.LookForOversizedTextures(textures, padding, maximumAtlasSize); if (iOversizedTextures == 0) { textures = this.SanitizeInputTextures(textures); iContainerCount = textures.Length; string rAtlasGUID = Uni2DEditorUtils.GetUnityAssetGUID(this); Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance; for (int iTextureIndex = 0, iTextureCount = m_rTextureContainers.Length; iTextureIndex < iTextureCount; ++iTextureIndex) { rAssetTable.RemoveAtlasUsingTexture(rAtlasGUID, m_rTextureContainers[iTextureIndex].GUID); } m_rTextureContainers = new Texture2DContainer[iContainerCount]; // Deep copy for (int iContainerIndex = 0; iContainerIndex < iContainerCount; ++iContainerIndex) { Texture2DContainer oTextureContainer = new Texture2DContainer(textures[iContainerIndex]); m_rTextureContainers[iContainerIndex] = oTextureContainer; rAssetTable.AddAtlasUsingTexture(rAtlasGUID, oTextureContainer.GUID); } rAssetTable.Save( ); m_iPadding = padding; m_eMaximumAtlasSize = maximumAtlasSize; bSuccess = Generate( ); if (a_bUpdateSprites) { Uni2DEditorSpriteBuilderUtils.UpdateSpriteInCurrentSceneAndResourcesAccordinglyToAtlasChange(this); } } else // Some textures can't fit { bSuccess = false; Debug.LogWarning("Uni2D could not regenerate atlas '" + (this.gameObject.name) + "' properly: " + iOversizedTextures + " texture" + (iOversizedTextures > 1 ? "s are" : " is") + " too large to fit in the atlas.", this.gameObject); } Uni2DEditorSpriteBuilderUtils.TexturesProcessingEnd(rTextureImporterSettings); } Uni2DAssetPostprocessor.Enabled = true; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); return(bSuccess); }
public void Rebuild( ) { //Debug.Log( "Rebuilding Uni2D Asset Table..." ); m_oAtlasGUIDSpritePrefabGUIDsMultiDict.Clear( ); m_oTextureGUIDAtlasGUIDsMultiDict.Clear( ); m_oTextureGUIDClipGUIDsMultiDict.Clear( ); m_oTextureGUIDSpritePrefabGUIDsMultiDict.Clear( ); m_oAtlasPathGUIDDict.Clear( ); m_oClipPathGUIDDict.Clear( ); m_oTextureImportGUIDDict.Clear(); // Iterate project's assets string[] rAssetPaths = AssetDatabase.GetAllAssetPaths( ); int iAssetCount = rAssetPaths.Length; float fInvAssetCount = 1.0f / (float)iAssetCount; int iProcessedAssets = 0; try { foreach (string rPath in rAssetPaths) { EditorUtility.DisplayProgressBar("Uni2D - Asset Table Rebuilding Progress", iProcessedAssets + " out of " + iAssetCount + " asset(s) processed...", fInvAssetCount * iProcessedAssets); Object rAssetObject = null; // Might be an atlas or a clip if (rPath.EndsWith(".prefab")) { rAssetObject = AssetDatabase.LoadAssetAtPath(rPath, typeof(Uni2DTextureAtlas)); if (rAssetObject != null) // It's an atlas { Uni2DTextureAtlas rAtlasAsset = (Uni2DTextureAtlas)rAssetObject; string rAtlasGUID = AssetDatabase.AssetPathToGUID(rPath); foreach (string rTextureGUID in rAtlasAsset.GetTextureGUIDs( )) { this.AddAtlasUsingTexture(rAtlasGUID, rTextureGUID); } m_oAtlasPathGUIDDict.Add(rPath, rAtlasGUID); rAtlasAsset = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } rAssetObject = AssetDatabase.LoadAssetAtPath(rPath, typeof(Uni2DAnimationClip)); if (rAssetObject != null) // It's an animation clip { Uni2DAnimationClip rAnimationClipAsset = (Uni2DAnimationClip)rAssetObject; string rAnimationClipGUID = AssetDatabase.AssetPathToGUID(rPath); foreach (string rTextureGUID in rAnimationClipAsset.GetAllFramesTextureGUIDs( )) { this.AddClipUsingTexture(rAnimationClipGUID, rTextureGUID); } m_oClipPathGUIDDict.Add(rPath, rAnimationClipGUID); rAnimationClipAsset = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } rAssetObject = AssetDatabase.LoadAssetAtPath(rPath, typeof(GameObject)); if (rAssetObject != null) // It's a sprite prefab { GameObject rPrefabAsset = (GameObject)rAssetObject; string rPrefabGUID = AssetDatabase.AssetPathToGUID(rPath); Uni2DSprite[] rSpritePrefabComponents = rPrefabAsset.GetComponentsInChildren <Uni2DSprite>(true); foreach (Uni2DSprite rSpritePrefabComponent in rSpritePrefabComponents) { Uni2DEditorSpriteSettings rSpriteSettings = rSpritePrefabComponent.SpriteSettings; this.AddSpritePrefabUsingTexture(rPrefabGUID, rSpriteSettings.textureContainer.GUID); if (rSpriteSettings.atlas != null) { this.AddSpritePrefabUsingAtlas(rPrefabGUID, Uni2DEditorUtils.GetUnityAssetGUID(rSpriteSettings.atlas)); } } rPrefabAsset = null; rSpritePrefabComponents = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } } ++iProcessedAssets; } } finally { this.Save( ); MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); EditorUtility.SetDirty(this); EditorUtility.ClearProgressBar( ); } //Debug.Log( "Uni2D Asset Table Rebuild: Done." ); }
private static void OnPostprocessAllAssets(string[] a_rImportedAssets, string[] a_rDeletedAssets, string[] a_rMovedAssets, string[] a_rMovedFromPath) { if (ms_bEnabled) { bool bUpdateAssets = false; bool bPostprocessPrefabs = false; bool bSaveTable = false; Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance; foreach (string rImportedAssetPath in a_rImportedAssets) { Texture2D rImportedTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Texture2D)); if (rImportedTexture != null) { if (Uni2DEditorUtils.IsMarkedAsSourceTexture(rImportedTexture)) { //Debug.Log ( "Imported " + rImportedAssetPath ); string rImportedTextureGUID = AssetDatabase.AssetPathToGUID(rImportedAssetPath); ms_oImportedTextureGUIDs.Add(rImportedTextureGUID); Uni2DEditorUtils.GenerateTextureImportGUID(rImportedTexture); bUpdateAssets = true; rImportedTexture = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); continue; } } Uni2DTextureAtlas rImportedAtlas = (Uni2DTextureAtlas)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Uni2DTextureAtlas)); if (rImportedAtlas != null) { //Debug.Log ( "Imported atlas " + rImportedAssetPath ); bSaveTable = true; rAssetTable.AddAtlasPath(rImportedAssetPath, AssetDatabase.AssetPathToGUID(rImportedAssetPath)); rImportedAtlas = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); continue; } Uni2DAnimationClip rImportedClip = (Uni2DAnimationClip)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(Uni2DAnimationClip)); if (rImportedClip != null) { //Debug.Log ( "Imported clip " + rImportedClip ); bSaveTable = true; rAssetTable.AddClipPath(rImportedAssetPath, AssetDatabase.AssetPathToGUID(rImportedAssetPath)); rImportedClip = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); continue; } GameObject rImportedGameObject = (GameObject)AssetDatabase.LoadAssetAtPath(rImportedAssetPath, typeof(GameObject)); if (rImportedGameObject != null) { //Debug.Log ( "Imported game object " + rImportedAssetPath ); ms_oGameObjectGUIDsToPostProcess.Add(AssetDatabase.AssetPathToGUID(rImportedAssetPath)); bPostprocessPrefabs = true; rImportedGameObject = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } } // Moved assets for (int iIndex = 0, iCount = a_rMovedAssets.Length; iIndex < iCount; ++iIndex) { //Debug.Log ( "Importing moved asset" ); Uni2DTextureAtlas rMovedAtlas = (Uni2DTextureAtlas)AssetDatabase.LoadAssetAtPath(a_rMovedAssets[iIndex], typeof(Uni2DTextureAtlas)); if (rMovedAtlas != null) { rAssetTable.RemoveAtlasFromPath(a_rMovedFromPath[iIndex], false); rAssetTable.AddAtlasPath(a_rMovedAssets[iIndex], AssetDatabase.AssetPathToGUID(a_rMovedAssets[iIndex])); bSaveTable = true; rMovedAtlas = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); continue; } Uni2DAnimationClip rMovedClip = (Uni2DAnimationClip)AssetDatabase.LoadAssetAtPath(a_rMovedAssets[iIndex], typeof(Uni2DAnimationClip)); if (rMovedClip != null) { rAssetTable.RemoveClipFromPath(a_rMovedFromPath[iIndex], false); rAssetTable.AddClipPath(a_rMovedAssets[iIndex], AssetDatabase.AssetPathToGUID(a_rMovedAssets[iIndex])); bSaveTable = true; rMovedClip = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } } // Deleted assets foreach (string rDeletedAsset in a_rDeletedAssets) { string[] rSpritePrefabGUIDs = rAssetTable.GetSpritePrefabGUIDsUsingThisAtlasPath(rDeletedAsset); if (rSpritePrefabGUIDs.Length > 0) { bUpdateAssets = true; ms_oSpritePrefabGUIDsToUpdate.UnionWith(rSpritePrefabGUIDs); } /* * // TODO: mettre des paths au lieu d'IDs * string[ ] rClipGUIDs = rAssetTable.GetClipGUIDsUsingThisTexturePath( rDeletedAsset ); * if( rClipGUIDs.Length > 0 ) * { * bUpdateAssets = true; * ms_oAnimationClipGUIDsToUpdate.UnionWith( rClipGUIDs ); * } */ bSaveTable = rAssetTable.RemoveAtlasFromPath(rDeletedAsset, true) || bSaveTable; bSaveTable = rAssetTable.RemoveClipFromPath(rDeletedAsset, true) || bSaveTable; } if (bSaveTable) { rAssetTable.Save( ); } if (bUpdateAssets) { ms_oAtlasGUIDsToUpdate.UnionWith(rAssetTable.GetAtlasGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs)); ms_oAnimationClipGUIDsToUpdate.UnionWith(rAssetTable.GetClipGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs)); ms_oSpritePrefabGUIDsToUpdate.UnionWith(rAssetTable.GetSpritePrefabGUIDsUsingTheseTextures(ms_oImportedTextureGUIDs)); ms_oSpritePrefabGUIDsToUpdate.UnionWith(rAssetTable.GetSpritePrefabGUIDsUsingTheseAtlases(ms_oAtlasGUIDsToUpdate)); EditorApplication.delayCall += UpdateUni2DAssets; } if (bPostprocessPrefabs) { EditorApplication.delayCall += OnSpritePrefabPostprocess; } } }
private static void UpdateUni2DAssets( ) { EditorApplication.delayCall -= UpdateUni2DAssets; Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance; try { Uni2DAssetPostprocessor.LockTo(false); // Update animation clips first, because they can change the atlases foreach (string rAnimationClipGUID in ms_oAnimationClipGUIDsToUpdate) { Uni2DAnimationClip rAnimationClip = Uni2DEditorUtils.GetAssetFromUnityGUID <Uni2DAnimationClip>(rAnimationClipGUID); if (rAnimationClip != null) { //Debug.Log ( "Updating clip " + rAnimationClipGUID ); rAnimationClip.OnTexturesChange(ms_oImportedTextureGUIDs); rAnimationClip = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } else { // Clean asset table foreach (string rTextureGUID in ms_oImportedTextureGUIDs) { rAssetTable.RemoveClipUsingTexture(rAnimationClipGUID, rTextureGUID); } } } foreach (string rAtlasGUID in ms_oAtlasGUIDsToUpdate) { Uni2DTextureAtlas rAtlas = Uni2DEditorUtils.GetAssetFromUnityGUID <Uni2DTextureAtlas>(rAtlasGUID); if (rAtlas != null) { //Debug.Log( "Updating atlas " + rAtlasGUID ); rAtlas.OnTextureChange( ); rAtlas = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } else { // Clean foreach (string rTextureGUID in ms_oImportedTextureGUIDs) { rAssetTable.RemoveAtlasUsingTexture(rAtlasGUID, rTextureGUID); } } } foreach (string rSpritePrefabGUID in ms_oSpritePrefabGUIDsToUpdate) { GameObject rSpritePrefab = Uni2DEditorUtils.GetAssetFromUnityGUID <GameObject>(rSpritePrefabGUID); if (rSpritePrefab != null) { //Debug.Log( "Updating sprite prefab " + rSpritePrefabGUID ); foreach (Uni2DSprite rSpritePrefabComponent in rSpritePrefab.GetComponentsInChildren <Uni2DSprite>(true)) { Uni2DEditorSpriteSettings rSpriteSettings = rSpritePrefabComponent.SpriteSettings; string rSpriteTextureGUID = rSpriteSettings.textureContainer.GUID; string rSpriteAtlasGUID = rSpriteSettings.atlas != null?Uni2DEditorUtils.GetUnityAssetGUID(rSpriteSettings.atlas) : null; if (ms_oImportedTextureGUIDs.Contains(rSpriteTextureGUID) || (!string.IsNullOrEmpty(rSpriteAtlasGUID) && ms_oAtlasGUIDsToUpdate.Contains(rSpriteAtlasGUID))) { rSpritePrefabComponent.Regenerate(true); } MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } rSpritePrefab = null; MultiUnityVersionSupportUtility.UnloadUnusedAssets( ); } else { // Clean foreach (string rTextureGUID in ms_oImportedTextureGUIDs) { rAssetTable.RemoveSpritePrefabUsingTexture(rSpritePrefabGUID, rTextureGUID); } foreach (string rAtlasGUID in ms_oAtlasGUIDsToUpdate) { rAssetTable.RemoveSpritePrefabUsingAtlas(rSpritePrefabGUID, rAtlasGUID); } } } } finally { ms_oImportedTextureGUIDs.Clear( ); ms_oAtlasGUIDsToUpdate.Clear( ); ms_oAnimationClipGUIDsToUpdate.Clear( ); ms_oSpritePrefabGUIDsToUpdate.Clear( ); rAssetTable.Save( ); Uni2DAssetPostprocessor.Unlock( ); Uni2DAssetPostprocessor.Enabled = true; } }