Created
November 15, 2020 12:45
-
-
Save rkandas/98748dbe2748a90a9292df7ee627973c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class ExternalTextureRenderer : MonoBehaviour | |
{ | |
[SerializeField] private RawImage image; | |
private Texture2D _imageTexture2D; | |
private IntPtr _nativeTexturePointer; | |
private AndroidJavaObject _androidApiInstance; | |
private void Start() | |
{ | |
_imageTexture2D = new Texture2D(1280, 800, TextureFormat.ARGB32, false) | |
{filterMode = FilterMode.Point}; | |
_imageTexture2D.Apply(); | |
image.texture = _imageTexture2D; | |
_nativeTexturePointer = _imageTexture2D.GetNativeTexturePtr(); | |
} | |
private void Update() | |
{ | |
if (_androidApiInstance == null) | |
{ | |
// it is important to call this in update method. Single Threaded Rendering will run in UnityMain Thread | |
InitializeAndroidSurface(1280, 800); | |
} | |
else | |
{ | |
_androidApiInstance.Call("updateSurfaceTexture"); | |
} | |
} | |
public void InitializeAndroidSurface( int viewportWidth, int viewportHeight) | |
{ | |
AndroidJavaClass androidWebViewApiClass = | |
new AndroidJavaClass("com.thoughtworks.texturerendererandroidplugin.TextureRendererPlugIn"); | |
AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); | |
AndroidJavaObject currentActivityObject = playerClass.GetStatic<AndroidJavaObject>("currentActivity"); | |
_androidApiInstance = | |
androidWebViewApiClass.CallStatic<AndroidJavaObject>("Instance", currentActivityObject, | |
viewportWidth, viewportHeight,_nativeTexturePointer.ToInt32()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment