22using System . Collections ;
33using System . Collections . Generic ;
44using System . IO ;
5- using System . Linq ;
65using System . Reflection ;
6+ using System . Xml ;
77using UnityEditor ;
8- using UnityEditor . PackageManager . UI ;
98using UnityEditor . SceneManagement ;
109using UnityEngine ;
1110
@@ -14,14 +13,18 @@ namespace UnityLauncherProTools
1413 public class InitializeProject : EditorWindow
1514 {
1615 // settings
17- static string [ ] folders = new string [ ] { "Fonts" , "Materials" , "Models" , "Prefabs" , "Scenes" , "Scripts" , "Shaders" , "Sounds" , "Textures" } ;
16+ static string [ ] folders = new string [ ] { "Fonts" , "Materials" , "Models" , "Plugins" , "Prefabs" , "Scenes" , "Scripts" , "Shaders" , "Sounds" , "Textures" } ;
17+
18+ static Dictionary < string , string > addPackages = new Dictionary < string , string > ( ) { { "com.unity.ide.visualstudio" , "2.0.17" } } ;
19+ static string [ ] blackListedPackages = new string [ ] { "com.unity.modules.unityanalytics" , "com.unity.modules.director" , "com.unity.collab-proxy" , "com.unity.ide.rider" , "com.unity.ide.vscode" , "com.unity.test-framework" , "com.unity.timeline" } ;
1820
1921 static InitializeProject window ;
2022 static string assetsFolder ;
2123 static bool deleteFile = true ;
2224
2325 // settings
2426 static bool createFolders = true ;
27+ static bool updatePackages = true ;
2528
2629 [ MenuItem ( "Tools/UnityLibrary/Initialize Project" ) ]
2730 public static void InitManually ( )
@@ -44,6 +47,7 @@ void OnGUI()
4447 GUILayout . Space ( 10 ) ;
4548
4649 Checkbox ( "Create Folders" , ref createFolders ) ;
50+ Checkbox ( "updatePackages" , ref updatePackages ) ;
4751
4852 GUILayout . Space ( 10 ) ;
4953 if ( GUILayout . Button ( "Setup Project" , GUILayout . Height ( 64 ) ) ) SetupProject ( ) ;
@@ -58,30 +62,44 @@ static void SetupProject()
5862
5963 if ( createFolders ) CreateFolders ( ) ;
6064
61- // TODO set these somewhere
65+ // TODO set these somewhere globally?
6266 PlayerSettings . companyName = "Company" ;
6367 PlayerSettings . productName = "Project" ;
6468
6569 PlayerSettings . colorSpace = ColorSpace . Linear ;
6670
6771 // save scene
6872 var scenePath = "Assets/Scenes/Main.unity" ;
73+ if ( Directory . Exists ( scenePath ) == false )
74+ {
75+ Directory . CreateDirectory ( "Assets/Scenes" ) ;
76+ }
6977 EditorSceneManager . SaveScene ( EditorSceneManager . GetActiveScene ( ) , scenePath ) ;
7078
7179 // add scene to build settings
7280 List < EditorBuildSettingsScene > editorBuildSettingsScenes = new List < EditorBuildSettingsScene > ( ) ;
7381 if ( ! string . IsNullOrEmpty ( scenePath ) ) editorBuildSettingsScenes . Add ( new EditorBuildSettingsScene ( scenePath , true ) ) ;
7482 EditorBuildSettings . scenes = editorBuildSettingsScenes . ToArray ( ) ;
7583
76- // TODO 2d/3d mode for editor
77- // TODO remove extra packages
78- // TODO setup light settings
79- // TODO adjust mainscene: camera pos, skybox off?
84+ // TODO 2d/3d mode for editor?
85+
86+ UpdatePackages ( ) ;
87+
88+ // skybox off from lighting settings
89+ RenderSettings . skybox = null ;
90+
91+ // skybox off from camera
92+ Camera . main . clearFlags = CameraClearFlags . SolidColor ;
93+ // TODO set background color?
94+
95+ // reset camera pos
8096 Camera . main . transform . position = Vector3 . zero ;
8197
8298 // disable editor camera easing and acceleration
99+ #if UNITY_2019_1_OR_NEWER
83100 SceneView . lastActiveSceneView . cameraSettings . easingEnabled = false ;
84101 SceneView . lastActiveSceneView . cameraSettings . accelerationEnabled = false ;
102+ #endif
85103
86104 // GizmoUtility in 2022.1
87105 //GizmoUtility.SetGizmoEnabled(GizmoType.Move, true);
@@ -109,8 +127,9 @@ static void SetupProject()
109127 // self destruct this editor script file
110128 if ( deleteFile == true )
111129 {
112- // FIXME file is deleted, if reimport some script, while editorwindow is open
130+ // FIXME in editor: file is deleted, if re-import some script, while editorwindow is open (resets deletefile var?)
113131 var scriptPath = Path . Combine ( assetsFolder , "Editor/InitializeProject.cs" ) ;
132+ Debug . Log ( "Deleting init script: " + scriptPath ) ;
114133 if ( File . Exists ( scriptPath ) ) File . Delete ( scriptPath ) ;
115134 if ( File . Exists ( scriptPath + ".meta" ) ) File . Delete ( scriptPath + ".meta" ) ;
116135 }
@@ -124,6 +143,80 @@ static void SetupProject()
124143 }
125144
126145
146+ static void UpdatePackages ( )
147+ {
148+ // check if packages.json exists
149+ var packagesPath = Path . Combine ( assetsFolder , "../Packages/manifest.json" ) ;
150+ if ( ! File . Exists ( packagesPath ) ) return ;
151+
152+ var json = File . ReadAllText ( packagesPath ) ;
153+
154+ // NOTE this seems to work in 2020.3 and later?
155+ var jsonConvertType = Type . GetType ( "Newtonsoft.Json.JsonConvert, Newtonsoft.Json" ) ;
156+ if ( jsonConvertType != null )
157+ {
158+ jsonConvertType = Assembly . Load ( "Newtonsoft.Json" ) . GetType ( "Newtonsoft.Json.JsonConvert" ) ;
159+ }
160+ IJsonSerializer jsonSerializer ;
161+ if ( jsonConvertType != null )
162+ {
163+ jsonSerializer = new NewtonsoftJsonSerializer ( jsonConvertType ) ;
164+ }
165+ else
166+ {
167+ jsonSerializer = new DefaultJsonSerializer ( ) ;
168+ }
169+
170+ // do we have real newtonsoft
171+ Type type = Type . GetType ( "Newtonsoft.Json.JsonConvert, Newtonsoft.Json" ) ;
172+ if ( type != null )
173+ {
174+ //Debug.Log("We have Newtonsoft.Json");
175+ var fromJson = jsonSerializer . Deserialize < DependenciesManifest > ( json ) ;
176+
177+ for ( int i = fromJson . dependencies . Count ; i > - 1 ; i -- )
178+ {
179+ for ( int k = 0 ; k < blackListedPackages . Length ; k ++ )
180+ {
181+ if ( fromJson . dependencies . ContainsKey ( blackListedPackages [ k ] ) )
182+ {
183+ fromJson . dependencies . Remove ( blackListedPackages [ k ] ) ;
184+ //Debug.Log("Removed " + blackListedPackages[k]);
185+ }
186+ }
187+ }
188+
189+ // add wanted packages, if missing
190+ foreach ( KeyValuePair < string , string > item in addPackages )
191+ {
192+ // TODO check if want to increase version number?
193+ if ( fromJson . dependencies . ContainsKey ( item . Key ) == false )
194+ {
195+ fromJson . dependencies . Add ( item . Key , item . Value ) ;
196+ Debug . Log ( "Added " + item . Key ) ;
197+ }
198+ else
199+ {
200+ //Debug.Log("Already contains " + item.Key);
201+ }
202+ }
203+
204+ // TODO add pretty print
205+ var toJson = jsonSerializer . Serialize ( fromJson ) ;
206+ // FIXME temporary pretty print, by adding new lines and tabs
207+ toJson = toJson . Replace ( "," , ",\n " ) ;
208+ toJson = toJson . Replace ( "{" , "{\n " ) ;
209+ toJson = toJson . Replace ( "}" , "\n }" ) ;
210+ toJson = toJson . Replace ( "\" dependencies" , "\t \" dependencies" ) ;
211+ toJson = toJson . Replace ( "\" com." , "\t \t \" com." ) ;
212+ //Debug.Log(toJson);
213+ File . WriteAllText ( packagesPath , toJson ) ;
214+ }
215+ else
216+ {
217+ Debug . Log ( "Newtonsoft.Json is not available, cannot remove packages.." ) ;
218+ }
219+ }
127220
128221 // toggle with clickable label text
129222 static void Checkbox ( string label , ref bool value )
@@ -139,11 +232,58 @@ static void CreateFolders()
139232 // create each folder if it doesnt exists
140233 foreach ( string folder in folders )
141234 {
142- if ( ! Directory . Exists ( assetsFolder + "/ " + folder ) )
235+ if ( ! Directory . Exists ( assetsFolder + " / " + folder ) )
143236 {
144237 Directory . CreateDirectory ( assetsFolder + "/" + folder ) ;
145238 }
146239 }
147240 }
148241 }
242+
243+ // manifest.json
244+ public class DependenciesManifest
245+ {
246+ public Dictionary < string , string > dependencies { get ; set ; }
247+ }
248+
249+ public interface IJsonSerializer
250+ {
251+ string Serialize ( object obj ) ;
252+ T Deserialize < T > ( string json ) ;
253+ }
254+
255+ public class DefaultJsonSerializer : IJsonSerializer
256+ {
257+ public string Serialize ( object obj )
258+ {
259+ return "default serializer" ;
260+ }
261+
262+ public T Deserialize < T > ( string json )
263+ {
264+ return default ( T ) ;
265+ }
266+ }
267+
268+ public class NewtonsoftJsonSerializer : IJsonSerializer
269+ {
270+ private readonly Type jsonConvertType ;
271+
272+ public NewtonsoftJsonSerializer ( Type jsonConvertType )
273+ {
274+ this . jsonConvertType = jsonConvertType ;
275+ }
276+
277+ public string Serialize ( object obj )
278+ {
279+ var serializeMethod = jsonConvertType . GetMethod ( "SerializeObject" , new Type [ ] { typeof ( object ) } ) ;
280+ return ( string ) serializeMethod . Invoke ( null , new object [ ] { obj } ) ;
281+ }
282+
283+ public T Deserialize < T > ( string json )
284+ {
285+ var deserializeMethod = jsonConvertType . GetMethod ( "DeserializeObject" , new Type [ ] { typeof ( string ) , typeof ( Type ) } ) ;
286+ return ( T ) deserializeMethod . Invoke ( null , new object [ ] { json , typeof ( T ) } ) ;
287+ }
288+ }
149289}
0 commit comments