// Copy clips private void CopyFrames(List <Uni2DAnimationFrame> a_rFramesSource, List <Uni2DAnimationFrame> a_rFramesDestination) { // Update asset table Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance; string rClipGUID = Uni2DEditorUtils.GetUnityAssetGUID(this); foreach (Uni2DAnimationFrame rOldFrame in a_rFramesDestination) { string oTextureGUID = rOldFrame.textureContainer != null ? rOldFrame.textureContainer.GUID : null; if (!string.IsNullOrEmpty(oTextureGUID)) { rAssetTable.RemoveClipUsingTexture(rClipGUID, oTextureGUID); } } a_rFramesDestination.Clear( ); foreach (Uni2DAnimationFrame rFrameSource in a_rFramesSource) { a_rFramesDestination.Add(new Uni2DAnimationFrame(rFrameSource)); string oTextureGUID = rFrameSource.textureContainer != null ? rFrameSource.textureContainer.GUID : null; if (!string.IsNullOrEmpty(oTextureGUID)) { rAssetTable.AddClipUsingTexture(rClipGUID, oTextureGUID); } } rAssetTable.Save( ); }
public Texture2DContainer(Texture2D a_rTexture, bool a_bKeepTextureReference) { m_oTextureGUID = Uni2DEditorUtils.GetUnityAssetGUID(a_rTexture); if (a_bKeepTextureReference) { m_rTexture = a_rTexture; } }
// 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; EditorUtility.UnloadUnusedAssets( ); return(bSuccess); }
public Texture2D GetAtlasTexture(Texture2D a_rTexture) { return(this.GetAtlasTexture(Uni2DEditorUtils.GetUnityAssetGUID(a_rTexture))); }
public Material GetMaterial(Texture2D a_rTexture) { return(this.GetMaterial(Uni2DEditorUtils.GetUnityAssetGUID(a_rTexture))); }
public bool Contains(Texture2D a_rTexture) { return(this.Contains(Uni2DEditorUtils.GetUnityAssetGUID(a_rTexture))); }
public bool GetUVs(Texture2D a_rTexture, out Rect a_rUVs, out bool a_bIsFlipped) { return(this.GetUVs(Uni2DEditorUtils.GetUnityAssetGUID(a_rTexture), out a_rUVs, out a_bIsFlipped)); }
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( ); // 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; EditorUtility.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; EditorUtility.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; EditorUtility.UnloadUnusedAssets( ); } } ++iProcessedAssets; } } finally { this.Save( ); EditorUtility.UnloadUnusedAssets( ); EditorUtility.SetDirty(this); EditorUtility.ClearProgressBar( ); } //Debug.Log( "Uni2D Asset Table Rebuild: Done." ); }
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; EditorUtility.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; EditorUtility.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); } EditorUtility.UnloadUnusedAssets( ); } rSpritePrefab = null; EditorUtility.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; } }
public static Uni2DTextureAtlas AtlasPopup(Uni2DTextureAtlas a_rTextureAtlas, IEnumerable <string> a_rTextureGUIDs, params GUILayoutOption[] a_rGUILayoutOptions) { // Get button control ID int iControlID = GUIUtility.GetControlID(FocusType.Passive); // Get selected value for our control // If no PopupCallbackInfo instance exists, the returned value is a_rTextureAtlas a_rTextureAtlas = PopupCallbackInfo <Uni2DTextureAtlas> .GetSelectedValueForControl(iControlID, a_rTextureAtlas); // Create a new generic menu // Each item menu will use AtlasPopupCallback as callback // AtlasPopupCallback will perform the logic and save the selected atlas to // the PopupCallbackInfo instance. string oPopupSelected = EditorGUI.showMixedValue ? "-" : (a_rTextureAtlas != null ? a_rTextureAtlas.name : "(None)"); if (GUILayout.Button(oPopupSelected, EditorStyles.popup, a_rGUILayoutOptions)) { string rAtlasGUID = Uni2DEditorUtils.GetUnityAssetGUID(a_rTextureAtlas); // Create a new popup callback info (control ID) and save it as current instance PopupCallbackInfo <Uni2DTextureAtlas> .instance = new PopupCallbackInfo <Uni2DTextureAtlas>(iControlID, a_rTextureAtlas); // Create our generic menu GenericMenu oPopupMenu = new GenericMenu( ); if (a_rTextureAtlas != null) { oPopupMenu.AddItem(new GUIContent(a_rTextureAtlas.name), true, AtlasPopupCallback, rAtlasGUID); oPopupMenu.AddSeparator(""); } // "None" special item menu oPopupMenu.AddItem(new GUIContent("(None)", "No atlasing"), a_rTextureAtlas == null, AtlasPopupCallback, ""); oPopupMenu.AddSeparator(""); // "Create" special item menu oPopupMenu.AddItem(new GUIContent("Create a new atlas...", "Creates a new Uni2D atlas and add the texture(s) right away"), false, AtlasPopupCallback, "NEW"); Uni2DEditorAssetTable rAssetTable = Uni2DEditorAssetTable.Instance; // List atlases containing the texture(s) Dictionary <string, string> oAtlasesReadyToUse = rAssetTable.GetAtlasNamesUsingTheseTextures(a_rTextureGUIDs); if (!string.IsNullOrEmpty(rAtlasGUID)) { oAtlasesReadyToUse.Remove(rAtlasGUID); } if (oAtlasesReadyToUse.Count > 0) { oPopupMenu.AddSeparator(""); // Add an item menu for each ready to use atlas foreach (KeyValuePair <string, string> rAtlasNameGUIDPair in oAtlasesReadyToUse) { oPopupMenu.AddItem(new GUIContent(rAtlasNameGUIDPair.Value), rAtlasNameGUIDPair.Key == rAtlasGUID, AtlasPopupCallback, rAtlasNameGUIDPair.Key); } } // List all available atlases Dictionary <string, string> oAvailableAtlases = rAssetTable.GetAllAtlasNames( ); if (oAvailableAtlases.Count > 0) { oPopupMenu.AddSeparator(""); // Add an item menu for each available atlas, in a submenu foreach (KeyValuePair <string, string> rAtlasNameGUIDPair in oAvailableAtlases) { oPopupMenu.AddItem(new GUIContent("All atlases/" + rAtlasNameGUIDPair.Value), rAtlasNameGUIDPair.Key == rAtlasGUID, AtlasPopupCallback, rAtlasNameGUIDPair.Key); } } // Finally show up the menu oPopupMenu.ShowAsContext( ); } return(a_rTextureAtlas); }
// On inspector gui public override void OnInspectorGUI( ) { Uni2DTextureAtlas rAtlas = target as Uni2DTextureAtlas; #if BEFORE_UNITY_4_3 EditorGUIUtility.LookLikeInspector( ); #endif // Material override EditorGUILayout.BeginVertical( ); { rAtlas.materialOverride = (Material)EditorGUILayout.ObjectField("Material Override", rAtlas.materialOverride, typeof(Material), false); rAtlas.maximumAtlasSize = (AtlasSize)EditorGUILayout.EnumPopup("Maximum Atlas Size", rAtlas.maximumAtlasSize); rAtlas.padding = EditorGUILayout.IntField("Padding", rAtlas.padding); rAtlas.padding = Mathf.Abs(rAtlas.padding); // Texture to pack list // Custom list GUI: displays Texture2D objects, handles asset GUIDs serializedObject.Update( ); SerializedProperty rSerializedProperty_Textures = serializedObject.FindProperty("textures"); int iContainerIndex = 0; EditorGUILayout.Space( ); while (true) { string oPropertyPath = rSerializedProperty_Textures.propertyPath; string oPropertyName = rSerializedProperty_Textures.name; bool bIsTextureContainer = oPropertyPath.Contains("textures"); // Indent EditorGUI.indentLevel = rSerializedProperty_Textures.depth; if (bIsTextureContainer) { if (oPropertyName == "textures") { GUIContent oGUIContentTexturesLabel = new GUIContent("Textures"); Rect rFoldoutRect = GUILayoutUtility.GetRect(oGUIContentTexturesLabel, EditorStyles.foldout); Event rCurrentEvent = Event.current; switch (rCurrentEvent.type) { // Drag performed case EventType.DragPerform: { // Check if dragged objects are inside the foldout rect if (rFoldoutRect.Contains(rCurrentEvent.mousePosition)) { // Accept and use the event DragAndDrop.AcceptDrag( ); rCurrentEvent.Use( ); EditorGUIUtility.hotControl = 0; DragAndDrop.activeControlID = 0; // Add the textures to the current list foreach (Object rDraggedObject in DragAndDrop.objectReferences) { if (rDraggedObject is Texture2D) { int iCurrentSize = rSerializedProperty_Textures.arraySize; ++rSerializedProperty_Textures.arraySize; SerializedProperty rSerializedProperty_Data = rSerializedProperty_Textures.GetArrayElementAtIndex(iCurrentSize); rSerializedProperty_Data = rSerializedProperty_Data.FindPropertyRelative("m_oTextureGUID"); rSerializedProperty_Data.stringValue = rDraggedObject != null ? Uni2DEditorUtils.GetUnityAssetGUID((Texture2D)rDraggedObject) : null; } } } } break; case EventType.DragUpdated: { if (rFoldoutRect.Contains(rCurrentEvent.mousePosition)) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; } } break; } EditorGUI.indentLevel = 0; ms_bTexturesFoldout = EditorGUI.Foldout(rFoldoutRect, ms_bTexturesFoldout, oGUIContentTexturesLabel); } else if (oPropertyName == "data") { SerializedProperty rSerializedProperty_TextureGUID = rSerializedProperty_Textures.FindPropertyRelative("m_oTextureGUID"); Texture2D rTexture = Uni2DEditorUtils.GetAssetFromUnityGUID <Texture2D>(rSerializedProperty_TextureGUID.stringValue); EditorGUI.BeginChangeCheck( ); { rTexture = (Texture2D)EditorGUILayout.ObjectField("Element " + iContainerIndex, rTexture, typeof(Texture2D), false); ++iContainerIndex; } if (EditorGUI.EndChangeCheck( )) { rSerializedProperty_TextureGUID.stringValue = rTexture != null ? Uni2DEditorUtils.GetUnityAssetGUID(rTexture) : null; } } else { // Default draw of the property field EditorGUILayout.PropertyField(rSerializedProperty_Textures); } } if (rSerializedProperty_Textures.NextVisible(ms_bTexturesFoldout) == false) { break; } } serializedObject.ApplyModifiedProperties( ); EditorGUILayout.Space( ); EditorGUI.indentLevel = 0; ///// Generated assets section ///// // Materials ms_bGeneratedMaterialsFoldout = EditorGUILayout.Foldout(ms_bGeneratedMaterialsFoldout, "Generated Materials"); if (ms_bGeneratedMaterialsFoldout) { ++EditorGUI.indentLevel; { if (ms_oAtlasMaterials.Length > 0) { foreach (Material rAtlasMaterial in ms_oAtlasMaterials) { EditorGUILayout.BeginHorizontal( ); { GUILayout.Space(16.0f); if (GUILayout.Button(EditorGUIUtility.ObjectContent(rAtlasMaterial, typeof(Material)), EditorStyles.label, GUILayout.ExpandWidth(false), GUILayout.MaxWidth(225.0f), GUILayout.MaxHeight(16.0f))) { EditorGUIUtility.PingObject(rAtlasMaterial); } } EditorGUILayout.EndHorizontal( ); } } else { EditorGUILayout.PrefixLabel("(None)"); } } --EditorGUI.indentLevel; } EditorGUILayout.Space( ); // Atlas textures ms_bGeneratedTexturesFoldout = EditorGUILayout.Foldout(ms_bGeneratedTexturesFoldout, "Generated Textures"); if (ms_bGeneratedTexturesFoldout) { ++EditorGUI.indentLevel; { if (ms_oAtlasTextures.Length > 0) { foreach (Texture2D rAtlasTexture in ms_oAtlasTextures) { EditorGUILayout.BeginHorizontal( ); { GUILayout.Space(16.0f); if (GUILayout.Button(EditorGUIUtility.ObjectContent(rAtlasTexture, typeof(Texture2D)), EditorStyles.label, GUILayout.ExpandWidth(false), GUILayout.MaxWidth(225.0f), GUILayout.MaxHeight(16.0f))) { EditorGUIUtility.PingObject(rAtlasTexture); } } EditorGUILayout.EndHorizontal( ); } } else { EditorGUILayout.PrefixLabel("(None)"); } } --EditorGUI.indentLevel; } bool bUnappliedSettings = rAtlas.UnappliedSettings; EditorGUI.BeginDisabledGroup(bUnappliedSettings == false); { // Apply/Revert EditorGUILayout.BeginHorizontal( ); { if (GUILayout.Button("Apply")) { this.ApplySettings( ); } if (GUILayout.Button("Revert")) { rAtlas.RevertSettings( ); } } EditorGUILayout.EndHorizontal( ); } EditorGUI.EndDisabledGroup(); // Generate if (GUILayout.Button("Force atlas regeneration")) { this.ApplySettings( ); } } EditorGUILayout.EndVertical( ); }