public static void ExportEvents(SceneMap map,string path = null){ int mapId = 10; if (map != null) { mapId = map.sceneId; } string exportDir = MapUtil.DataPath(); exportDir += "MapData/"; if (!Directory.Exists (exportDir)) { Directory.CreateDirectory (exportDir); } if (path == null) { path = exportDir +"/MapEvent_" + mapId + ".bytes"; } SceneMap mapRoot = FindSceneRoot (); GameObject triggerRoot = FindTriggerRoot(); GameObject wallRoot = FindWallRoot (); GameObject eventRoot = FindEventRoot (); GameObject itemRoot = FindItemRoot (); GameObject audioRoot = FindAudioRoot (); GameObject areaRoot = FindAreaRoot (); List<Example.MapWall> walls = new List<Example.MapWall> (); List<Example.MapEvent> events = new List<Example.MapEvent> (); List<Example.MapTrigger> triggers = new List<Example.MapTrigger> (); List<Example.MapEntityEvent> entityEvents = new List<Example.MapEntityEvent> (); List<Example.MapItem> items = new List<Example.MapItem> (); List<Example.MapAudioSource> audios = new List<Example.MapAudioSource> (); List<Example.MapArea> areas = new List<Example.MapArea> (); if (mapRoot == null) { Debug.LogError ("没有场景配置,将场景预制拖到编辑器中,或者创建一个新的场景"); return; } if (eventRoot != null) { var sceneEvents = eventRoot.GetComponentsInChildren<MapEvent> (); foreach (var sceneEvent in sceneEvents) { var mapEvent = ExportMapEvent (events,sceneEvent); } } if (wallRoot != null) { var sceneWalls = wallRoot.GetComponentsInChildren<MapWall> (); foreach (var sceneWall in sceneWalls) { var wall = ExportMapWall (walls,sceneWall); var eventTriggers = sceneWall.GetComponentsInChildren <MapEventTrigger>(); var entityEvent = ExportMapEntityEvent (eventTriggers,wall.Id,Example.MapEntityEvent.Type.WALL); if (eventTriggers.Length > 0) { entityEvents.Add (entityEvent); } } } var sceneTriggers = triggerRoot.GetComponentsInChildren<SceneTrigger> (); foreach (var sceneTrigger in sceneTriggers) { var trigger = ExportMapTrigger (triggers,sceneTrigger); var eventTriggers = sceneTrigger.GetComponentsInChildren <MapEventTrigger>(); foreach (var eventTrigger in eventTriggers) { if (eventTrigger.eventType == MapEventTriggerType.TriggerIn) { trigger.InActions = ExportMapEventActions (eventTrigger.GetComponentsInChildren<MapEventAction>(),trigger.Id.ToString()); }else if (eventTrigger.eventType == MapEventTriggerType.TriggerOut) { trigger.OutActions = ExportMapEventActions (eventTrigger.GetComponentsInChildren<MapEventAction>(),trigger.Id.ToString()); } } } foreach (var npc in mapRoot.npcList) { var eventTriggers = npc.GetComponentsInChildren <MapEventTrigger>(); var entityEvent = ExportMapEntityEvent (eventTriggers,npc.objectId,Example.MapEntityEvent.Type.NPC); if (eventTriggers.Length > 0) { entityEvents.Add (entityEvent); } } if (itemRoot != null) { var sceneItems = itemRoot.GetComponentsInChildren<SceneItem> (); foreach (var sceneItem in sceneItems) { if (string.IsNullOrEmpty (sceneItem.itemId)) { //GameObject.DestroyImmediate (sceneItem.gameObject); Debug.LogErrorFormat("{0} not valid item object",MapEventHelper.GetGameObjectPath(sceneItem.transform)); continue; } var item = ExportMapItem (items,sceneItem); var eventTriggers = sceneItem.GetComponentsInChildren <MapEventTrigger>(); var entityEvent = ExportMapEntityEvent (eventTriggers,item.Id,Example.MapEntityEvent.Type.ITEM); if (eventTriggers.Length > 0) { entityEvents.Add (entityEvent); } } } if (audioRoot != null) { var audioSources = audioRoot.GetComponentsInChildren<MapAudioSource> (); foreach (var audioSource in audioSources) { var audio = ExportMapAudioSource (audios,audioSource); } } if (areaRoot != null) { var mapAreas = areaRoot.GetComponentsInChildren<MapArea> (); foreach (var mapArea in mapAreas) { var area = ExportMapArea (areas,mapArea); } } Example.MapEventData mapEventData = new Example.MapEventData (); mapEventData.Walls = walls; mapEventData.Events = events; mapEventData.Triggers = triggers; mapEventData.EntityEvents = entityEvents; mapEventData.Items = items; mapEventData.AudioSources = audios; mapEventData.Areas = areas; var bytes = Example.MapEventData.SerializeToBytes (mapEventData); FileStream fs = new FileStream (path, FileMode.Create); fs.Write (bytes,0,bytes.Length); fs.Close (); /*var prefab = PrefabUtility.GetPrefabParent (wallRoot); PrefabUtility.ReplacePrefab (wallRoot,prefab); //prefab = PrefabUtility.GetPrefabParent (itemRoot); //PrefabUtility.ReplacePrefab (itemRoot,prefab); prefab = PrefabUtility.GetPrefabParent (eventRoot); PrefabUtility.ReplacePrefab (eventRoot,prefab); prefab = PrefabUtility.GetPrefabParent (triggerRoot); PrefabUtility.ReplacePrefab (triggerRoot,prefab);*/ Debug.LogFormat ("导出场景事件{0}数据到{1}",mapId,path); }
public static void PrintMapEvent(string path) { var mapName = System.IO.Path.GetFileName(path); var root = MapEventHelper.GetOrCreateGameObject(mapName); var wallRoot = MapEventHelper.AddChildGameObject(root, "GameWalls"); var triggerRoot = MapEventHelper.AddChildGameObject(root, "GameTriggers"); var eventRoot = MapEventHelper.AddChildGameObject(root, "GameEvents"); var data = File.ReadAllBytes(path); Example.MapEventData mapEventData = Example.MapEventData.Deserialize(data); foreach (var mapWall in mapEventData.Walls) { var go = MapEventHelper.AddChildGameObject(wallRoot, "wall-" + (mapWall.Id)); go.transform.position = MapUtil.Vector3fToVector3(mapWall.Center); go.transform.rotation = Quaternion.Euler(MapUtil.Vector3fToVector3(mapWall.Rotation)); var box = go.AddComponent <BoxCollider> (); box.size = MapUtil.Vector3fToVector3(mapWall.Size); } foreach (var mapTrigger in mapEventData.Triggers) { var go = MapEventHelper.AddChildGameObject(triggerRoot, "trigger-" + (mapTrigger.Id)); go.transform.position = MapUtil.Vector3fToVector3(mapTrigger.Center); go.transform.rotation = Quaternion.Euler(MapUtil.Vector3fToVector3(mapTrigger.Rotation)); if (mapTrigger.shape == Example.MapTrigger.Shape.BOX) { var box = go.AddComponent <BoxCollider> (); box.size = MapUtil.Vector3fToVector3(mapTrigger.Size); } else if (mapTrigger.shape == Example.MapTrigger.Shape.CIRCLE) { var sphere = go.AddComponent <SphereCollider> (); sphere.radius = mapTrigger.Radius; } } foreach (var mapEvent in mapEventData.Events) { var go = MapEventHelper.AddChildGameObject(eventRoot, "event-" + (mapEvent.Id)); var eventObj = go.AddComponent <MapEvent> (); eventObj.eventType = (MapEventType)mapEvent.EventType; eventObj.eventId = mapEvent.Id; foreach (var condition in mapEvent.Conditions) { var conditionGO = MapEventHelper.AddChildGameObject(go, "condition"); PrintArgment(conditionGO, condition.Arg2); } foreach (var action in mapEvent.Actions) { var actionGO = MapEventHelper.AddChildGameObject(go, "action-" + action.ActionType); foreach (var arg in action.Args) { var argGO = MapEventHelper.AddChildGameObject(actionGO, "arg"); PrintArgment(argGO, arg); if (argGO.transform.childCount == 0) { GameObject.DestroyImmediate(argGO); } } } } }