Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question SurfaceView exception in Unity 2022

Discussion in 'Android' started by kashif789us, Jun 21, 2023.

  1. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    Hi. Prior to Unity 2022 I would fetch the SurfaceView by extending the UnityPlayerActivity and then using the following Java code:

    Code (CSharp):
    1. FrameLayout frameLayout = (FrameLayout) mUnityPlayer.getView();
    2. SurfaceView surfaceView = (SurfaceView) frameLayout.getChildAt(0);
    After upgrading to Unity 2022, it's throwing an exception:

    How should I get the SurfaceView in Unity 2022?
     
  2. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    I exported the Android project and when searching through it I found out that you can get the surfaceView through the following code:

    Code (CSharp):
    1. SurfaceView surfaceView = findViewById(R.id.unitySurfaceView);
     
    unity_kIW5vB8P_ITvmw likes this.
  3. unity_kIW5vB8P_ITvmw

    unity_kIW5vB8P_ITvmw

    Joined:
    Mar 18, 2021
    Posts:
    4
    I'm facing the same issue.
    Did you found any solution ?
     
  4. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    @unity_kIW5vB8P_ITvmw
     
  5. unity_kIW5vB8P_ITvmw

    unity_kIW5vB8P_ITvmw

    Joined:
    Mar 18, 2021
    Posts:
    4
    Thank you very much.

    I have no knowledge about android development, and don't know how to add "unitySurfaceView" into R.id.
    Then I watch android development tutorial and do the way you found.
    It's working so nice. Thank you ;)

    Now I post the more detail answer for others facing the same problem.
    Step 1: Just add the ids.xml into res/values/
    Step 2: Copy the following content and save it.

    Code (xml):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3.     <item type="id" name="unitySurfaceView" />
    4. </resources>
    5.  
    Step 3: Replace this

    SurfaceView surfaceView = (SurfaceView) frameLayout.getChildAt(0);


    with this

    SurfaceView surfaceView = findViewById(R.id.unitySurfaceView);
     
    anxela24 likes this.