赞
踩
项目示例源代码:
https://github.com/sueleeyu/framework
1. unity版本2020.2.6f1,新建项目:
2.Hieraychy下Scene重命名为GeospatialScene,选择Window-Package Manager- Unity Register,依次下载:
XR Plugin Management 4.0.7
AR Foundation 4.1.10
ARCore XR Plugin 4.1.10
ARKit XR Plugin 4.1.10
3.如下:
4.删除原有摄像机, 点击Hierarchy的“+”,添加AR Session Origin和AR Session:
5.切换项目平台,File-Build Settings,先添加Scene,然后Platform下切换到Android:
6.Player Settings设置,点击Build Setting下的Player Settings:
Scripting Backend选择IL2CPP,这样Target Architectures可以勾选ARM64:
7.XR-Plugin Management:
8.打包设置:因ARCore Extensions需要build gradle 插件在4.0.1以上,打包时需要勾选下图选项:
与android工程的对应关系:
Main Gradle Template–> unityLibrary库的build.gradle文件
Launcher Gradle Template->launcher库的build.gradle文件
Base Gradle Template->android工程的build.gradle文件
Gradle Properties->android工程的gradle.properties文件
否则会出现打包错误:
9.测试打包APK
如果打包时间较长,
a.打开项目下
\Assets\Plugins\Android\baseProjectTemplate.gradle文件,更改为:
- // GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
-
-
-
- allprojects {
-
- buildscript {
-
- repositories {**ARTIFACTORYREPOSITORY**
-
- maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
-
- google()
-
- jcenter()
-
- }
-
-
-
- dependencies {
-
- // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
-
- // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
-
- // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
-
- // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
-
- classpath 'com.android.tools.build:gradle:4.2.2'
-
- **BUILD_SCRIPT_DEPS**
-
- }
-
- }
-
-
-
- repositories {**ARTIFACTORYREPOSITORY**
-
- maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
-
- google()
-
- jcenter()
-
- flatDir {
-
- dirs "${project(':unityLibrary').projectDir}/libs"
-
- }
-
- }
-
- }
-
- task clean(type: Delete) {
-
- delete rootProject.buildDir
-
- }

b.\Assets\Plugins\Android\mainTemplate.gradle文件,改为:
- //AAA Android Resolver Repos Start
-
- ([rootProject] + (rootProject.subprojects as List)).each { project ->
-
- project.repositories {
-
- def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
-
- maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
-
- maven {
-
- url "https://maven.aliyun.com/repository/google/" // Assets/GoogleMobileAds/Editor/GoogleMobileAdsDependencies.xml:7
-
- }
-
- mavenLocal()
-
- jcenter()
-
- mavenCentral()
-
- }
-
- }
-
- //AAA Android Resolver Repos End
-
- apply plugin: 'com.android.library'
-
- **APPLY_PLUGINS**
-
-
-
- dependencies {
-
- implementation fileTree(dir: 'libs', include: ['*.jar'])
-
- **DEPS**}
-
-
-
- android {
-
- compileSdkVersion **APIVERSION**
-
- buildToolsVersion '**BUILDTOOLS**'
-
-
-
- compileOptions {
-
- sourceCompatibility JavaVersion.VERSION_1_8
-
- targetCompatibility JavaVersion.VERSION_1_8
-
- }
-
-
-
- defaultConfig {
-
- minSdkVersion **MINSDKVERSION**
-
- targetSdkVersion **TARGETSDKVERSION**
-
- ndk {
-
- abiFilters **ABIFILTERS**
-
- }
-
- versionCode **VERSIONCODE**
-
- versionName '**VERSIONNAME**'
-
- consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD**
-
- }
-
-
-
- lintOptions {
-
- abortOnError false
-
- }
-
-
-
- aaptOptions {
-
- noCompress = ['.ress', '.resource', '.obb'] + unityStreamingAssets.tokenize(', ')
-
- ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
-
- }**PACKAGING_OPTIONS**
-
- }**REPOSITORIES**
-
- **IL_CPP_BUILD_SETUP**
-
- **SOURCE_BUILD_SETUP**
-
- **EXTERNAL_SOURCES**

