Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#nullable enable

using UnityEditor;

namespace Immutable.Audience.Editor
{
/// <summary>
/// Adds the mobile attribution toggles to <see cref="AudienceMobileBuildSettings"/>.
/// </summary>
/// <remarks>
/// The toggles read and write <see cref="MobileAttributionDefine"/> directly.
/// They are not fields on the asset, so an existing define set manually
/// (the old way) shows correctly the first time this asset is opened,
/// and nothing gets reset by updating the package.
/// </remarks>
[CustomEditor(typeof(AudienceMobileBuildSettings))]
internal sealed class AudienceMobileBuildSettingsEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Mobile Attribution", EditorStyles.boldLabel);
DrawToggle(BuildTargetGroup.iOS, "Enable for iOS");
DrawToggle(BuildTargetGroup.Android, "Enable for Android");
EditorGUILayout.Space();

DrawDefaultInspector();
}

private static void DrawToggle(BuildTargetGroup group, string label)
{
var enabled = MobileAttributionDefine.IsEnabled(group);
var toggled = EditorGUILayout.Toggle(label, enabled);
if (toggled != enabled) MobileAttributionDefine.SetEnabled(group, toggled);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions src/Packages/Audience/Editor/MobileAttributionDefine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#nullable enable

using System.Collections.Generic;
using UnityEditor;

namespace Immutable.Audience.Editor
{
/// <summary>
/// Reads and writes the <c>AUDIENCE_MOBILE_ATTRIBUTION</c> scripting
/// define on Player Settings.
/// </summary>
/// <remarks>
/// No state is cached or serialized here. Player Settings is the only
/// source of truth, so this always reflects whatever a studio has
/// already set, including manually, and nothing here can go stale.
/// </remarks>
internal static class MobileAttributionDefine
{
internal const string Symbol = "AUDIENCE_MOBILE_ATTRIBUTION";

internal static bool IsEnabled(BuildTargetGroup group) =>
Contains(PlayerSettings.GetScriptingDefineSymbolsForGroup(group));

internal static void SetEnabled(BuildTargetGroup group, bool enabled)
{
var current = PlayerSettings.GetScriptingDefineSymbolsForGroup(group);
PlayerSettings.SetScriptingDefineSymbolsForGroup(group, WithSymbol(current, enabled));
}

internal static bool Contains(string? defines)
{
foreach (var define in (defines ?? string.Empty).Split(';'))
{
if (define.Trim() == Symbol) return true;
}
return false;
}

internal static string WithSymbol(string? defines, bool enabled)
{
var result = new List<string>();
foreach (var define in (defines ?? string.Empty).Split(';', System.StringSplitOptions.RemoveEmptyEntries))
{
var trimmed = define.Trim();
if (trimmed.Length == 0 || trimmed == Symbol) continue;
result.Add(trimmed);
}
if (enabled) result.Add(Symbol);
return string.Join(";", result);
}
}
}
11 changes: 11 additions & 0 deletions src/Packages/Audience/Editor/MobileAttributionDefine.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 1 addition & 14 deletions src/Packages/Audience/Editor/iOSFrameworkPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static void OnPostProcessBuild(BuildTarget target, string pathToBuiltPr
if (target != BuildTarget.iOS) return;

#if UNITY_IOS
if (!AttributionDefineEnabled()) return;
if (!MobileAttributionDefine.IsEnabled(BuildTargetGroup.iOS)) return;

var pbxPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
if (!File.Exists(pbxPath))
Expand All @@ -75,18 +75,5 @@ internal static void OnPostProcessBuild(BuildTarget target, string pathToBuiltPr
pbx.WriteToFile(pbxPath);
#endif
}

// Reads the iOS-target define list specifically. The post-processor
// mutates iOS build output regardless of which target the editor is
// currently focused on.
private static bool AttributionDefineEnabled()
{
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS) ?? string.Empty;
foreach (var define in defines.Split(';'))
{
if (define.Trim() == iOSInfoPlistPostProcessor.AttributionDefine) return true;
}
return false;
}
}
}
23 changes: 4 additions & 19 deletions src/Packages/Audience/Editor/iOSInfoPlistPostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ namespace Immutable.Audience.Editor
internal static class iOSInfoPlistPostProcessor
{
internal const int CallbackOrder = 9050;
internal const string AttributionDefine = "AUDIENCE_MOBILE_ATTRIBUTION";

[PostProcessBuild(CallbackOrder)]
internal static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS) return;

#if UNITY_IOS
if (!AttributionDefineEnabled()) return;
if (!MobileAttributionDefine.IsEnabled(BuildTargetGroup.iOS)) return;

var plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
if (!File.Exists(plistPath))
Expand All @@ -68,12 +67,11 @@ internal static void OnPostProcessBuild(BuildTarget target, string pathToBuiltPr
[MenuItem("Tools/Immutable/Audience/Validate iOS Build Settings")]
private static void ValidateBuildSettings()
{
if (!AttributionDefineEnabled())
if (!MobileAttributionDefine.IsEnabled(BuildTargetGroup.iOS))
{
Debug.LogWarning(
"[ImmutableAudience] AUDIENCE_MOBILE_ATTRIBUTION scripting define is not set " +
"for the iOS player target. The post-processor will not modify Info.plist. " +
"Add the define under Player Settings → Other Settings → Scripting Define Symbols.");
"[ImmutableAudience] Mobile attribution is not enabled for iOS. The post-processor " +
"will not modify Info.plist. Enable it on the AudienceMobileBuildSettings asset.");
return;
}

Expand All @@ -92,19 +90,6 @@ private static void ValidateBuildSettings()
: string.Concat(System.Array.ConvertAll(ids, id => $" - {id}\n"))));
}

// Reads the iOS-target define list specifically. The post-processor
// mutates iOS build output regardless of which target the editor is
// currently focused on.
private static bool AttributionDefineEnabled()
{
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS) ?? string.Empty;
foreach (var define in defines.Split(';'))
{
if (define.Trim() == AttributionDefine) return true;
}
return false;
}

#if UNITY_IOS
internal static void ApplyTrackingUsageDescription(
PlistElementDict root,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static void OnPostProcessBuild(BuildTarget target, string pathToBuiltPr
if (target != BuildTarget.iOS) return;

#if UNITY_IOS
if (!AttributionDefineEnabled()) return;
if (!MobileAttributionDefine.IsEnabled(BuildTargetGroup.iOS)) return;

var builtManifestPath = FindBuiltManifest(pathToBuiltProject);
if (builtManifestPath == null)
Expand Down Expand Up @@ -119,15 +119,5 @@ v is PlistElementString s &&
purposes.AddString("NSPrivacyCollectedDataTypePurposeAnalytics");
}
#endif

private static bool AttributionDefineEnabled()
{
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS) ?? string.Empty;
foreach (var define in defines.Split(';'))
{
if (define.Trim() == iOSInfoPlistPostProcessor.AttributionDefine) return true;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#nullable enable

using NUnit.Framework;
using Immutable.Audience.Editor;

namespace Immutable.Audience.Editor.Tests
{
[TestFixture]
internal class MobileAttributionDefineTests
{
[Test]
public void Contains_NullOrEmpty_ReturnsFalse()
{
Assert.IsFalse(MobileAttributionDefine.Contains(null));
Assert.IsFalse(MobileAttributionDefine.Contains(""));
}

[Test]
public void Contains_SymbolAmongOthers_ReturnsTrue()
{
Assert.IsTrue(MobileAttributionDefine.Contains("FOO;AUDIENCE_MOBILE_ATTRIBUTION;BAR"));
}

[Test]
public void Contains_SymbolWithStrayWhitespace_ReturnsTrue()
{
Assert.IsTrue(MobileAttributionDefine.Contains("FOO; AUDIENCE_MOBILE_ATTRIBUTION ;BAR"));
}

[Test]
public void Contains_OtherDefinesOnly_ReturnsFalse()
{
Assert.IsFalse(MobileAttributionDefine.Contains("FOO;BAR"));
}

[Test]
public void WithSymbol_EnableOnEmpty_AddsOnlySymbol()
{
Assert.AreEqual("AUDIENCE_MOBILE_ATTRIBUTION", MobileAttributionDefine.WithSymbol(null, enabled: true));
}

[Test]
public void WithSymbol_EnableWithOthers_KeepsOthersAndAddsSymbol()
{
var result = MobileAttributionDefine.WithSymbol("FOO;BAR", enabled: true);

Assert.IsTrue(MobileAttributionDefine.Contains(result));
Assert.IsTrue(result.Contains("FOO"));
Assert.IsTrue(result.Contains("BAR"));
}

[Test]
public void WithSymbol_EnableWhenAlreadyPresent_DoesNotDuplicate()
{
var result = MobileAttributionDefine.WithSymbol("FOO;AUDIENCE_MOBILE_ATTRIBUTION;BAR", enabled: true);

var symbolCount = System.Array.FindAll(result.Split(';'), d => d == "AUDIENCE_MOBILE_ATTRIBUTION").Length;
Assert.AreEqual(1, symbolCount);
}

[Test]
public void WithSymbol_DisableRemovesSymbolOnly()
{
var result = MobileAttributionDefine.WithSymbol("FOO;AUDIENCE_MOBILE_ATTRIBUTION;BAR", enabled: false);

Assert.IsFalse(MobileAttributionDefine.Contains(result));
Assert.IsTrue(result.Contains("FOO"));
Assert.IsTrue(result.Contains("BAR"));
}

[Test]
public void WithSymbol_DisableWhenAbsent_LeavesOthersUnchanged()
{
var result = MobileAttributionDefine.WithSymbol("FOO;BAR", enabled: false);

Assert.AreEqual("FOO;BAR", result);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading