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.
83 lines
2.1 KiB
83 lines
2.1 KiB
|
|
using UnityEngine; |
|
using AX.MessageSystem; |
|
|
|
public class ObjHideOrShow : MonoBehaviour |
|
{ |
|
private int mCurrentLayer;//用来记录切层到Hidden前的当前层。 |
|
|
|
private void Start() |
|
{ |
|
mCurrentLayer = gameObject.layer; |
|
|
|
if (GameObject.Find("Main Camera").GetComponent<CameraManager>().viewType == CameraViewState.PlanView ) |
|
{ |
|
ObjHidden(null); |
|
} |
|
} |
|
void OnEnable() |
|
{ |
|
MessageDispatcher.AddListener("OBJ_HIDDEN", ObjHidden); |
|
MessageDispatcher.AddListener("OBJ_SHOWED", ObjShowed); |
|
} |
|
void OnDisable() |
|
{ |
|
MessageDispatcher.RemoveListener("OBJ_HIDDEN", ObjHidden); |
|
MessageDispatcher.RemoveListener("OBJ_SHOWED", ObjShowed); |
|
} |
|
void OnDestroy() |
|
{ |
|
MessageDispatcher.RemoveListener("OBJ_HIDDEN", ObjHidden); |
|
MessageDispatcher.RemoveListener("OBJ_SHOWED", ObjShowed); |
|
} |
|
|
|
/// <summary> |
|
/// 隐藏显示物体 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
private void ObjHidden(IMessage obj) |
|
{ |
|
if (gameObject.layer != LayerMask.NameToLayer("Hidden")) |
|
{ |
|
int layer = LayerMask.NameToLayer("Hidden"); |
|
SetLayerRecursively(gameObject, layer); |
|
} |
|
|
|
} |
|
|
|
|
|
/// <summary> |
|
/// 显示隐藏物体 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
private void ObjShowed(IMessage obj) |
|
{ |
|
if (gameObject.layer == LayerMask.NameToLayer("Hidden")) |
|
{ |
|
SetLayerRecursively(gameObject, mCurrentLayer); |
|
} |
|
|
|
} |
|
|
|
|
|
/// <summary> |
|
/// 设置物体新的层 |
|
/// </summary> |
|
/// <param name="obj">设置蹭的物体</param> |
|
/// <param name="newLayer">目标层</param> |
|
private void SetLayerRecursively(GameObject obj, int newLayer) |
|
{ |
|
obj.layer = newLayer; |
|
foreach (Transform child in obj.transform) |
|
{ |
|
if (child.name == "Quad") |
|
{ |
|
return; |
|
} |
|
else |
|
{ |
|
SetLayerRecursively(child.gameObject, newLayer); |
|
} |
|
} |
|
} |
|
}
|
|
|