ar_tourism_flutter_unity/unity/VRProject2/Assets/zhl/zhlScripts/GetKuang.cs
2025-05-14 17:04:13 +08:00

100 lines
3.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
//[ExecuteInEditMode]
public class GetKuang : MonoBehaviour
{
public Button OpenANdOffTextPlane;
public GameObject textPlane;
public Button ShareButton;
public RawImage myRawImage;
//public GameObject OpenANdOffTextPlane2;
string path;
public bool isGreen;
public RectTransform buttonTrans2; // 修改为RectTransform类型
public RectTransform buttonTrans1; // 修改为RectTransform类型
// Start is called before the first frame update
void Start()
{
//Debug.Log(transform.parent.name);
ShareButton.onClick.AddListener(SharePicture);
if (OpenANdOffTextPlane != null)
{
OpenANdOffTextPlane.onClick.AddListener(OpenANdOff);
}
else
{
Debug.LogWarning("OpenANdOffTextPlane button is not assigned.");
}
}
// Update is called once per frame
private void Update()
{
}
public void OpenANdOff()
{
if (textPlane.activeSelf == false)
{
textPlane.SetActive(true);
// 使用anchoredPosition设置UI位置
OpenANdOffTextPlane.GetComponent<RectTransform>().anchoredPosition = buttonTrans2.anchoredPosition;
}
else if (textPlane.activeSelf == true)
{
textPlane.SetActive(false);
// 使用anchoredPosition重置位置
OpenANdOffTextPlane.GetComponent<RectTransform>().anchoredPosition = buttonTrans1.anchoredPosition;
}
}
public void CloseUI()
{
if (textPlane.activeSelf == true)
{
textPlane.SetActive(false);
OpenANdOffTextPlane.GetComponent<RectTransform>().anchoredPosition = buttonTrans1.anchoredPosition;
OpenANdOffTextPlane.gameObject.SetActive(false);
}
}
string myName;
public void SharePicture()
{
RawImage rawImage = myRawImage;
Texture2D texture = rawImage.texture as Texture2D;
//texture.isReadable = true;(改为可读)
byte[] byt;
if (texture != null)
{
byt = texture.EncodeToPNG();
path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
myName = path + "/Pictures/Screenshots/" + "Screenshot_" + GetCurTime() + ".png";
File.WriteAllBytes(myName, byt);
// try
// {
// RuntimeLog.Instance.AddLog(myName);
// }
// catch (Exception ex)
// {
// RuntimeLog.Instance.AddLog(ex.Message);
// }
new NativeShare().AddFile(myName, null).Share();
}
}
string GetCurTime()
{
return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
}
}