Skip to content

Experimental Unity 6 (6000) support. #967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
timbotimbo opened this issue Jul 2, 2024 · 13 comments
Open

Experimental Unity 6 (6000) support. #967

timbotimbo opened this issue Jul 2, 2024 · 13 comments
Labels
android This issue is specific to the Android Platform. enhancement New feature or request help wanted Extra attention is needed ios This issue is specific to the iOS/iPadOS Platform. unity breaking change

Comments

@timbotimbo
Copy link
Collaborator

timbotimbo commented Jul 2, 2024

Description

Unity 2023.1 and 6 (6000.0) include breaking changes for the Android version of this plugin.
And given that 2023.3 turned into 6000, I decided to implement support for Unity 6000.

For a litte more info, see the PR #966

Installing & testing

This alpha version is now live in the experimental/unity_6000 branch.

You can either download it and include the plugin using a relative path

flutter_unity_widget:
    path: <folder path>

or include it using the git url

flutter_unity_widget:
    git:
      url: https://github.com/juicycleff/flutter-unity-view-widget.git
      ref: experimental/unity_6000 # branch name

Update
The branch now also includes a fuw-6000.0.0.unitypackage.

Setup (android)

  1. Your Flutter project will need to use Java 17 and gradle 8.x,
    Check the example project in the unity_6000 branch for the relevant build.gradle files.

  2. Update.
    Use the 6000.0.0 unitypackage or copy the latest unity/Assets/FlutterUnityIntegration/Editor folder from the branch into your Unity project.

  3. Set the Unity Application Entry Point to Activity.
    Player Settings -> Settings for Android -> Other Settings -> Application Entry Point

  4. After the Unity export, android/unityLibrary/build.gradle might include a broken import.

    • Either fix this automatically by updating your Assets/FlutterUnityIntegration/Editor/build.cs file with the one from the example project.
    • Or comment the 2nd line apply from: '../shared/keepUnitySymbols.gradle' in unityLibrary/build.gradle

Setup (ios)

There don't seem to be any breaking changes in the plugin for iOS.

You do need a small change in the Unity export.
Use the 6000.0.0 unitypackage or copy the latest unity/Assets/FlutterUnityIntegration/Editor folder from the branch into your Unity project.

@timbotimbo timbotimbo added enhancement New feature or request help wanted Extra attention is needed android This issue is specific to the Android Platform. unity breaking change labels Jul 2, 2024
@artificerchris
Copy link

I'm getting this error when trying to build my flutter application with the export for Android:

Build file '<flutter project>/android/unityLibrary/build.gradle' line: 57

* What went wrong:
A problem occurred evaluating project ':unityLibrary'.
> Could not find method packaging() for arguments [build_e9lpdzw6rpzcgazd3rj2x5k7f$_run_closure2$_closure12@5afe6187] on extension 'android' of type com.android.build.gradle.LibraryExtension.

Coming from this set of lines in the build.grade:

packaging {
    jniLibs {
        useLegacyPackaging true
    }
}

@timbotimbo
Copy link
Collaborator Author

timbotimbo commented Aug 25, 2024

@artificerchris

Just tried this again with Unity 6000.0.16 and Flutter 3.24.1.

The example from this repo worked worked without modifications.
(I just had to delete a themeselector line from the androidmanifest)

Then I made a new project using Flutter create, and got your error.

Matching the gradle, AGP and kotlin versions with the example got me past the error.
You likely forgot to upgrade the project to gradle and AGP 8.x.

Using the new gradle apply method used in the latest Flutter versions:

gradle-wrapper.properties

- distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip
+ distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip

settings.gradle

-    id "com.android.application" version "7.3.0" apply false
-    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
+    id "com.android.application" version "8.3.0" apply false
+    id "org.jetbrains.kotlin.android" version "1.9.10" apply false

@artificerchris
Copy link

Seems my reading comprehension was a bit off, thank you for your help, that did fix that issue, and now I have a whole new one, the build works, all the C++ compiles successfully, but I get this error during my build:

e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:7:27 Unresolved reference: UnityPlayerActivity
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:11:31 Unresolved reference: UnityPlayerActivity
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:14:5 'onCreate' overrides nothing
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:15:15 Unresolved reference: onCreate
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:17:14 Unresolved reference: window
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:18:22 Unresolved reference: intent
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:25:9 Unresolved reference: mUnityPlayer
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:30:9 Unresolved reference: mUnityPlayer
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:34:22 None of the following functions can be called with the arguments supplied: 
public constructor Intent(p0: Context!, p1: Class<*>!) defined in android.content.Intent
public constructor Intent(p0: String!, p1: Uri!) defined in android.content.Intent
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:36:16 Variable expected
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:37:9 Unresolved reference: startActivity
e: file:///<dart-pub-cache>/git/flutter-unity-view-widget-d910dd5904bdbf0eda4a52c31e6f3a66da992f9d/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/OverrideUnityActivity.kt:40:5 'onUnityPlayerUnloaded' overrides nothing

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':flutter_unity_widget:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 5m 59s
Error: Gradle task assembleDebug failed with exit code 1

