unity18 19现有的很方便的sdk接入自动依赖下载,其中也有一些坑一一记录下来

公司老项目sdk接入实在是乱的不行,由于现在开发休闲游戏,每个项目都要使用sdk,于是就自己接了一套以便后续使用。

安卓

友盟导入就可以用
facebook需要点Facebook->设置app id
Ironsource -> admob 需要在manifest里面添加id,官网文档有
ironsource导入后,点IronSource->Integration Manager->选择需要的广告点Install(如加载不出来需要开全局)
	有部分广告需要把打包方式改成gradle,所以建议一直是gradle
	当改成gradle的时候,facebook和友盟会报错,需要勾选上 publishing setting->custom prograd file
	在其中加入->
-keep class com.umeng.** { *; } 
-keep class com.facebook.** { *; } 
-keepattributes Signature

自动解决依赖Assets->External Denpendency Manager->Android Resolver->Resolver自动下载
注意需要电脑的jdk版本需要是1.8  我的是java version "1.8.0_271"  最新的1.8都可以

IOS

友盟有点问题

unity pangle接广告 unity游戏广告接入_Android

这个问题好像在后边自动pod install后不出现了,如果有的话可以这样解决

"_sqlite3_errmsg", 等等sqlite3找不到需要在UnityFramework的Other Linker Flags加入-lsqlite3

其他正常导入,

注意!点Assets->External Denpendency Manager->IOS Resolver->Setting->Cocoapods Integration改成None,否则会自动pod install,由于默认源是git,会卡住很久

有需要可以更改pod为清华源,加快下载

$ cd ~/.cocoapods/repos 
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master

然后pos install推荐使用update install

pod install --repo-update

如果遇到FBSDK或者什么框架pod安装不上说超时,需要将terminal

打开终端,直接执行:(执行后,只对当前终端起作用;重启终端后,默认失效;)

$ export http_proxy=socks5://127.0.0.1:1086 # 配置http 代理访问
 
$ export https_proxy=socks5://127.0.0.1:1086 # 配置https 代理访问
 
$ export all_proxy=socks5://127.0.0.1:1086 # 配置http和https访问
 
$ unset http_proxy # 取消http 代理访问
 
$ unset https_proxy # 取消https 代理访问

端口需要和shadowSocks中对应

unity pangle接广告 unity游戏广告接入_unity pangle接广告_02

附上我现在用的打包脚本

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;

public enum CommandArgsName
{
   up_version,
   platform,
   project_name,
}

public static class Build
{
   private static string KeyStorePath => Directory.GetFiles(Path.Combine(Application.dataPath, ".."), "*.keystore")[0];

   private static string GetOutPath(BuildTarget buildTarget)
   {
      if(buildTarget == BuildTarget.Android)
         return Path.Combine(Application.dataPath, $"../../{Path.GetFileNameWithoutExtension(KeyStorePath)}.apk");
      return Path.Combine(Application.dataPath, $"../../{Path.GetFileNameWithoutExtension(KeyStorePath)}");
   }
   
   private static string KeyStorePwd => Path.Combine(Application.dataPath, "../keystore_password.txt");
   
   public static void EditorBuild(bool upVersion, BuildTarget buildTarget)
   {
      EditorUserBuildSettings.SwitchActiveBuildTarget(BuildPipeline.GetBuildTargetGroup(buildTarget), buildTarget);
      if (upVersion)
      {
         UpVersion(buildTarget);
      }
      PlayerSettings.keystorePass = File.ReadAllText(KeyStorePwd);
      PlayerSettings.keyaliasPass = File.ReadAllText(KeyStorePwd);
      PlayerSettings.Android.keystoreName = KeyStorePath;
      PlayerSettings.Android.keyaliasName = Path.GetFileNameWithoutExtension(KeyStorePath);
      EditorUserBuildSettings.androidDebugMinification = AndroidMinification.Gradle;
      EditorUserBuildSettings.androidReleaseMinification = AndroidMinification.Gradle;
      BuildPipeline.BuildPlayer(GetBuildScenes(), GetOutPath(buildTarget), buildTarget, BuildOptions.None);
      
      OpenTargetFolder(buildTarget);
   }


   private static void OpenTargetFolder(BuildTarget buildTarget)
   {
      if (Application.platform == RuntimePlatform.WindowsEditor)
      {
         System.Diagnostics.Process.Start("explorer.exe", Path.GetDirectoryName(GetOutPath(buildTarget)));
      }
      else if (Application.platform == RuntimePlatform.OSXEditor)
      {
         System.Diagnostics.Process.Start("open", Path.GetDirectoryName(GetOutPath(buildTarget)));
      }
   }
   
