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 Importlogo : MonoBehaviour, IPointerDownHandler { private static GCHandle _handle; #if UNITY_WEBGL && !UNITY_EDITOR [DllImport("__Internal")] private static extern void InitializeFileUpload(Action callback); [DllImport("__Internal")] private static extern void OpenFileDialog(); [DllImport("__Internal")] private static extern void SetUploadTarget(string gameObjectName); void Start() { Debug.Log("[Importlogo] Initializing WebGL file upload"); InitializeFileUpload(HandleFileUpload); } public void OnPointerDown(PointerEventData eventData) { _G.LogoSelect = name; Debug.Log("[Importlogo] Opening file dialog"); SetUploadTarget(gameObject.name); OpenFileDialog(); } [MonoPInvokeCallback(typeof(Action))] private static void HandleFileUpload(IntPtr data, int length) { Debug.Log($"[Importlogo] Received file data, length: {length} bytes"); if (!_handle.IsAllocated) { Debug.LogError("[Importlogo] Handle not allocated!"); return; } try { byte[] byteArray = new byte[length]; Marshal.Copy(data, byteArray, 0, length); if (_handle.Target is Importlogo instance) { MainThreadDispatcher.Instance.Enqueue(() => { try { Debug.Log("[Importlogo] Creating texture from bytes"); Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false); if (tex.LoadImage(byteArray)) { Debug.Log("[Importlogo] Texture created successfully"); instance.SetOn(tex); } else { Debug.LogError("[Importlogo] Failed to load image data"); } } catch (Exception e) { Debug.LogError($"[Importlogo] Texture creation error: {e}\nStack: {e.StackTrace}"); } }); } } catch (Exception e) { Debug.LogError($"[Importlogo] File processing error: {e}\nStack: {e.StackTrace}"); } } #endif void OnDestroy() { if (_handle.IsAllocated) { _handle.Free(); } } #if UNITY_STANDALONE && !UNITY_EDITOR public void OnPointerDown(PointerEventData eventData) { } public void ClickLocal() { _G.LogoSelect = name; Texture2D tex = Resources.Load("LibrairyCSV/"+_G.LogoSelect); SetOn(tex); } #endif #if UNITY_EDITOR public void OnPointerDown(PointerEventData eventData) { } public void ClickLocal() { string[] paths = StandaloneFileBrowser.OpenFilePanel("Select Logo", "", "png", true); if (paths.Length > 0 && !string.IsNullOrEmpty(paths[0])) { _G.LogoSelect = name; StartCoroutine(LoadLogoFromPath(paths[0])); Get.o2("HIDER", "LoadingCircle").SetActive(true); } } #endif private IEnumerator LoadLogoFromPath(string path) { print("path======" + path);//path======D:/Clients/Rona/logo2.png using (UnityWebRequest www = UnityWebRequestTexture.GetTexture("file://" + path)) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { Texture2D tex = DownloadHandlerTexture.GetContent(www); SetOn(tex); } Get.o2("HIDER", "LoadingCircle").SetActive(false); } } public void SetOn(Texture2D tex) { if (tex == null) { Debug.LogWarning("Received null texture"); return; } Debug.Log("_G.LogoSelect===" + _G.LogoSelect); 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("Canvas/Panel_main/" + _G.LogoSelect)?.GetComponent(); if (logoImage != null) { logoImage.sprite = mySprite; Debug.Log($"Logo loaded: {tex.width}x{tex.height}"); _G.Logo = mySprite; _G.LOGO = tex; } else { Debug.LogError("Could not find logo image component"); } } catch (Exception e) { Debug.LogError($"Sprite creation error: {e.Message}"); } } public void OnImageBase64Received(string base64Data) { Debug.Log("[Importlogo] Received base64 image string"); MainThreadDispatcher.Instance.Enqueue(() => { try { byte[] imageBytes = Convert.FromBase64String(base64Data); Texture2D tex = new(2, 2, TextureFormat.RGBA32, false); if (tex.LoadImage(imageBytes)) { Debug.Log("[Importlogo] Texture loaded from base64"); SetOn(tex); } else { Debug.LogError("[Importlogo] Failed to decode base64 image"); } } catch (Exception e) { Debug.LogError($"[Importlogo] Base64 decode error: {e.Message}"); } }); } }