unity 导入 android studio,互传消息

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

参考链接

提示:测试过后最详细能用的是第四条,具体过程不写了,会提供下第四条的源码(https://download.csdn.net/download/qq_36930286/88802385),

因为是第一次用androidstudio,也会介绍一下使用中遇到的问题和解决方案链接(链接中传递消息的方式是UnitySendMessage,但是实际测试中,我个人使用的是一个场景一个小游戏的方式,这个传递是场景打开后才会获取到消息,所以最后用的是json文件的方式,也会记录一下)

 一、一、07、Android使用UnityPlayer.UnitySendMessage发消息给Unity - 简书

二、【Unity3D】Android Studio 工程中使用 Java 代码调用 Unity 的 C# 脚本 ( Java 中调用 UnityPlayer#UnitySendMessage 方法 )_unityplayer.unitysendmessage-CSDN博客三、Unity项目导入Android studio unity 导入 android studio_laojean的技术博客_51CTO博客

四、unity android 流程 android unity3d教程_ghpsyn的技术博客_51CTO博客

一、基础信息

gradle官网链接:https://services.gradle.org/distributions/

android studio安装: 最好不要安装最新版本
https://developer.android.google.cn/studio/archive?hl=zh-cn

 使用的unity版本:

android studio版本:

二、使用步骤

1.跳转场景卡半天SceneManager.LoadScene 的时候 资源问题

build.gradle下

aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb', '.unityexp'] + unityStreamingAssets.tokenize(', ')
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

2.使用json传参、发布消息

在源代码的

MainActivity.java中json字符串写入文件:
protected void writeJsonToFile(Context context) {
        File file = new File(context.getExternalFilesDir(null), "data.json");
        try (FileOutputStream fos = new FileOutputStream(file);
             OutputStreamWriter osw = new OutputStreamWriter(fos)) {
            //osw.write(jsonObject.toString());
            osw.write(tokenStr);

            osw.flush();
            System.out.println( "JSON written to file successfully."+file.getAbsolutePath());
        } catch (Exception e) {
            System.out.println( "Error writing JSON to file"+e);
        }
    }

调用的时候 

writeJsonToFile(this);

然后unity 可以在Awake方法中读取json字符串

在string path = Path.Combine(Application.persistentDataPath, "data.json");这个路径下可以不显示初始场景直接跳转指定场景

3.遇到的其他问题

a.问题:Android NDK location should not contain whitespace...解决方法
https://blog.csdn.net/gengkui9897/article/details/86349520
b.Installed Build Tools revision 31-33.x.x is corrupted.Remove and install again using the SDK Manager
https://blog.csdn.net/crazestone0614/article/details/128174736
c.Task list not built during sync
https://blog.csdn.net/zengkai19900512/article/details/129026228
d.【Android studio错误处理】Can‘t determine type for tag ‘<macro name=“m3_comp_assist_chip_container_shape“>
https://blog.csdn.net/qq_44921056/article/details/131633953
e.为你理清Android项目Gradle配置,解决常见编译问题
https://blog.csdn.net/totomiz/article/details/130878104

f.Unable to tunnel through proxy. Proxy returns “HTTP/1.1 400 Bad Request“

https://blog.csdn.net/qq_42015021/article/details/124583099

g.https://blog.csdn.net/qq_34988204/article/details/134481010
【Android】Multiple Gradle daemons might be spawned because the Gradle JDK and JAVA_HOME locations...

h.connect time out

https://blog.csdn.net/bbkky/article/details/134361554
i.Connection reset
https://blog.csdn.net/ouyang_peng/article/details/89255978

j.Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle.或是Could not install Gradle distribution from '  
这两个问题连着加速器通过

k.Could not get unknown property 'unityStreamingAssets' for object of type com.android.build.gradle.internal.dsl.AaptOptions.
https://stackoverflow.com/questions/66080360/gradle-build-error-in-unity-with-error-could-not-get-unknown-property-unitystre
https://www.jianshu.com/p/072b0a1e9438(详细)

l.【错误记录】约束布局报错 ( Missing Constraints in ConstraintLayout. This view is not constrained. It only has )
https://blog.csdn.net/shulianghan/article/details/129459934
m.Android studio button has no onclick option
https://stackoverflow.com/questions/58220536/android-studio-button-has-no-onclick-option
n.Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
   > Could not resolve project :unityLibrary.
     Required by:
         project :app
      > No matching configuration of project :unityLibrary was found. The consumer was configured to find an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' but:
          - None of the consumable configurations have attributes.(问题未解决)

o.Android Studio is using the following JDK location when running Gradle:
            E:\dwj\soft\Android Studio\jre
            Using different JDK locations on different processes might cause Gradle to
            spawn multiple daemons, for example, by executing Gradle tasks from a terminal
            while using Android Studio.

Project Structure--sdklocation--jdklocation  看看自己的JAVA_HOME

p.Session 'app': Installation did not succeed.
            The application could not be installed: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
        https://blog.csdn.net/dodod2012/article/details/133172928    
找个android11及以下的手机就可以

q.Android Studio查看Minimum SDK
Project Structure--module--compilesdk version
r.TextMate bundle load error: Bundle yaml can't be registered
https://bltang.cc/7369.html
s.Unable to start activity ComponentInfo{/com.unity3d.player.UnityPlayerActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
https://www.jianshu.com/p/6ce4199b3f8d
t.一启动就打开unity
https://blog.51cto.com/u_14256/7557195(把unityLibrary模块下的AndroidManifest.xml文件里的程序启动入口注释掉(启动应该写在app模块的MainActivity里))
u.Android StudioExecution failed for task ':app:checkDebugAarMetadata'.
https://blog.csdn.net/EliasChang/article/details/134692120
build.gradle(:app)
dependencies {
    implementation project(':unityLibrary')
    implementation fileTree(dir: project(':unityLibrary').getProjectDir().toString() + ('\\libs'), include: ['*.jar'])
  /*  implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.11.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'*/
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}注释掉的是旧的


  • 19
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值