如果首行是如下内容也要同时删除:
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
提示:如果我们修改了Unity生成的文件,比如build.gradle,比如UnityPlayerActivity.java等,那么可以删除这些文件中以下注释,来防止Unity覆盖该文件:
GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
c.打开Android Resolver设置:
d.取消覆盖mainTemplate.gradle的设置:
10.签名打包的APK:打开命令提示符-进入证书所在目录,把未签名的APK放到当前目录,更名orient.apk,命令行输入:
jarsigner -verbose -keystore user.keystore -signedjar sign.apk orient.apk 212
user.keystore为证书,如:F:\unity\sign\user.keystore
orient.apk为未签名APK,如:F:\unity\sign\orient.apk
sign.apk为签名后的APK
212为证书创建时设置的别名
11.查看SHA-1 指纹,命令行输入:
keytool -list -printcert -jarfile sign.apk
安装要求:
1.翻墙
2.ARCore扩展包需要ARFoundation4.1.5以上。
3.unity版本2020.2.6f1以上
google参考文档:
开始使用 ARCore Foundation for AR Foundation | Google Developers
1.导入ARCore扩展包:windows-package manager,点击 “+” 按钮,然后从下拉菜单中选择 Add package from git URL... 选项
2.复制以下网址到文本字段中,点击Add:
GitHub - google-ar/arcore-unity-extensions: Google ARCore Extensions for Unity's AR Foundation
安装后:
2.添加ARCore Extensions游戏对象:Hierarchy-“+”-XR-ARCore Extensions
3.关联游戏对象:拖动对象到相应字段
4.制作ARCoreExtensionsConfig(和ARCoreExtensionsCameraConfigFilter)素材资源: Project > Assets-右键创建Configurations文件夹,Configurations内右键点击 Create > XR > ARCore Extensions Config,点击后添加。
5.拖动关联素材资源:
6.在 Unity 中,转到 Edit > Project Settings > XR > ARCore Extensions。 Android Authentiation Strategy 下拉菜单中,选择 API Key
7.在Android API Key 字段中,粘贴从 Google Cloud Platform 获得的 API 密钥(下述),并勾选Optional Features 下的两个要使用的API的复选框:
授权完成。
1.Hierarchy右键-Create Empty,点击创建的组件,右侧Inspector命名UI。Hierarchy右键-Create Empty,点击创建的组件,右侧Inspector命名Game:
2.UI下新建Text,命名Log,设置:
1. Assets-新建Scripts文件夹,新建Logger.cs脚本文件:
- using System.Collections.Generic;
-
- using System.Text;
-
- using UnityEngine;
-
- using UnityEngine.UI;
-
-
-
- namespace FrameworkDesign.Example
-
- {
-
- public class Logger : MonoBehaviour
-
- {
-
- [SerializeField]
-
- Text m_LogText;
-
- public Text logText
-
- {
-
- get => m_LogText;
-
- set => m_LogText = value;
-
- }
-
-
-
- [SerializeField]
-
- int m_VisibleMessageCount = 40;
-
- public int visibleMessageCount
-
- {
-
- get => m_VisibleMessageCount;
-
- set => m_VisibleMessageCount = value;
-
- }
-
-
-
- int m_LastMessageCount;
-
-
-
- static List<string> s_Log = new List<string>();
-
-
-
- static StringBuilder m_StringBuilder = new StringBuilder();
-
-
-
- void Awake()
-
- {
-
- if (m_LogText == null)
-
- {
-
- m_LogText = GetComponent<Text>();
-
- }
-
-
-
- lock (s_Log)
-
- {
-
- s_Log?.Clear();
-
- }
-
-
-
- Log("Log console initialized.");
-
- }
-
-
-
- void Update()
-
- {
-
- lock (s_Log)
-
- {
-
- if (m_LastMessageCount != s_Log.Count)
-
- {
-
- m_StringBuilder.Clear();
-
- var startIndex = Mathf.Max(s_Log.Count - m_VisibleMessageCount, 0);
-
- for (int i = startIndex; i < s_Log.Count; ++i)
-
- {
-
- m_StringBuilder.Append($"{i:000}> {s_Log[i]}\n");
-
- }
-
-
-
- var text = m_StringBuilder.ToString();
-
-
-
- if (m_LogText)
-
- {
-
- m_LogText.text = text;
-
- }
-
- else
-
- {
-
- Debug.Log(text);
-
- }
-
- }
-
-
-
- m_LastMessageCount = s_Log.Count;
-
- }
-
- }
-
-
-
- public static void Log(string message)
-
- {
-
- lock (s_Log)
-
- {
-
- if (s_Log == null)
-
- s_Log = new List<string>();
-
-
-
- s_Log.Add(message);
-
- }
-
- }
-
- }
-
- }

2. 挂载Log,选择Log组件,将Logger.cs拖动添加为Component,关联Log(拖动Log到LogTest栏)
1. unity运行提示:Display 1 No cameras rendering
解决方法:Game视图-右键-取消第一个的勾选
1. Unity api:
Unity - Manual: Unity User Manual 2021.3 (LTS)
2. Geospatial 文档:
使用 ARCore Geospatial API 打造全球规模的沉浸式 AR 体验 | Google Developers
3.本项目示例源代码:
https://github.com/sueleeyu/framework
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。