1010using System . Reflection ;
1111using System . Globalization ;
1212using System . Collections . Generic ;
13- using System . Diagnostics ;
1413using System . Diagnostics . CodeAnalysis ;
1514using Unity . VectorGraphics . Editor ;
1615using UnityEditor ;
2120using Trackman ;
2221using Object = UnityEngine . Object ;
2322using Debug = UnityEngine . Debug ;
23+ using PackageInfo = UnityEditor . PackageManager . PackageInfo ;
2424
2525// ReSharper disable MemberCanBeMadeStatic.Local
2626
2727namespace Figma . Inspectors
2828{
29- using Attributes ;
3029 using global ;
31- using PackageInfo = UnityEditor . PackageManager . PackageInfo ;
30+ using Attributes ;
3231
3332 [ CustomEditor ( typeof ( Figma ) , true ) ]
3433 [ SuppressMessage ( "Roslynator" , "RCS1213:Remove unused member declaration." ) ]
3534 public class FigmaInspector : Editor
3635 {
37- const string uiDocumentsOnlyIcon = "d_Refresh@2x" ;
38- const string uiDocumentWithImagesIcon = "d_RawImage Icon" ;
36+ const string api = "https://api.figma.com/v1" ;
37+ const string documentsOnlyIcon = "d_Refresh@2x" ;
38+ const string documentWithImagesIcon = "d_RawImage Icon" ;
3939 const string folderIcon = "d_Project" ;
4040 const int maxConcurrentRequests = 5 ;
41- const string api = "https://api.figma.com/v1" ;
42- static readonly string [ ] propertiesToCut = { "componentProperties" } ;
4341
4442 #region Fields
4543 SerializedProperty title ;
@@ -48,11 +46,11 @@ public class FigmaInspector : Editor
4846 SerializedProperty fontsDirs ;
4947
5048 UIDocument document ;
51- List < PackageInfo > packages = new ( ) ;
49+ readonly List < PackageInfo > packages = new ( ) ;
5250 #endregion
5351
5452 #region Properties
55- static string PAT
53+ static string PersonalAccessToken
5654 {
5755 get => EditorPrefs . GetString ( "Figma/Editor/PAT" , "" ) ;
5856 set => EditorPrefs . SetString ( "Figma/Editor/PAT" , value ) ;
@@ -91,33 +89,31 @@ public override void OnInspectorGUI()
9189
9290 void OnPersonalAccessTokenGUI ( )
9391 {
94- if ( PAT . NotNullOrEmpty ( ) )
92+ if ( PersonalAccessToken . NotNullOrEmpty ( ) )
9593 {
9694 GUILayout . BeginHorizontal ( ) ;
9795 GUI . color = Color . green ;
9896 EditorGUILayout . LabelField ( "Personal Access Token OK" ) ;
9997 GUI . color = Color . white ;
10098 GUI . backgroundColor = Color . red ;
101- if ( GUILayout . Button ( new GUIContent ( "X" , "Remove old PAT and enter new PAT" ) , GUILayout . Width ( 25 ) , GUILayout . Height ( 25 ) ) ) PAT = "" ;
99+ if ( GUILayout . Button ( new GUIContent ( "X" , "Remove old PAT and enter new PAT" ) , GUILayout . Width ( 25 ) , GUILayout . Height ( 25 ) ) ) PersonalAccessToken = "" ;
102100 GUI . backgroundColor = Color . white ;
103101 GUILayout . EndHorizontal ( ) ;
104102 }
105103 else
106104 {
107- PAT = EditorGUILayout . TextField ( "Personal Access Token" , PAT ) ;
105+ PersonalAccessToken = EditorGUILayout . TextField ( "Personal Access Token" , PersonalAccessToken ) ;
108106 }
109107 }
110108 void OnAssetGUI ( )
111109 {
112- IEnumerable < string > FontsDirs ( )
113- {
114- foreach ( SerializedProperty fontsDir in fontsDirs )
115- yield return fontsDir . stringValue ;
116- }
117110 async void Update ( string assetPath , bool downloadImages )
118111 {
119- string folder ;
120- string relativeFolder ;
112+ IEnumerable < string > GetFontsDirs ( )
113+ {
114+ foreach ( SerializedProperty fontsDir in fontsDirs )
115+ yield return fontsDir . stringValue ;
116+ }
121117
122118 if ( assetPath . NullOrEmpty ( ) )
123119 {
@@ -130,21 +126,10 @@ async void Update(string assetPath, bool downloadImages)
130126 }
131127 }
132128
133- if ( assetPath . StartsWith ( "Packages" ) )
134- {
135- PackageInfo packageInfo = PackageInfo . FindForAssetPath ( assetPath ) ;
136- folder = $ "{ packageInfo . resolvedPath } { Path . GetDirectoryName ( assetPath . Replace ( packageInfo . assetPath , "" ) ) } ";
137- relativeFolder = Path . GetDirectoryName ( assetPath ) ;
138- }
139- else
140- {
141- folder = Path . GetDirectoryName ( assetPath ) ;
142- relativeFolder = Path . GetRelativePath ( Directory . GetCurrentDirectory ( ) , folder ) ;
143- }
144-
145- if ( ! folder . NotNullOrEmpty ( ) ) return ;
129+ ( string folder , string relativeFolder ) = GetFolderAndRelativeFolder ( assetPath ) ;
130+ if ( folder . NullOrEmpty ( ) || relativeFolder . NullOrEmpty ( ) ) return ;
146131
147- await UpdateTitleAsync ( document , ( Figma ) target , title . stringValue , folder , relativeFolder , Event . current . modifiers == EventModifiers . Control , downloadImages , FontsDirs ( ) . ToArray ( ) ) ;
132+ await UpdateTitleWithProgressAsync ( document , ( Figma ) target , title . stringValue , folder , relativeFolder , Event . current . modifiers == EventModifiers . Control , downloadImages , GetFontsDirs ( ) . ToArray ( ) ) ;
148133 }
149134
150135 EditorGUILayout . BeginVertical ( GUI . skin . box ) ;
@@ -160,8 +145,8 @@ async void Update(string assetPath, bool downloadImages)
160145 bool forceUpdate = default ;
161146 bool downloadImages = false ;
162147
163- if ( GUILayout . Button ( new GUIContent ( "Update UI" , EditorGUIUtility . IconContent ( uiDocumentsOnlyIcon ) . image ) , GUILayout . Height ( 20 ) ) ||
164- ( downloadImages = GUILayout . Button ( new GUIContent ( "Update UI & Images" , EditorGUIUtility . IconContent ( uiDocumentWithImagesIcon ) . image ) , GUILayout . Width ( 184 ) , GUILayout . Height ( 20 ) ) ) ||
148+ if ( GUILayout . Button ( new GUIContent ( "Update UI" , EditorGUIUtility . IconContent ( documentsOnlyIcon ) . image ) , GUILayout . Height ( 20 ) ) ||
149+ ( downloadImages = GUILayout . Button ( new GUIContent ( "Update UI & Images" , EditorGUIUtility . IconContent ( documentWithImagesIcon ) . image ) , GUILayout . Width ( 184 ) , GUILayout . Height ( 20 ) ) ) ||
165150 ( forceUpdate = GUILayout . Button ( new GUIContent ( EditorGUIUtility . FindTexture ( folderIcon ) ) , GUILayout . Width ( 36 ) ) ) )
166151 Update ( forceUpdate ? default : AssetDatabase . GetAssetPath ( visualTreeAsset ) ,
167152 forceUpdate ? EditorUtility . DisplayDialog ( "Figma Updater" , "Do you want to update images as well?" , "Yes" , "No" ) : downloadImages ) ;
@@ -203,15 +188,10 @@ void OnFigmaGUI()
203188 #endregion
204189
205190 #region Support Methods
206- static async Task UpdateDocumentAsync ( UIDocument document , Figma figma , string title , bool downloadImages , bool systemCopyBuffer , IReadOnlyCollection < string > fontDirs )
191+ static ( string folder , string relativeFolder ) GetFolderAndRelativeFolder ( string assetPath )
207192 {
208193 string folder ;
209194 string relativeFolder ;
210- string assetPath = AssetDatabase . GetAssetPath ( document . visualTreeAsset ) ;
211-
212- if ( assetPath . NullOrEmpty ( ) )
213- throw new NotSupportedException ( ) ;
214-
215195 if ( assetPath . StartsWith ( "Packages" ) )
216196 {
217197 PackageInfo packageInfo = PackageInfo . FindForAssetPath ( assetPath ) ;
@@ -223,11 +203,19 @@ static async Task UpdateDocumentAsync(UIDocument document, Figma figma, string t
223203 folder = Path . GetDirectoryName ( assetPath ) ;
224204 relativeFolder = Path . GetRelativePath ( Directory . GetCurrentDirectory ( ) , folder ) ;
225205 }
206+ return ( folder , relativeFolder ) ;
207+ }
208+ static async Task UpdateDocumentAsync ( UIDocument document , Figma figma , string title , bool downloadImages , bool systemCopyBuffer , IReadOnlyCollection < string > fontDirs )
209+ {
210+ string assetPath = AssetDatabase . GetAssetPath ( document . visualTreeAsset ) ;
211+ if ( assetPath . NullOrEmpty ( ) ) throw new NotSupportedException ( ) ;
212+
213+ ( string folder , string relativeFolder ) = GetFolderAndRelativeFolder ( assetPath ) ;
214+ if ( folder . NullOrEmpty ( ) || relativeFolder . NullOrEmpty ( ) ) return ;
226215
227- if ( folder . NotNullOrEmpty ( ) )
228- await UpdateTitleAsync ( document , figma , title , folder , relativeFolder , systemCopyBuffer , downloadImages , fontDirs ) ;
216+ if ( folder . NotNullOrEmpty ( ) ) await UpdateTitleWithProgressAsync ( document , figma , title , folder , relativeFolder , systemCopyBuffer , downloadImages , fontDirs ) ;
229217 }
230- static async Task UpdateTitleAsync ( UIDocument document , Figma figma , string title , string folder , string relativeFolder , bool systemCopyBuffer , bool downloadImages , IReadOnlyCollection < string > fontDirs )
218+ static async Task UpdateTitleWithProgressAsync ( UIDocument document , Figma figma , string title , string folder , string relativeFolder , bool systemCopyBuffer , bool downloadImages , IReadOnlyCollection < string > fontDirs )
231219 {
232220 if ( ! Directory . Exists ( Path . Combine ( folder , "Images" ) ) ) Directory . CreateDirectory ( Path . Combine ( folder , "Images" ) ) ;
233221 if ( ! Directory . Exists ( Path . Combine ( folder , "Elements" ) ) ) Directory . CreateDirectory ( Path . Combine ( folder , "Elements" ) ) ;
@@ -500,7 +488,7 @@ void AddSvgImport(string id, string path)
500488 AssetDatabase . ImportAsset ( Path . Combine ( relativeFolder , "Images" ) , ImportAssetOptions . ImportRecursive | ImportAssetOptions . ForceSynchronousImport ) ;
501489
502490 Progress . SetDescription ( progress , "Importing png..." ) ;
503- foreach ( TextureImporter importer in importPng . Select ( relativePath => ( TextureImporter ) AssetImporter . GetAtPath ( relativePath ) ) )
491+ foreach ( TextureImporter importer in importPng . Select ( x => ( TextureImporter ) AssetImporter . GetAtPath ( x ) ) )
504492 {
505493 importer . npotScale = TextureImporterNPOTScale . None ;
506494 importer . mipmapEnabled = false ;
@@ -612,7 +600,7 @@ string GetFontPath(string name, string extension)
612600 #endregion
613601
614602 Progress . Report ( progress , 1 , 5 , "Downloading nodes" ) ;
615- Dictionary < string , string > headers = new ( ) { { "X-FIGMA-TOKEN" , PAT } } ;
603+ Dictionary < string , string > headers = new ( ) { { "X-FIGMA-TOKEN" , PersonalAccessToken } } ;
616604 string json = Encoding . UTF8 . GetString ( await $ "{ api } /files/{ title } ". HttpGetAsync ( headers , cancellationToken : token ) ) ;
617605 if ( systemCopyBuffer ) GUIUtility . systemCopyBuffer = json ;
618606
0 commit comments