上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

165 lines
4.8 KiB

using AX.MessageSystem;
using System.Collections;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class UIFollowTarget : MonoBehaviour
{
private Transform Target;
[HideInInspector] public Button m_Button;
public string IconName;
public float DistanceY = 0f;
//显示文字
public bool ShowInfo;
private Text m_Text;
public string Info;
//拉近
public bool ZoomIn = true;
public ZoomInMode Mode = ZoomInMode.Normal;
public float RotX = 0;
public float RotY = 30;
public float Distance = 5f;
//切换楼层
public bool ChangeFloor;
public int FloorNumber;
public string Buildingfilter = "Floor";
//显示属性面板
public bool ShowAttrubePanel;
public bool isRendering;
public void LoadIcon(bool isLoad)
{
if (isLoad)
{
Target = transform;
if (GameObject.Find(this.name + "Icon") == null)
{
//GameObject go = Instantiate(Loaded.Result);
GameObject go = Instantiate(AssetManager.Instance.Icons.Find(icon => icon.name==IconName))as GameObject;
go.name = this.name + "Icon";
go.transform.SetParent(UIManager.GetUIRoot(UIViewType.Icon), false);
m_Button = go.GetComponent<Button>();
m_Button.OnClickAsObservable()
.Subscribe(_ => OnClick());
m_Text = m_Button.transform.Find("Text").GetComponent<Text>();
if (ShowInfo)
{
m_Text.gameObject.SetActive(true);
m_Text.text = Info;
}
else
{
m_Text.gameObject.SetActive(false);
m_Text.text = string.Empty;
}
}
isRendering = true;
}
else
{
if (m_Button != null)
{
Destroy(m_Button.gameObject);
m_Button = null;
}
isRendering = false;
}
}
private void OnClick()
{
if (ChangeFloor)
{
MessageDispatcher.SendMessage("FLOORNUMBER", FloorNumber, Buildingfilter);
}
if (ShowAttrubePanel)
{
UIManager.Instance.Show<EquipmentRoomAttributePanel>(new EquipmentRoom() { Id=name});
//AssetManager.EquipmentSelected = this.gameObject;
//UIPanel.Show<EquipmentRoomAttributePanel>();
}
if (ZoomIn)
{
switch (Mode)
{
case ZoomInMode.Normal:
Camera.main.GetComponent<CameraOrbit>().SetCameraView(Target.position, Distance);
break;
case ZoomInMode.RotateAngle:
Camera.main.GetComponent<CameraOrbit>().SetCameraView(Target.position, Distance, RotX, RotY);
break;
case ZoomInMode.RotateXAngle:
Camera.main.GetComponent<CameraOrbit>().SetCameraView(Target.position, Distance, RotX, Camera.main.GetComponent<CameraOrbit>().y);
break;
case ZoomInMode.RotateYAngle:
Camera.main.GetComponent<CameraOrbit>().SetCameraView(Target.position, Distance, Camera.main.GetComponent<CameraOrbit>().x, RotY);
break;
}
}
}
void Update()
{
if (m_Button == null)
return;
if (IsInView(transform.position) && isRendering)
{
m_Button.gameObject.SetActive(true);
m_Button.GetComponent<RectTransform>().position = WorldToUI(new Vector3(Target.position.x, Target.position.y + DistanceY, Target.position.z));
}
else
m_Button.gameObject.SetActive(false);
}
public bool IsInView(Vector3 worldPos)
{
Transform camTransform = Camera.main.transform;
Vector2 viewPos = Camera.main.WorldToViewportPoint(worldPos);
Vector3 dir = (worldPos - camTransform.position).normalized;
float dot = Vector3.Dot(camTransform.forward, dir);//判断物体是否在相机前面
if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1)
return true;
else
return false;
}
public Vector3 WorldToUI(Vector3 Point)
{
Vector3 pos = RectTransformUtility.WorldToScreenPoint(Camera.main, Point);
pos.z = 0;
return pos;
}
private void OnDestroy()
{
LoadIcon(false);
}
public void Hide()
{
StartCoroutine(HideCoroutine());
}
private IEnumerator HideCoroutine()
{
isRendering = false;
yield return new WaitForSeconds(0.1f);
gameObject.SetActive(false);
}
}