using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CloneCurtainLine : CloneMultiLine { private string curtainResourcesName; public GameObject curtainPrefab; //水幕预设 public override void Awake() { variableX = 10; variableY = 18; variableZ = 10; Height = 0.2f; cloneObjType = CloneObjType.CurtainLine; parentResourcesName = "Prefab/Tool/CurtainLineParent"; ResourcesName = "Prefab/Tool/CurtainLineItem"; curtainResourcesName = "Prefab/Tool/WaterCurtain"; if (parentPrefab == null) { parentPrefab = Resources.Load(parentResourcesName) as GameObject; } if (clonePrefab == null) { clonePrefab = Resources.Load(ResourcesName) as GameObject; } if (curtainPrefab == null) { curtainPrefab = Resources.Load(curtainResourcesName) as GameObject; } } protected override void Clone() { var hitPoint = data.hitPos; if (!started) { prePos = hitPoint; started = true; //克隆父物体 var parentObj = EntitiesManager.Instance.CreateObj(parentPrefab, prePos, transform, gameObjID); parentObj.name = gameObjID.ToString(); SelectedObjs.gameObjs.Add(parentObj); //设置克隆物体所在楼层属性,属性从点击的对象上获取 var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); CloneGameObjInfo objMsg = parentObj.GetComponent(); objMsg.gameObjType = cloneObjType; if (hitObj.GetComponent()) { CloneGameObjInfo floorMsg = hitObj.GetComponent(); objMsg.buildNum = floorMsg.buildNum; objMsg.floorNum = floorMsg.floorNum; objMsg.interlayerNum = floorMsg.interlayerNum; } parentObj.AddComponent(); parentObj.AddComponent(); parentObj.AddComponent(); nowObj = parentObj; AddRecordEventCloneParent(parentObj.transform); //记录克隆父物体 } else { curPos = hitPoint; //如果两点不重合 if (curPos.x != prePos.x && curPos.x != prePos.x) { //克隆子物体 DrawLine(prePos, curPos, nowObj); } prePos = curPos; } } protected override void DrawLine(Vector3 prePos, Vector3 curPos, GameObject nowObj) { midPos = (prePos + curPos) / 2; Vector3 clonedObjPos = new Vector3(midPos.x, midPos.y + Height, midPos.z); var clonedObj = Instantiate(clonePrefab, clonedObjPos, Quaternion.identity) as GameObject; clonedObj.transform.parent = nowObj.transform; clonedObj.name = nowObj.name; SetClonedObj(clonedObj); CreateCurtain(prePos, curPos, nowObj.transform); CurtainLineItem objMsg = clonedObj.GetComponent(); objMsg.gameObjID = nowObj.GetComponent().gameObjID; objMsg.gameObjType = cloneObjType; objMsg.prePos = prePos; objMsg.curPos = curPos; //设置克隆物体所在楼层属性,属性从点击的对象上获取 var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); CloneGameObjInfo floorMsg = hitObj.GetComponent(); objMsg.buildNum = floorMsg.buildNum; objMsg.floorNum = floorMsg.floorNum; objMsg.interlayerNum = floorMsg.interlayerNum; SelectedObjs.gameObjs.Add(clonedObj); AddRecordEventCloneChild(clonedObj.transform);//记录克隆子物体 } /// /// 生成水幕 /// /// /// private void CreateCurtain(Vector3 prePos, Vector3 curPos,Transform parent) { Vector3 vertPos; var Distance = Vector3.Distance(prePos, curPos); float pos = Mathf.CeilToInt(Distance); float count = Mathf.CeilToInt(pos / 2); int dex = 1; if (count < 1) { count = 1; dex = 0; } for (int i = 0; i < count + dex; i++) { if (count > 1) { if (i != 0) { vertPos = new Vector3(prePos.x + (i / (float)count) * (curPos.x - prePos.x), prePos.y + (i / (float)count) * (curPos.y - prePos.y) + 0.6f, prePos.z + (i / (float)count) * (curPos.z - prePos.z)); GameObject curtain = Instantiate(curtainPrefab, vertPos, Quaternion.identity) as GameObject; curtain.transform.parent = parent; curtain.transform.forward = -Vector3.up; curtain.name = "Curtain-" + parent.name; var curtainCtrl=curtain.AddComponent(); curtainCtrl.gameObjID = long.Parse(nowObj.name); CloneGameObjInfo floorMsg = parent.GetComponent(); curtainCtrl.buildNum = floorMsg.buildNum; curtainCtrl.floorNum = floorMsg.floorNum; curtainCtrl.interlayerNum = floorMsg.interlayerNum; } } } } /// /// 添加初始状态记录 /// /// public override void AddRecordFrame(List list) { foreach (Transform child in transform) { var data = new RecordCurtainLineParent(); SetBaseData(data, child); data.isEvent = false; for(int i = 0; i < child.childCount; i++) { var item = child.GetChild(i); if (!item.name.Contains("Curtain")) { CurtainLineItem itemMsg = item.GetComponent(); var itemData = new RecordCurtainLineChild(); SetBaseData(itemData, item.transform); itemData.prePos = itemMsg.prePos; itemData.curPos = itemMsg.curPos; data.list.Add(itemData); } } data.task = child.GetComponent().task; string json = JsonUtility.ToJson(data); var objectJson = new ObjectData(); objectJson.cloneObjType = cloneObjType; objectJson.json = json; list.Add(objectJson); } } /// /// 克隆记录父物体 /// /// public override void AddRecordEventCloneParent(Transform obj) { if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) { var eventData = new EventData(); eventData.time = RecordManager.Instance.RecordTimer; eventData.cloneObjType = cloneObjType; eventData.eventType = RecordEventType.Clone; var data = new RecordCurtainLineParent(); SetBaseData(data, obj); data.isEvent = true; data.isChild = false; string json = JsonUtility.ToJson(data); eventData.json = json; RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } /// /// 克隆记录子物体 /// /// public override void AddRecordEventCloneChild(Transform obj) { if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) { var eventData = new EventData(); eventData.time = RecordManager.Instance.RecordTimer; eventData.cloneObjType = cloneObjType; eventData.eventType = RecordEventType.Clone; var data = new RecordCurtainLineChild(); SetBaseData(data, obj); data.isEvent = true; data.isChild = true; data.prePos = obj.GetComponent().prePos; data.curPos = obj.GetComponent().curPos; string json = JsonUtility.ToJson(data); eventData.json = json; RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } } [Serializable] public class RecordCurtainLine:RecordObjectBase { public bool isChild; public bool isEvent; } [Serializable] public class RecordCurtainLineChild: RecordCurtainLine { public Vector3 prePos; public Vector3 curPos; } [Serializable] public class RecordCurtainLineParent : RecordCurtainLine { public List list = new List(); public string task; }