ar_tourism_flutter_unity/unity/VRProject2/Assets/zhl/zhlScripts/MyPlane.cs

312 lines
8.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using Unity.VisualScripting;
using System.Collections;
public class MyPlane : MonoBehaviour
{
public int idx;
public int idy;
public Vector3 myVector3;
private Quaternion identy;
public Transform myCameraTra;
public bool isMP4 = false;
public GameObject rawImageKuang;
[SerializeField]private Transform xiKuang;
[SerializeField]private Material[] rawImageGetmaterials;
public Button closeButton;
public Camera myCamera;
public Transform gameParent;
public Text myText;
public Button[] thisButtons;
public RawImage rawImageGet;
public Vector3 originalScale;
public GameObject thisPlane;
public GetKuang getKuang;
//UI是否被关闭回到世界位置
private bool isClose = false;
private ResourceUI resourceUI;
public void SetmyVector3()
{
myVector3 = transform.position;
identy = transform.rotation;
}
void Awake()
{
resourceUI = GetComponent<ResourceUI>();
}
void OnEnable()
{
if(resourceUI.isResquested == false)
{
if(PlayerManager.Instance != null && PlayerManager.Instance.initSprites.Length > 0)
{
int random = Random.Range(0,PlayerManager.Instance.initSprites.Length);
rawImageGet.texture = PlayerManager.Instance.initSprites[random];
}
else
{
Debug.Log(idx + "," + idy + ":NULL");
}
}
}
void Start()
{
SetmyVector3();
gameParent = transform.parent;
isMP4 = false;
getKuang = transform.GetComponent<GetKuang>();
myCamera = GameObject.Find("MyCamera").GetComponent<Camera>();
myCameraTra = myCamera.transform.GetChild(0);
closeButton.onClick.AddListener(() =>
{
OnBecameInvisible();
});
originalScale = transform.localScale; // 记录原始缩放
}
public void GetImage(Texture thisTexture, string thisText)
{
this.rawImageGet.texture = thisTexture;
myText.text = thisText;
}
// void Update()
// {
// // SetButton();
// // if (transform.parent.name.Contains("childTran"))
// // {
// // transform.position = transform.parent.position;
// // transform.rotation = transform.parent.rotation;
// // }
// // else
// // {
// // }
// // if (transform.position == myCamera.transform.position)
// // {
// // OnBecameInvisible();
// // }
// }
public void SetButton()
{
if (transform.parent != null)
if (transform.parent.parent != null)
{
if (transform.parent.parent.name == "MyCamera")
{
rawImageGet.color = new Color(1, 1, 1, 1);
foreach (var item in thisButtons)
{
item.gameObject.SetActive(true);
}
}
else
{
rawImageGet.color = new Color(1, 1, 1, 0.8f);
if (thisButtons[0].gameObject.activeSelf)
{
foreach (var item in thisButtons)
{
item.gameObject.SetActive(false);
}
}
}
}
}
public void StartPlay()
{
}
public void Left()
{
Debug.Log("123");
OnBecameInvisible();
PlaneManager planeManager = transform.parent.GetComponent<PlaneManager>();
if (planeManager != null)
{
int currentIndex = planeManager.gameObjects.IndexOf(gameObject);
if (currentIndex > 0)
{
GameObject previousObject = planeManager.gameObjects[currentIndex - 1];
previousObject.GetComponent<MyPlane>().OnMoveInvisible();
previousObject.GetComponent<MyPlane>().StartPlay();
}
else if (currentIndex == 0)
{
GameObject previousObject = planeManager.gameObjects[planeManager.gameObjects.Count - 1];
previousObject.GetComponent<MyPlane>().OnMoveInvisible();
previousObject.GetComponent<MyPlane>().StartPlay();
}
}
}
public void Right()
{
OnBecameInvisible();
PlaneManager planeManager = transform.parent.GetComponent<PlaneManager>();
if (planeManager != null)
{
int currentIndex = planeManager.gameObjects.IndexOf(gameObject);
if (currentIndex < planeManager.gameObjects.Count - 1)
{
GameObject previousObject = planeManager.gameObjects[currentIndex + 1];
previousObject.GetComponent<MyPlane>().OnMoveInvisible();
previousObject.GetComponent<MyPlane>().StartPlay();
}
else if (currentIndex == planeManager.gameObjects.Count - 1)
{
GameObject previousObject = planeManager.gameObjects[0];
previousObject.GetComponent<MyPlane>().OnMoveInvisible();
previousObject.GetComponent<MyPlane>().StartPlay();
}
}
}
private IEnumerator ReturnMoveTransformToTarget(Transform sourceTransform, Vector3 targetTransform, float duration)
{
var t = 0f;
while (t < duration)
{
t += Time.deltaTime;
//Debug.Log("t:" + t);
sourceTransform.SetPositionAndRotation(
Vector3.Lerp(sourceTransform.position, targetTransform, t / duration),
Quaternion.Slerp(sourceTransform.rotation, identy, t / duration)
);
sourceTransform.localScale = Vector3.Lerp(sourceTransform.localScale, originalScale * gameParent.parent.localScale.x, t / duration);
yield return null;
}
transform.SetParent(gameParent);
transform.SetPositionAndRotation(myVector3, identy);
transform.localScale = originalScale;
foreach (var item in thisButtons)
{
item.gameObject.SetActive(false);
}
transform.gameObject.layer = LayerMask.NameToLayer("Default");
//isClose = false;
ResourcesUIChooseOnly.Instance.isChosenForResources = false;
}
public void OnBecameInvisible()
{
//thisPlane.SetActive(false);
getKuang.CloseUI();
if(resourceUI.uIType == UIType.Small){
rawImageGet.color = new Color(1, 1, 1, 0.8f);
}else{
rawImageGet.color = new Color(1, 1, 1, 0.9f);
}
Debug.Log(transform.localScale + "。。。" + originalScale);
Debug.Log("HUIQU");
rawImageKuang.SetActive(true);
xiKuang.gameObject.SetActive(false);
rawImageGet.material = rawImageGetmaterials[0];
if (transform.parent != null)
if (transform.parent.parent.name == "MyCamera")
{
StartCoroutine(ReturnMoveTransformToTarget(transform, myVector3, 1f));
}
}
private IEnumerator MoveTransformToTarget(Transform sourceTransform, Transform targetTransform, float duration)
{
Debug.Log("targetTran.name" + targetTransform.name);
transform.localScale = originalScale/gameParent.parent.localScale.x;
//Debug.Log("设置为父物体");
sourceTransform.SetParent(targetTransform);
//Debug.Log("设置为本地坐标原点");
// 激活按钮
if (thisButtons != null)
{
foreach (var item in thisButtons)
{
if (item != null)
{
if(item == resourceUI.closeBt)
{
item.gameObject.SetActive(true);
}
else
{
if(resourceUI.isUserUploaded == true)
{
item.gameObject.SetActive(true);
if (item == resourceUI.infoBt)
{
item.onClick.Invoke();
}
}
else
{
item.gameObject.SetActive(false);
}
}
}
}
}
// 设置层和颜色
transform.gameObject.layer = LayerMask.NameToLayer("Face");
if (rawImageGet != null)
rawImageGet.color = new Color(1, 1, 1, 1);
var t = 0f;
while (t < duration)
{
t += Time.deltaTime;
//Debug.Log("t:" + t);
sourceTransform.SetPositionAndRotation(
Vector3.Lerp(sourceTransform.position, targetTransform.position, t / duration),
Quaternion.Slerp(sourceTransform.rotation, targetTransform.rotation, t / duration)
);
yield return null;
}
sourceTransform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
}
public void OnMoveInvisible()
{
//if(!isClose)
if(!ResourcesUIChooseOnly.Instance.isChosenForResources)
{
Debug.Log("OnMoveInvisible");
//isClose = true;
ResourcesUIChooseOnly.Instance.isChosenForResources = true;
SetmyVector3();
//Vector3 newRotation = myCameraTra.parent.GetChild(1).transform.rotation.eulerAngles;
xiKuang.gameObject.SetActive(true);
thisPlane.SetActive(false);
rawImageKuang.SetActive(false);
rawImageGet.material = rawImageGetmaterials[1];
Transform targetTransform = transform.parent.name.Contains("BigPlane") ?
myCameraTra : myCameraTra.parent.GetChild(1);
// 清理现有动画
transform.DOKill();
StopAllCoroutines();
// 启动新的移动协程
//StartCoroutine(ARMoveToTarget(targetTransform, newRotation));
StartCoroutine(MoveTransformToTarget(transform, targetTransform, 1f));
}
}
}