   public static void JenkinsBuild()
   {
      BuildTarget buildTarget = (BuildTarget)Enum.Parse(typeof(BuildTarget), GetEnvironmentVariable(CommandArgsName.platform));
      bool upVersion = Boolean.Parse(GetEnvironmentVariable(CommandArgsName.up_version));
      if (buildTarget == BuildTarget.Android && upVersion)
      {
         UpVersion(buildTarget);
      }
      BuildPipeline.BuildPlayer(GetBuildScenes(), GetOutPath(buildTarget), BuildTarget.Android, BuildOptions.None);
   }

   private static void UpVersion(BuildTarget buildTarget)
   {
      var bundleVersion = PlayerSettings.bundleVersion;
      string[] strs = bundleVersion.Split('.');
      var lastVersion = int.Parse(strs[strs.Length - 1]) + 1;
      strs[strs.Length - 1] = lastVersion.ToString();
      PlayerSettings.bundleVersion = string.Join(".", strs);
      if (buildTarget == BuildTarget.Android)
         PlayerSettings.Android.bundleVersionCode += 1;
      else if (buildTarget == BuildTarget.iOS)
         PlayerSettings.iOS.buildNumber = (int.Parse(PlayerSettings.iOS.buildNumber) + 1).ToString();
   }
   
   static string[] GetBuildScenes()
   {
      List<string> names = new List<string>();
      foreach(EditorBuildSettingsScene e in EditorBuildSettings.scenes)
      {
         if(e==null)
            continue;
         if(e.enabled)
            names.Add(e.path);
      }
      return names.ToArray();
   }
   
   static string GetEnvironmentVariable(CommandArgsName commandArgsName)
   {
      return Environment.GetEnvironmentVariable(commandArgsName.ToString());
   }

   [PostProcessBuild(1)]
   public static void OnPostProcessBuild(BuildTarget target, string path)
   {
      if (target == BuildTarget.iOS)
      {
         // Read.
         string projectPath = PBXProject.GetPBXProjectPath(path);
         PBXProject project = new PBXProject();
         project.ReadFromString(File.ReadAllText(projectPath));
         string targetGUID = project.GetUnityFrameworkTargetGuid();

         AddFrameworks(project, targetGUID);
         string plistPath = Path.Combine(path, "Info.plist");
         PlistDocument plist = new PlistDocument();
         plist.ReadFromString(File.ReadAllText(plistPath));
       
         // Get root
         PlistElementDict rootDict = plist.root;
       
         // Change value of CFBundleVersion in Xcode plist
         var buildKey = "GADApplicationIdentifier";
         rootDict.SetString(buildKey, ConstData.AdmobIosKey);
         var capKey = "UIRequiredDeviceCapabilities";
         if (rootDict.values.ContainsKey(capKey))
            rootDict.values.Remove(capKey);       
         // Write to file
         File.WriteAllText(plistPath, plist.WriteToString());

         // Write.
         File.WriteAllText(projectPath, project.WriteToString());

         System.Diagnostics.Process.Start("pod", $"install --project-directory={path}");
      }
   }

   static void AddFrameworks(PBXProject project, string targetGUID)
   {
      // Frameworks 

      project.AddFrameworkToProject(targetGUID, "libz.dylib", false);
      project.AddFrameworkToProject(targetGUID, "libsqlite3.tbd", false);
      project.AddFrameworkToProject(targetGUID, "CoreTelephony.framework", false);
   }
}
#endif

希望对各位sdk接入人有帮助

错误合集:

Q:

firebase - Unity Firebase加载失败:“无法从应用程序的资源读取Firebase选项。

A:

unity pangle接广告 unity游戏广告接入_Android_03

注意这两个地方有没有自动生成的xml文件

Q:

发布Android出错unexpected element <queries> found in <manifest>

A:
由于Android11新标签不识别问题导致,修改unity安装路径Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates下的baseProjectTemplate.gradle文件
classpath 'com.android.tools.build:gradle:3.4.0' 

修改为
classpath 'com.android.tools.build:gradle:3.4.3'

Q:

D8: Cannot fit requested classes in a single dex file (# methods: 71752 > 65536)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':launcher:transformClassesAndResourcesWithR8ForRelease'.
> com.android.tools.r8.CompilationFailedException: Compilation failed to complete

or :

The number of method references in a .dex file cannot exceed 64k API 17

A:

Player Settings中将支持的最低Android API级别设定为21及以上。Android API Level 21及更高版本支持从APK文件中加载多个DEX文件,因此不会受64K的限制。

Unity打包apk报错: Cannot fit requested classes in a single dex file (# methods: 73376 > 65536)的解决办法

Q: Unity发布安卓9.0及以上版本不能访问http请求

A: 在AndroidManifest.xml的application中添加如下一条属性即可:android:usesCleartextTraffic="true"