using UnityEngine; using System.Runtime.InteropServices; public class YoutubeButton : MonoBehaviour { // Tu peux mettre ID ou URL complète public string youtubeIdOrUrl = "https://youtu.be/Ksnc7kOFUvg"; #if UNITY_WEBGL && !UNITY_EDITOR [DllImport("__Internal")] private static extern void openYoutube(string id); // ✅ nouveau [DllImport("__Internal")] private static extern void closeYoutube(); // ✅ nouveau #endif static string GetVideoId(string idOrUrl) { if (string.IsNullOrEmpty(idOrUrl)) return "Ksnc7kOFUvg"; // Déjà un ID if (!idOrUrl.Contains("http") && !idOrUrl.Contains("youtu")) return idOrUrl; // youtu.be/XXXX int i = idOrUrl.IndexOf("youtu.be/"); if (i >= 0) { string s = idOrUrl.Substring(i + "youtu.be/".Length); int q = s.IndexOf("?"); return (q >= 0) ? s.Substring(0, q) : s; } // youtube.com/watch?v=XXXX i = idOrUrl.IndexOf("v="); if (i >= 0) { string s = idOrUrl.Substring(i + 2); int amp = s.IndexOf("&"); return (amp >= 0) ? s.Substring(0, amp) : s; } // youtube.com/embed/XXXX i = idOrUrl.IndexOf("/embed/"); if (i >= 0) { string s = idOrUrl.Substring(i + "/embed/".Length); int q = s.IndexOf("?"); return (q >= 0) ? s.Substring(0, q) : s; } return "Ksnc7kOFUvg"; } static string GetWatchUrl(string idOrUrl) { if (!string.IsNullOrEmpty(idOrUrl) && (idOrUrl.StartsWith("http://") || idOrUrl.StartsWith("https://"))) return idOrUrl; return "https://www.youtube.com/watch?v=" + GetVideoId(idOrUrl); } // ✅ À brancher sur tous tes boutons vidéos public void OpenYoutube() { string id = GetVideoId(youtubeIdOrUrl); #if UNITY_WEBGL && !UNITY_EDITOR openYoutube(id); // ✅ ouvre / remplace toujours #else // Editor: ouvre l'URL dans le navigateur Application.OpenURL(GetWatchUrl(youtubeIdOrUrl)); #endif } // ✅ À brancher sur ton bouton "Fermer" (panneau) public void CloseYoutube() { #if UNITY_WEBGL && !UNITY_EDITOR closeYoutube(); #else Debug.Log("CloseYoutube: uniquement en WebGL (Editor ne peut pas fermer un player HTML)."); #endif } public void OpenVideoTutorialPanel() { GameObject.Find("Panel_SCENE").transform.Find("TutorialVideo").gameObject.SetActive(true); } }