fix: 完善 Android项目自动修改 Unity Project 路径
Signed-off-by: huyufei <yufei.hu@castbox.fm>dev
							parent
							
								
									6b02f619ce
								
							
						
					
					
						commit
						6d3e94cf2f
					
				|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: f0cef2fc19b2407381c7163d3a87ddec | ||||
| timeCreated: 1722257668 | ||||
|  | @ -0,0 +1,56 @@ | |||
| #if UNITY_ANDROID | ||||
| 
 | ||||
| namespace Guru.BuildTool | ||||
| { | ||||
|     using System.IO; | ||||
|     using UnityEditor.Android; | ||||
|     using UnityEngine; | ||||
|      | ||||
|     public class AndroidSettingsGradleFixer: IPostGenerateGradleAndroidProject | ||||
|     { | ||||
|         private const string SettingsGradleName= "settings.gradle"; | ||||
|         private const string K_LINE_UNITYPROJECT = "def unityProjectPath";  | ||||
|          | ||||
|         public int callbackOrder => 1; | ||||
| 
 | ||||
|         public void OnPostGenerateGradleAndroidProject(string buildPath) | ||||
|         { | ||||
|             FixSettingsInAndroidProject(buildPath); | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 设置项目中的 Settings 文件 | ||||
|         /// </summary> | ||||
|         /// <param name="buildPath"></param> | ||||
|         private void FixSettingsInAndroidProject(string buildPath) | ||||
|         { | ||||
|             var settingsPath = Path.GetFullPath($"{buildPath}/../{SettingsGradleName}"); | ||||
| 
 | ||||
|             if (File.Exists(settingsPath)) | ||||
|             { | ||||
|                 bool isDirty = false; | ||||
|                 var lines = File.ReadAllLines(settingsPath); | ||||
|                 string projectPath = Path.GetFullPath($"{Application.dataPath}/../").Replace("\\", "/"); // Unity project path | ||||
|                 for (int i = 0; i < lines.Length; i++) | ||||
|                 { | ||||
|                     if (lines[i].Contains(K_LINE_UNITYPROJECT)) | ||||
|                     { | ||||
|                         lines[i] = $"        def unityProjectPath = $/file:////{projectPath}/$.replace(\"\\\\\", \"/\")"; | ||||
|                         isDirty = true; | ||||
|                         break; | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 if (isDirty) | ||||
|                 { | ||||
|                     File.WriteAllLines(settingsPath, lines); | ||||
|                     Debug.Log($"[SDK] --- Fix Unity Project Path at:{settingsPath}"); | ||||
|                 } | ||||
|                  | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  | @ -0,0 +1,3 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 125df18a1af243018be105c54ab9bb5b | ||||
| timeCreated: 1722257677 | ||||
|  | @ -12,7 +12,7 @@ namespace Guru | |||
| 		private bool _isInitOnce; | ||||
| 		private Action _onInitComplete; | ||||
| 		 | ||||
| 		public void StartService(Action onInitComplete) | ||||
| 		public void StartService(Action onInitComplete = null) | ||||
| 		{ | ||||
| 			if(_isInitOnce) return; | ||||
| 			_isInitOnce = true; | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue