using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class ReplayCurtainLine : ReplaySimpleObject { protected CloneCurtainLine cloneCurtainLine; public override void Start() { cloneCurtainLine = GetComponent(); base.Start(); } /// /// 回放克隆 /// /// public override void CloneObject(string json) { var jsonData = JsonUtility.FromJson(json); if (jsonData.isEvent == false) { var data= JsonUtility.FromJson(json); ReplayFrame(data); } else { if (jsonData.isChild) { var data = JsonUtility.FromJson(json); ReplayEventCloneChild(data); } else { var data= JsonUtility.FromJson(json); ReplayEventCloneParent(data); } } } /// /// 回放事件:克隆父物体 /// /// private void ReplayEventCloneParent(RecordCurtainLineParent data) { var parentObj = EntitiesManager.Instance.CreateObj(cloneCurtainLine.parentPrefab, data.myTransform.getMyPosition(), transform, long.Parse(data.objectName)); SetBaseGameObjectData(parentObj, data); parentObj.AddComponent(); parentObj.AddComponent(); parentObj.AddComponent(); } private void SetBaseGameObjectData(GameObject obj, RecordCurtainLineParent data) { obj.name = data.objectName; obj.transform.localRotation = data.myTransform.getMyRotation(); obj.transform.localPosition = data.myTransform.getMyPosition(); obj.transform.localScale = data.myTransform.getMyScale(); SelectedObjs.gameObjs.Add(obj); CloneGameObjInfo objMsg = obj.GetComponent(); objMsg.gameObjType = data.gameObjType; objMsg.buildNum = data.buildNum; objMsg.floorNum = data.floorNum; objMsg.interlayerNum = data.interlayerNum; obj.SetActive(data.isActive); } /// /// 回放事件:克隆子物体 /// /// private void ReplayEventCloneChild(RecordCurtainLineChild data) { Transform parent = transform.Find(data.objectName); var clonedChild = Instantiate(cloneCurtainLine.clonePrefab, parent); clonedChild.transform.localPosition = data.myTransform.getMyPosition(); clonedChild.name = data.objectName; clonedChild.transform.localRotation = data.myTransform.getMyRotation(); clonedChild.transform.localScale = data.myTransform.getMyScale(); CurtainLineItem objMsg = clonedChild.GetComponent(); objMsg.gameObjID = parent.GetComponent().gameObjID; objMsg.gameObjType = data.gameObjType; objMsg.buildNum = data.buildNum; objMsg.floorNum = data.floorNum; objMsg.interlayerNum = data.interlayerNum; objMsg.prePos = data.prePos; objMsg.curPos = data.curPos; CreateCurtain(objMsg.prePos, objMsg.curPos,parent); SelectedObjs.gameObjs.Add(clonedChild); } /// /// 回放状态 /// /// protected void ReplayFrame(RecordCurtainLineParent jsonData) { var parentObj = EntitiesManager.Instance.CreateObj(cloneCurtainLine.parentPrefab, jsonData.myTransform.getMyPosition(), transform, long.Parse(jsonData.objectName)); parentObj.name = jsonData.objectName;//名字即ID SetBaseGameObject(parentObj, jsonData); parentObj.AddComponent(); parentObj.AddComponent(); parentObj.AddComponent(); //生成子物体 var list = jsonData.list; foreach (RecordCurtainLineChild childData in list) { var clonedChild = Instantiate(cloneCurtainLine.clonePrefab, parentObj.transform); clonedChild.transform.localPosition = childData.myTransform.getMyPosition(); clonedChild.name = childData.objectName; clonedChild.transform.localRotation = childData.myTransform.getMyRotation(); clonedChild.transform.localScale = childData.myTransform.getMyScale(); SelectedObjs.gameObjs.Add(clonedChild); CurtainLineItem objMsg = clonedChild.GetComponent(); objMsg.gameObjID = parentObj.GetComponent().gameObjID; objMsg.gameObjType = childData.gameObjType; objMsg.buildNum = childData.buildNum; objMsg.floorNum = childData.floorNum; objMsg.interlayerNum = childData.interlayerNum; objMsg.prePos = childData.prePos; objMsg.curPos = childData.curPos; CreateCurtain(objMsg.prePos, objMsg.curPos, parentObj.transform); //生成该段水幕 } parentObj.GetComponent().ReplayTask(jsonData.task); } /// /// 生成水幕 /// /// /// 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(cloneCurtainLine.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 = parent.GetComponent().gameObjID; CloneGameObjInfo floorMsg = parent.GetComponent(); curtainCtrl.buildNum = floorMsg.buildNum; curtainCtrl.floorNum = floorMsg.floorNum; curtainCtrl.interlayerNum = floorMsg.interlayerNum; } } } } }