using AX.InputSystem; using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CloneMultiLine : CloneSingleObj { protected Vector3 prePos; protected Vector3 curPos; protected Vector3 midPos; protected float variableX = 100; protected float variableY = 50; protected float variableZ = 80; public static bool started = false; public GameObject parentPrefab; public string parentResourcesName; public static GameObject nowObj; public override void Awake() { if (parentPrefab == null) { parentPrefab = Resources.Load(parentResourcesName) as GameObject; } base.Awake(); } protected virtual void CreateParentPrefab() { parentPrefab = new GameObject(cloneObjType.ToString()); parentPrefab.AddComponent(); parentPrefab.AddComponent(); } protected override void Clone() { var hitPoint = data.hitPos; if (!started) { prePos = hitPoint; started = true; //克隆父物体 var clonedObj = EntitiesManager.Instance.CreateObj(parentPrefab, prePos, transform, gameObjID); clonedObj.name = gameObjID.ToString(); SelectedObjs.gameObjs.Add(clonedObj); //设置克隆物体所在楼层属性,属性从点击的对象上获取 var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID); CloneGameObjInfo floorMsg = hitObj.GetComponent(); CloneGameObjInfo objMsg = clonedObj.GetComponent(); objMsg.gameObjType = cloneObjType; objMsg.buildNum = floorMsg.buildNum; objMsg.floorNum = floorMsg.floorNum; objMsg.interlayerNum = floorMsg.interlayerNum; SetCloneGameObject(clonedObj); nowObj = clonedObj; AddRecordEventCloneParent(clonedObj.transform);//记录克隆父物体 } else { curPos = hitPoint; //如果两点不重合 if (curPos.x != prePos.x && curPos.x != prePos.x) { //克隆子物体 DrawLine(prePos, curPos, nowObj); } prePos = curPos; } } protected virtual 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.name = nowObj.name; clonedObj.transform.parent = nowObj.transform; SetClonedObj(clonedObj); CloneGameObjInfo objMsg = clonedObj.GetComponent(); objMsg.gameObjID = nowObj.GetComponent().gameObjID; //子物体的id同父物体 objMsg.gameObjType = cloneObjType; //设置克隆物体所在楼层属性,属性从点击的对象上获取 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); SetCloneGameObject(clonedObj); AddRecordEventCloneChild(clonedObj.transform); //记录克隆子物体 } protected virtual void SetClonedObj(GameObject clonedObj) { clonedObj.transform.up = (curPos - prePos).normalized;//改变线条的朝向 float distance = Vector3.Distance(prePos, curPos);//计算两点的距离 clonedObj.transform.localScale = new Vector3(variableX, distance * variableY, variableZ);//延长线条,连接两点。 } /// /// 添加初始状态记录 /// /// public override void AddRecordFrame(List list) { foreach (Transform child in transform) { var data = new RecordMultiLine(); //SetBaseData(data, child); SetMultiLineData(data, child); data.isEvent = false; string json = JsonUtility.ToJson(data); var objectJson = new ObjectData(); objectJson.cloneObjType = cloneObjType; objectJson.json = json; list.Add(objectJson); } } /// /// 添加初始状态记录时填充数据 /// /// /// public void SetMultiLineData(RecordMultiLine data, Transform obj) { var msg = obj.GetComponent(); data.gameObjType = msg.gameObjType; data.objectName = obj.gameObject.name; data.buildNum = msg.buildNum; data.floorNum = msg.floorNum; data.interlayerNum = msg.interlayerNum; data.myTransform.setMyPosition(obj.localPosition); data.myTransform.setMyRotation(obj.localRotation); data.myTransform.setMyScale(obj.localScale); data.isActive = obj.gameObject.activeSelf; data.isEvent = false; //生成子物体 for(int i = 0; i < obj.childCount; i++) { RecordObjectBase childData = new RecordObjectBase(); Transform childTransform = obj.GetChild(i); CloneGameObjInfo childMsg = childTransform.GetComponent(); childData.gameObjType = childMsg.gameObjType; childData.objectName = childTransform.gameObject.name; childData.buildNum = childMsg.buildNum; childData.floorNum = childMsg.floorNum; childData.interlayerNum = childMsg.interlayerNum; childData.myTransform.setMyPosition(childTransform.localPosition); childData.myTransform.setMyRotation(childTransform.localRotation); childData.myTransform.setMyScale(childTransform.localScale); data.list.Add(childData); } } /// /// 克隆记录父物体 /// /// public virtual 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 RecordMultiLine(); SetBaseData(data, obj); data.isChild = false; data.isEvent = true; string json = JsonUtility.ToJson(data); eventData.json = json; RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } /// /// 克隆记录子物体 /// /// public virtual 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 RecordMultiLine(); SetBaseData(data, obj); data.isChild = true; //记录是否为子物体 data.isEvent = true; string json = JsonUtility.ToJson(data); eventData.json = json; RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } public override void SetCloneGameObject(GameObject obj) { obj.AddComponent(); obj.AddComponent(); } }