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.
96 lines
3.1 KiB
96 lines
3.1 KiB
using UnityEngine; |
|
using UnityEngine.UI; |
|
using AX.MessageSystem; |
|
|
|
public class TruckMessagePanel : MonoBehaviour { |
|
|
|
private GameObject mFollowObj; |
|
private GameObject mCanvas; |
|
private Vector3 offset; |
|
private static TruckMessagePanel instance; |
|
|
|
private Text mFireTruckType;//车辆类型 |
|
private Text mOrganization;//所属单位 |
|
|
|
private void Awake() |
|
{ |
|
mFireTruckType = transform.Find("FireTruckType").GetComponent<Text>(); |
|
mOrganization = transform.Find("Organization").GetComponent<Text>(); |
|
mCanvas = GameObject.Find("Canvas"); |
|
offset = new Vector3(0, 6,0); |
|
} |
|
void Start () { |
|
MessageDispatcher.AddListener("HideOtherMessagePanel", HidePanel); |
|
} |
|
private void OnDestroy() |
|
{ |
|
MessageDispatcher.RemoveListener("HideOtherMessagePanel", HidePanel); |
|
} |
|
|
|
void Update () { |
|
SetUIPosition(); |
|
} |
|
private void HidePanel(IMessage obj) |
|
{ |
|
var messageType = (MessageType)obj.Data; |
|
if (messageType != MessageType.TruckMessage) |
|
{ |
|
instance.gameObject.SetActive(false); |
|
} |
|
} |
|
|
|
public static TruckMessagePanel Instance |
|
{ |
|
get |
|
{ |
|
if (instance == null) |
|
{ |
|
var panel = Resources.Load("UI/TruckMessagePanel") as GameObject; |
|
var parent = GameObject.Find("Canvas/MessageParent").transform; |
|
var cloneObj = Instantiate(panel, parent); |
|
cloneObj.name = panel.name; |
|
instance = cloneObj.GetComponent<TruckMessagePanel>(); |
|
cloneObj.SetActive(false); |
|
} |
|
return instance; |
|
} |
|
} |
|
|
|
public void SetUI(GameObject obj) |
|
{ |
|
mFollowObj = obj; |
|
var mTruck = obj.GetComponent<CloneGameObjInfo>(); |
|
instance.gameObject.SetActive(true); |
|
mFireTruckType.text = mTruck.GetObjectName(); |
|
User mUser =new User(); |
|
mUser.Id = mTruck.UserID; |
|
Room mRoom = CurrentUserInfo.room; |
|
foreach (UserData mUserData in mRoom.UserList) |
|
{ |
|
if (mUser.Id == mUserData.UserInfo.Id) |
|
{ |
|
mOrganization.text = mUserData.Org.DisplayName; |
|
} |
|
} |
|
} |
|
private void SetUIPosition() |
|
{ |
|
Vector3 dir = (mFollowObj.transform.position - Camera.main.gameObject.transform.position).normalized; |
|
float dot = Vector3.Dot(Camera.main.gameObject.transform.forward, dir);//判断物体是否在相机前面 |
|
if (dot < 0) |
|
{ |
|
gameObject.SetActive(false); |
|
return; |
|
} |
|
else |
|
{ |
|
gameObject.SetActive(true); |
|
} |
|
Vector2 position; |
|
//世界坐标转屏幕坐标 |
|
Vector2 screenPosition = Camera.main.WorldToScreenPoint(mFollowObj.transform.position + offset); |
|
//屏幕坐标转画布坐标 |
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponent<RectTransform>(), screenPosition, mCanvas.GetComponent<Canvas>().worldCamera, out position); |
|
GetComponent<RectTransform>().anchoredPosition = position; |
|
} |
|
}
|
|
|