@timbotimbo
Copy link
Collaborator Author

In Unity try Player Settings -> Settings for Android -> Other Settings -> Application Entry Point
Check Activity and not GameActivity.

@artificerchris
Copy link

@timbotimbo That did it! Thank you so much for your help, my application now compiles properly for both iOS and Android. Would it be best if I run into more issues to just reply to this issue, or should I make new issues for other things I run into in the 6000 branch?

@devidw
Copy link

devidw commented Jan 15, 2025

Hey @timbotimbo,

Thanks so much for keeping this library alive!

Based on the title 'for Android', does it mean the unity ios build output is unchanged - so unity 6 should work fine, or more like it that would require another PR to add IOS support?


For context:

This version works great for me on android.

On IOS however I'm stuck with a white screen and no output logs in xcode as soon as I include the flutter unity widget as a dependency.

(I also run into the UnityFramework not found issue a lot of ppl brought up in many other issues. Was able to fix that in my build by adding and embedding UnityFramework in the pods target for the flutter unity widget directly and also adding the parent path of the folder to the framework search paths under build settings.)

The only way I could get it running on IOS so far was with a unity 2022 export + master branch.

@timbotimbo timbotimbo changed the title Experimental Unity 6 (6000) support for Android. Experimental Unity 6 (6000) support. Feb 15, 2025
@timbotimbo
Copy link
Collaborator Author

@devidw
Sorry for the slow response.
I just added a commit that should fix your ios issue.

The string replace in /Assets/FlutterUnityIntegration/Editor/XCodePostBuild.cswas looking for a line that doesn't exist anymore in 6000.
Simply using the latest version of that file should be enough.
You might have to delete the ios/unitylibrary folder if you made an export prior to this fix.

I did a quick test on an iPad and everything seems to work like the master branch.

@timbotimbo timbotimbo added the ios This issue is specific to the iOS/iPadOS Platform. label Feb 15, 2025
@timbotimbo timbotimbo pinned this issue Feb 15, 2025
@devidw
Copy link

devidw commented Feb 15, 2025

Much appreciated @timbotimbo!

We ended up working around unity for the particular project we were working on, but this will be my go-to solution for any mobile + unity needs in the future.

@timbotimbo
Copy link
Collaborator Author

Added a fuw-6000.0.0 unitypackage in this branch to make it easier to use the updated export scripts.

@vgtle
Copy link

vgtle commented Mar 5, 2025

I had issues with android, where I got a white screen. Turned out, I need to add <string name=”game_view_content_description”>Game view</string> to the generated strings.xml file. I am still looking for the best way to do this.

@timbotimbo
Copy link
Collaborator Author

That is an old issue (as far back as Unity 2018) that I've never managed to reproduce.
But some unknown Unity config or plugin seems to cause the string to be omitted.
If you used anything specific in Unity, please let me know.

My exports of the example have strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">flutterunitywidgets</string>
  <string name="game_view_content_description">Game view</string>
</resources>

You can try adding this check to the build.cs export.
timbotimbo@00f5c8f

/* Assets/FlutterUnityIntegration/Editor/Build.cs
  private static void ModifyAndroidGradle() { 
*/

// Make sure "game_view_content_description" is in strings.xml
var stringsFile = Path.Combine(APKPath, "launcher", "src", "main", "res", "values", "strings.xml");
if (File.Exists(stringsFile))
{
    var stringsText = File.ReadAllText(stringsFile);
    if (!stringsText.Contains("game_view_content_description"))
    {
        stringsText = stringsText.Replace("<resources>", "<resources>\n  <string name=\"game_view_content_description\">Game view</string>");
        File.WriteAllText(stringsFile, stringsText);
    }
}
else
{
    Debug.LogError("Android res/values/strings.xml file not found during export.");
}

I'll make sure to add that to this repo in the future.

@vgtle
Copy link

vgtle commented Mar 5, 2025

Yes that is what I did now. I don't know why this happens now. The only thing I did was upgrading unity and the ar packages. Before it was working without any problems. Now, I Just had to move that to the following line since it was overwritten every time and I was wondering why it is not working:

     DoBuildAndroid(Path.Combine(APKPath, "unityLibrary"), false, true);
    Copy(Path.Combine(APKPath + "/launcher/src/main/res"), Path.Combine(AndroidExportPath, "src/main/res"));

    ModifyStringsXml(AndroidExportPath); // <--- Do it here because it was overwritten by the line above

@Nomad404
Copy link

Nomad404 commented Apr 9, 2025

FYI
I've tested this with 6000.0.44f1 and was only able to make the widget work after adding
<string name="game_view_content_description" translatable="false">Game view</string> to strings.xml of :unityLibrary manually after building the module from Unity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android This issue is specific to the Android Platform. enhancement New feature or request help wanted Extra attention is needed ios This issue is specific to the iOS/iPadOS Platform. unity breaking change
Projects
None yet
Development

No branches or pull requests

5 participants