using System; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using AOT; using System.Collections; using UnityEngine.Networking; using SFB; [RequireComponent(typeof(Button))] public class ImportBG : MonoBehaviour, IPointerDownHandler { private static GCHandle _handle; #if UNITY_WEBGL && !UNITY_EDITOR// WebGL----------------------------------------------------------------------------------WebGL [DllImport("__Internal")] private static extern void InitializeFileUpload(Action callback); [DllImport("__Internal")] private static extern void OpenFileDialog(); void Start() { Debug.Log("[ImportBG ] Initializing WebGL file upload"); InitializeFileUpload(HandleFileUpload); } public void OnPointerDown(PointerEventData eventData) { Debug.Log("[ImportBG ] Opening file dialog"); _G.LogoSelect="StartBG"; if (!_handle.IsAllocated) { _handle = GCHandle.Alloc(this); } OpenFileDialog(); } [MonoPInvokeCallback(typeof(Action))] private static void HandleFileUpload(IntPtr data, int length) { Debug.Log($"[ImportBG ] Received file data, length: {length} bytes"); if (!_handle.IsAllocated) { Debug.LogError("[ImportBG ] Handle not allocated!"); return; } try { byte[] byteArray = new byte[length]; Marshal.Copy(data, byteArray, 0, length); if (_handle.Target is ImportBG instance) { MainThreadDispatcher.Instance.Enqueue(() => { try { Debug.Log("[ImportBG ] Creating texture from bytes"); Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false); if (tex.LoadImage(byteArray)) { Debug.Log("[ImportBG ] Texture created successfully"); instance.SetOn(tex); } else { Debug.LogError("[ImportBG ] Failed to load image data"); } } catch (Exception e) { Debug.LogError($"[ImportBG ] Texture creation error: {e}\nStack: {e.StackTrace}"); } }); } } catch (Exception e) { Debug.LogError($"[ImportBG ] File processing error: {e}\nStack: {e.StackTrace}"); } } #endif #if UNITY_EDITOR// EDITOR----------------------------------------------------------------------------------EDITOR public void OnPointerDown(PointerEventData eventData) { } public void ClickLocal() { string paths = StandaloneFileBrowser.OpenFilePanel("Title", "", "png", true)[0]; if (paths.Length > 0) { StartCoroutine(OutputRoutineBG(paths)); Get.o2("HIDER", "LoadingCircle").SetActive(true); } } #endif private IEnumerator OutputRoutineBG(string url) { UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { Texture2D tex = DownloadHandlerTexture.GetContent(www); _G.BACKGROUND = tex; Debug.Log("Loaded image size2: " + tex.width + "x" + tex.height); Sprite mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); GameObject.Find("StartBG").GetComponent().sprite = mySprite;//.SetTexture("_Texture2D",tex); print("LOGO LOADED"); } Get.o2("HIDER", "LoadingCircle").SetActive(false); } public void SetOn(Texture2D tex) { if (tex == null) { Debug.LogWarning("Received null texture"); return; } try { Sprite mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height),new Vector2(0.5f, 0.5f), 100.0f); var logoImage = GameObject.Find("StartBG")?.GetComponent(); if (logoImage != null) { logoImage.sprite = mySprite; Debug.Log($"Logo loaded: {tex.width}x{tex.height}"); _G.BACKGROUND = tex; } else { Debug.LogError("Could not find logo image component"); } } catch (Exception e) { Debug.LogError($"Sprite creation error: {e.Message}"); } } }