using AX.InputSystem; using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CloneDashedLine : CloneSingleObj { public static bool started = false; public static GameObject nowObj; public Vector3 prePos; public Vector3 curPos; public GameObject parentPrefab; public string parentResourcesName; public override void Awake() { if (parentPrefab == null) { parentPrefab = Resources.Load(parentResourcesName) as GameObject; } base.Awake(); } public override void Execute(IMessage obj) { gameObjID = (long)obj.Sender; data = ((CloneCmdArgs)obj.Data); if (data.cloneObjType == cloneObjType) { Clone(); } } public 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); CloneGameObjInfo objMsg = clonedObj.GetComponent(); 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; 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) { Vector3 vertPos; var Distance = Vector3.Distance(prePos, curPos); float pos = Mathf.CeilToInt(Distance); float count = Mathf.CeilToInt(pos / 3); 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), prePos.z + (i / (float)count) * (curPos.z - prePos.z)); GameObject cloneObj = Instantiate(clonePrefab, vertPos, Quaternion.identity) as GameObject; cloneObj.name = nowObj.name; cloneObj.transform.parent = nowObj.transform; //cloneObj.transform.forward = ((curPos - prePos)).normalized; SetDirect(cloneObj); //设定id为父物体id CloneGameObjInfo objMsg = cloneObj.GetComponent(); objMsg.gameObjID = nowObj.GetComponent().gameObjID; 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(cloneObj); SetCloneGameObject(cloneObj); cloneObj.AddComponent(); //添加控制箭头大小 AddRecordEventCloneChild(cloneObj.transform); //记录克隆子物体 } } else { GameObject cloneObj = Instantiate(clonePrefab, curPos, Quaternion.identity) as GameObject; cloneObj.name = nowObj.name; cloneObj.transform.parent = nowObj.transform; cloneObj.transform.forward = ((curPos - prePos)).normalized; //设定id为父物体id CloneGameObjInfo objMsg = cloneObj.GetComponent(); objMsg.gameObjID = nowObj.GetComponent().gameObjID; 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(cloneObj); SetCloneGameObject(cloneObj); cloneObj.AddComponent(); //添加控制箭头大小 AddRecordEventCloneChild(cloneObj.transform); //记录克隆子物体 } } } protected virtual void SetDirect(GameObject cloneObj) { cloneObj.transform.forward = ((curPos - prePos)).normalized; } /// /// 添加初始状态记录 /// /// public override void AddRecordFrame(List list) { foreach (Transform child in transform) { var data = new RecordDashedLine(); //SetBaseData(data, child); SetDashedLineData(data, child); string json = JsonUtility.ToJson(data); var objectJson = new ObjectData(); objectJson.cloneObjType = cloneObjType; objectJson.json = json; list.Add(objectJson); } } public void SetDashedLineData(RecordDashedLine 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; //生成子物体 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); } } /// /// 克隆记录父物体 /// /// private 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 RecordDashedLine(); 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 RecordDashedLine(); 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(); } }