using System.Runtime.InteropServices; using UnityEngine; public class VideoJSBridge : MonoBehaviour { #if UNITY_WEBGL && !UNITY_EDITOR [DllImport("__Internal")] static extern void HOY_VideoCreate(int id, string url); [DllImport("__Internal")] static extern void HOY_VideoDestroy(int id); [DllImport("__Internal")] static extern bool HOY_VideoReady(int id); [DllImport("__Internal")] static extern int HOY_VideoWidth(int id); [DllImport("__Internal")] static extern int HOY_VideoHeight(int id); [DllImport("__Internal")] static extern void HOY_VideoUpdateTexture(int id, int unityTextureId); #else static void HOY_VideoCreate(int id, string url) {} static void HOY_VideoDestroy(int id) {} static bool HOY_VideoReady(int id) => false; static int HOY_VideoWidth(int id) => 0; static int HOY_VideoHeight(int id) => 0; static void HOY_VideoUpdateTexture(int id, int tid) {} #endif int _id; Renderer _r; Texture2D _tex; bool _ready, _init; public void Initialise(string url, Renderer r, int id) { _id = id; _r = r; HOY_VideoCreate(_id, url); _init = true; } void Update() { if (!_init) return; if (!_ready) { if (!HOY_VideoReady(_id)) return; int w = HOY_VideoWidth(_id), h = HOY_VideoHeight(_id); if (w <= 0 || h <= 0) return; _tex = new Texture2D(w, h, TextureFormat.RGBA32, false); _tex.wrapMode = TextureWrapMode.Clamp; if (_r != null) { _r.material = new Material(_r.sharedMaterial); _r.material.mainTexture = _tex; } _ready = true; } if (_tex != null) HOY_VideoUpdateTexture(_id, (int)_tex.GetNativeTexturePtr()); } void OnDestroy() { if (_init) HOY_VideoDestroy(_id); if (_tex != null) Destroy(_tex); } }