2026-01-09 08:45:22 +03:00

132 lines
4.8 KiB
C#

// csharp
using System;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.IO;
public class QR_viewer : MonoBehaviour
{
public Transform guid;
[Header("Icons")]
public Sprite telegramIcon;
public Sprite whatsappIcon;
public Sprite iconBackground;
[Header("Icon settings")]
[Range(0.1f, 0.4f)]
public float iconSizeRatio = 0.22f;
public float backgroundPadding = 1.12f;
private void OnEnable()
{
SettingsManager.instance.OnSettingsChanged.AddListener(UpdateQRcode);
}
private void Start()
{
UpdateQRcode();
}
void UpdateQRcode()
{
foreach (Transform child in guid)
Destroy(child.gameObject);
string setting = SettingsManager.instance.GetSettingValue("QR-код для связи");
if (setting == "Telegram")
{
string url = "https://t.me/" + PlayerPrefs.GetString("TelegramContact");
GenerateQRPlain(url, guid, System.Drawing.Color.FromArgb(20, 120, 255), System.Drawing.Color.FromArgb(255, 255, 255));
}
else if (setting == "WhatsApp")
{
string url = "https://wa.me/" + PlayerPrefs.GetString("WhatsAppNumber");
GenerateQRPlain(url, guid, System.Drawing.Color.FromArgb(0, 200, 0), System.Drawing.Color.FromArgb(255, 255, 255));
}
else
{
string url = "https://t.me/" + PlayerPrefs.GetString("TelegramContact");
GenerateQRPlain(url, guid,System.Drawing.Color.FromArgb(20, 120, 255), System.Drawing.Color.FromArgb(255, 255, 255));
url = "https://wa.me/" + PlayerPrefs.GetString("WhatsAppNumber");
GenerateQRPlain(url, guid, System.Drawing.Color.FromArgb(0, 200, 0), System.Drawing.Color.FromArgb(255, 255, 255));
}
}
public void GenerateQRPlain(string url, Transform parent, System.Drawing.Color darkColor,System.Drawing.Color whiteColor)
{
string suffix = $"Здравствуйте! Я победил на аппарате {PlayerPrefs.GetString("Serial")}. Жду приз!";
suffix = workOnSuffix(suffix);
string message = $"{url}?text={suffix}";
Debug.Log(message);
// стандартная генерация текстуры через QrMaker (без цветовой подмены)
Texture2D tex = QrMaker.MakeTexture(message, 1, drawQuietZones: true, darkColor, whiteColor);
if (tex == null)
return;
tex.filterMode = FilterMode.Point;
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height),
new Vector2(0.5f, 0.5f));
GameObject go = new GameObject("QR_Image", typeof(RectTransform), typeof(Image));
go.transform.SetParent(parent, false);
RectTransform qrRt = go.GetComponent<RectTransform>();
qrRt.anchorMin = qrRt.anchorMax = new Vector2(0.5f, 0.5f);
qrRt.pivot = new Vector2(0.5f, 0.5f);
qrRt.sizeDelta = new Vector2(tex.width, tex.height);
Image img = go.GetComponent<Image>();
img.sprite = sprite;
img.preserveAspect = true;
Sprite iconSprite = url.Contains("t.me") ? telegramIcon : whatsappIcon;
if (iconSprite == null)
return;
float iconSize = Mathf.Min(qrRt.sizeDelta.x, qrRt.sizeDelta.y) * iconSizeRatio;
if (iconBackground != null)
{
GameObject bg = new GameObject("IconBackground", typeof(RectTransform), typeof(Image));
bg.transform.SetParent(go.transform, false);
RectTransform bgRt = bg.GetComponent<RectTransform>();
bgRt.anchorMin = bgRt.anchorMax = new Vector2(0.5f, 0.5f);
bgRt.pivot = new Vector2(0.5f, 0.5f);
bgRt.anchoredPosition = Vector2.zero;
bgRt.sizeDelta = new Vector2(iconSize * backgroundPadding, iconSize * backgroundPadding);
Image bgImg = bg.GetComponent<Image>();
bgImg.sprite = iconBackground;
bgImg.preserveAspect = true;
bgImg.color = Color.white;
}
GameObject iconGo = new GameObject("QR_Icon", typeof(RectTransform), typeof(Image));
iconGo.transform.SetParent(go.transform, false);
RectTransform iconRt = iconGo.GetComponent<RectTransform>();
iconRt.anchorMin = iconRt.anchorMax = new Vector2(0.5f, 0.5f);
iconRt.pivot = new Vector2(0.5f, 0.5f);
iconRt.anchoredPosition = Vector2.zero;
iconRt.sizeDelta = new Vector2(iconSize, iconSize);
Image iconImg = iconGo.GetComponent<Image>();
iconImg.sprite = iconSprite;
iconImg.preserveAspect = true;
iconImg.raycastTarget = false;
}
string workOnSuffix(string suffix)
{
return Uri.EscapeDataString(suffix);
}
}