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.
108 lines
3.6 KiB
108 lines
3.6 KiB
4 years ago
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using AX.MessageSystem;
|
||
|
|
||
|
public class CharacterMessagePanel : MonoBehaviour {
|
||
|
|
||
|
private GameObject mFollowObj;
|
||
|
private GameObject mCanvas;
|
||
|
private Vector3 offset;
|
||
|
private static CharacterMessagePanel instance;
|
||
|
|
||
|
private Text mUserName;//姓名
|
||
|
private Text mDutyCategory;//职务
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
mUserName = transform.Find("UserName").GetComponent<Text>();
|
||
|
mDutyCategory = transform.Find("DutyCategory").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);
|
||
|
}
|
||
|
private void HidePanel(IMessage obj)
|
||
|
{
|
||
|
var messageType = (MessageType)obj.Data;
|
||
|
if (messageType != MessageType.CharacterMessage)
|
||
|
{
|
||
|
instance.gameObject.SetActive(false);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Update () {
|
||
|
SetUIPosition();
|
||
|
}
|
||
|
|
||
|
public static CharacterMessagePanel Instance
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (instance == null)
|
||
|
{
|
||
|
var panel = Resources.Load("UI/CharacterMessagePanel") as GameObject;
|
||
|
var parent = GameObject.Find("Canvas/MessageParent").transform;
|
||
|
var cloneObj = Instantiate(panel,parent);
|
||
|
cloneObj.name = panel.name;
|
||
|
instance = cloneObj.GetComponent<CharacterMessagePanel>();
|
||
|
cloneObj.SetActive(false);
|
||
|
}
|
||
|
return instance;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void SetUI(GameObject obj)
|
||
|
{
|
||
|
mFollowObj = obj;
|
||
|
if (obj.GetComponent<CloneGameObjInfo>().gameObjType == CloneObjType.fireman)
|
||
|
{
|
||
|
var mFireManMessage = obj.GetComponent<CloneGameObjInfo>();
|
||
|
instance.gameObject.SetActive(true);
|
||
|
mUserName.text = "";
|
||
|
mDutyCategory.text = mFireManMessage.GetObjectName();
|
||
|
}
|
||
|
else if(obj.GetComponent<CloneGameObjInfo>().gameObjType == CloneObjType.Character)
|
||
|
{
|
||
|
CloneGameObjInfo mCloneGameObjInfo = obj.GetComponent<CloneGameObjInfo>();
|
||
|
instance.gameObject.SetActive(true);
|
||
|
User mUser = new User();
|
||
|
mUser.Id = mCloneGameObjInfo.UserID;
|
||
|
Room mRoom = CurrentUserInfo.room;
|
||
|
foreach (UserData mUserData in mRoom.UserList)
|
||
|
{
|
||
|
if (mUser.Id == mUserData.UserInfo.Id)
|
||
|
{
|
||
|
mUserName.text = mUserData.UserInfo.RealName;
|
||
|
mDutyCategory.text = mUserData.Role.ToString();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|