1
- using System . Collections ;
1
+ using System ;
2
+ using System . IO ;
3
+ using System . Collections ;
2
4
using System . Diagnostics ;
5
+ using System . Text . RegularExpressions ;
3
6
4
7
using UnityEngine ;
5
8
using UnityEditor ;
6
9
using UnityEditor . Callbacks ;
7
10
8
11
public class AdjustEditor : MonoBehaviour
9
12
{
10
- static string iOSBuildPath = "" ;
11
13
static bool isEnabled = true ;
12
- static bool isAdjusted = false ;
14
+ static string iOSBuildPath = "" ;
13
15
14
16
[ PostProcessBuild ]
15
17
public static void OnPostprocessBuild ( BuildTarget target , string pathToBuiltProject )
@@ -31,84 +33,110 @@ public static void OnPostprocessBuild (BuildTarget target, string pathToBuiltPro
31
33
}
32
34
}
33
35
34
- [ MenuItem ( "Assets/Adjust/Fix AndroidManifest.xml" ) ]
36
+ [ MenuItem ( "Assets/Adjust/Fix AndroidManifest.xml" ) ]
35
37
static void FixAndroidManifest ( )
36
38
{
37
39
#if UNITY_ANDROID
38
40
var exitCode = RunPostBuildScript ( preBuild : true ) ;
39
41
40
42
if ( exitCode == 1 ) {
41
43
EditorUtility . DisplayDialog ( "Adjust" ,
42
- string . Format ( "AndroidManifest.xml changed or created at {0}/Plugins/Android/ ." , Application . dataPath ) ,
44
+ string . Format ( "AndroidManifest.xml changed or created at {0}/Plugins/Android/ ." , Application . dataPath ) ,
43
45
"OK" ) ;
44
46
} else if ( exitCode == 0 ) {
45
- EditorUtility . DisplayDialog ( "Adjust" ,
46
- "AndroidManifest.xml did not needed to be changed." ,
47
- "OK" ) ;
47
+ EditorUtility . DisplayDialog ( "Adjust" , "AndroidManifest.xml did not needed to be changed." , "OK" ) ;
48
48
} else {
49
- EditorUtility . DisplayDialog ( "Adjust" ,
50
- GenerateErrorScriptMessage ( exitCode ) ,
51
- "OK" ) ;
49
+ EditorUtility . DisplayDialog ( "Adjust" , GenerateErrorScriptMessage ( exitCode ) , "OK" ) ;
52
50
53
51
}
54
52
#else
55
53
EditorUtility . DisplayDialog ( "Adjust" , "Option only valid for the Android platform." , "OK" ) ;
56
54
#endif
57
55
}
58
56
59
- [ MenuItem ( "Assets/Adjust/Set iOS build path" ) ]
57
+ [ MenuItem ( "Assets/Adjust/Set iOS build path" ) ]
60
58
static void SetiOSBuildPath ( )
61
59
{
62
60
#if UNITY_IOS
63
- AdjustEditor . iOSBuildPath = EditorUtility . OpenFolderPanel (
61
+ AdjustEditor . iOSBuildPath = EditorUtility . OpenFolderPanel (
64
62
title : "iOs build path" ,
65
- folder : EditorUserBuildSettings . GetBuildLocation ( BuildTarget . iOS ) ,
63
+ folder : EditorUserBuildSettings . GetBuildLocation ( BuildTarget . iOS ) ,
66
64
defaultName : "" ) ;
67
65
68
66
if ( AdjustEditor . iOSBuildPath == "" ) {
69
- UnityEngine . Debug . Log ( "iOS build path reset to default path" ) ;
67
+ UnityEngine . Debug . Log ( "iOS build path reset to default path" ) ;
70
68
} else {
71
- UnityEngine . Debug . Log ( string . Format ( "iOS build path: {0}" , AdjustEditor . iOSBuildPath ) ) ;
69
+ UnityEngine . Debug . Log ( string . Format ( "iOS build path: {0}" , AdjustEditor . iOSBuildPath ) ) ;
72
70
}
73
71
#else
74
72
EditorUtility . DisplayDialog ( "Adjust" , "Option only valid for the Android platform." , "OK" ) ;
75
73
#endif
76
74
}
77
75
78
- [ MenuItem ( "Assets/Adjust/Change post processing status" ) ]
76
+ [ MenuItem ( "Assets/Adjust/Change post processing status" ) ]
79
77
static void ChangePostProcessingStatus ( )
80
78
{
81
79
isEnabled = ! isEnabled ;
82
80
83
- EditorUtility . DisplayDialog ( "Adjust" ,
84
- "The post processing for adjust is now " +
85
- ( isEnabled ? "enabled." : "disabled." ) ,
86
- "OK" ) ;
81
+ EditorUtility . DisplayDialog ( "Adjust" , "The post processing for adjust is now " +
82
+ ( isEnabled ? "enabled." : "disabled." ) , "OK" ) ;
87
83
}
88
84
89
85
static int RunPostBuildScript ( bool preBuild , string pathToBuiltProject = "" )
90
86
{
91
- string pathToScript = null ;
87
+ string resultContent ;
92
88
string arguments = null ;
89
+ string pathToScript = null ;
93
90
94
- #if UNITY_ANDROID
91
+ string filePath = System . IO . Path . Combine ( Environment . CurrentDirectory ,
92
+ @"Assets/Editor/PostprocessBuildPlayer_AdjustPostBuildiOS.py" ) ;
93
+
94
+ // Check if Unity is running on Windows operating system.
95
+ // If yes - fix line endings in python scripts.
96
+ if ( Application . platform == RuntimePlatform . WindowsEditor ) {
97
+ UnityEngine . Debug . Log ( "Windows platform" ) ;
98
+
99
+ using ( System . IO . StreamReader streamReader = new System . IO . StreamReader ( filePath ) ) {
100
+ string fileContent = streamReader . ReadToEnd ( ) ;
101
+ resultContent = Regex . Replace ( fileContent , @"\r\n|\n\r|\n|\r" , "\r \n " ) ;
102
+ }
103
+
104
+ if ( File . Exists ( filePath ) ) {
105
+ File . WriteAllText ( filePath , resultContent ) ;
106
+ }
107
+ } else {
108
+ UnityEngine . Debug . Log ( "Unix platform" ) ;
109
+
110
+ using ( System . IO . StreamReader streamReader = new System . IO . StreamReader ( filePath ) ) {
111
+ string replaceWith = "\n " ;
112
+ string fileContent = streamReader . ReadToEnd ( ) ;
113
+
114
+ resultContent = fileContent . Replace ( "\r \n " , replaceWith ) ;
115
+ }
116
+
117
+ if ( File . Exists ( filePath ) ) {
118
+ File . WriteAllText ( filePath , resultContent ) ;
119
+ }
120
+ }
121
+
122
+ #if UNITY_ANDROID
95
123
pathToScript = "/Editor/PostprocessBuildPlayer_AdjustPostBuildAndroid.py" ;
96
124
arguments = "\" " + Application . dataPath + "\" " ;
97
125
98
126
if ( preBuild ) {
99
127
arguments = "--pre-build " + arguments ;
100
128
}
101
- #elif UNITY_IOS
129
+ #elif UNITY_IOS
102
130
pathToScript = "/Editor/PostprocessBuildPlayer_AdjustPostBuildiOS.py" ;
103
131
104
132
if ( AdjustEditor . iOSBuildPath == "" ) {
105
133
arguments = "\" " + pathToBuiltProject + "\" " ;
106
134
} else {
107
135
arguments = "\" " + AdjustEditor . iOSBuildPath + "\" " ;
108
136
}
109
- #else
137
+ #else
110
138
return - 1 ;
111
- #endif
139
+ #endif
112
140
113
141
Process proc = new Process ( ) ;
114
142
proc . EnableRaisingEvents = false ;
@@ -122,26 +150,26 @@ static int RunPostBuildScript (bool preBuild, string pathToBuiltProject = "")
122
150
123
151
static string GenerateErrorScriptMessage ( int exitCode )
124
152
{
125
- #if UNITY_ANDROID
153
+ #if UNITY_ANDROID
126
154
if ( exitCode == 1 ) {
127
155
return "The AndroidManifest.xml file was only changed or created after building the package. " +
128
156
"PLease build again the Android Unity package so it can use the new file" ;
129
157
}
130
- #endif
158
+ #endif
131
159
132
160
if ( exitCode != 0 ) {
133
161
var message = "Build script exited with error." +
134
- " Please check the Adjust log file for more information at {0}" ;
162
+ " Please check the Adjust log file for more information at {0}" ;
135
163
string projectPath = Application . dataPath . Substring ( 0 , Application . dataPath . Length - 7 ) ;
136
164
string logFile = null ;
137
165
138
- #if UNITY_ANDROID
166
+ #if UNITY_ANDROID
139
167
logFile = projectPath + "/AdjustPostBuildAndroidLog.txt" ;
140
- #elif UNITY_IOS
168
+ #elif UNITY_IOS
141
169
logFile = projectPath + "/AdjustPostBuildiOSLog.txt" ;
142
- #else
170
+ #else
143
171
return null ;
144
- #endif
172
+ #endif
145
173
return string . Format ( message , logFile ) ;
146
174
}
147
175
0 commit comments