public void Load() { TextAsset[] allTextAssets = Resources.LoadAll <TextAsset>(NPCData.dataDirectoryPath);//加载所有场景的npc信息 List <NPCDataInfoCollection> npcDataInfoCollectionList = new List <NPCDataInfoCollection>(); foreach (TextAsset textAsset in allTextAssets) { string assetName = textAsset.name; string assetText = Encoding.UTF8.GetString(textAsset.bytes); NPCDataInfoCollection npcDataInfoCollection = DeSerializeNow <NPCDataInfoCollection>(assetText); if (npcDataInfoCollection != null) { npcDataInfoCollectionList.Add(npcDataInfoCollection); } } npcDataInfoCollections = npcDataInfoCollectionList.ToArray(); //重新设置一遍他们各自所在的场景 foreach (NPCDataInfoCollection npcDataInfoCollection in npcDataInfoCollections) { foreach (NPCDataInfo npcDataInfo in npcDataInfoCollection.NPCDataInfos) { if (string.IsNullOrEmpty(npcDataInfo.SceneName)) { npcDataInfo.SceneName = npcDataInfoCollection.sceneName; } } } }
/// <summary> /// 加载当前场景的NPC对象 /// </summary> private void LoadNowAssetObj() { if (dataDic.ContainsKey(this.nowSceneName)) { TextAsset nowSceneTextAsset = dataDic[this.nowSceneName]; string assetText = Encoding.UTF8.GetString(nowSceneTextAsset.bytes); NPCDataInfoCollection npcDataInfoCollection = DeSerializeNow <NPCDataInfoCollection>(assetText); if (npcDataInfoCollection != null) { nowNPCDataInfoCollection = npcDataInfoCollection; foreach (NPCDataInfo npcDataInfo in nowNPCDataInfoCollection.NPCDataInfos) { if (string.IsNullOrEmpty(npcDataInfo.npcPrefabName)) { continue; } if (!npcDataDic.ContainsKey(npcDataInfo.npcPrefabName)) { continue; } npcDataInfo.Load(); } } } }
/// <summary> /// 通过npc id获取npc信息 /// </summary> /// <param name="npcID">npc id</param> /// <returns></returns> public NPCDataInfo GetNPCDataInfo(string sceneName, int npcID) { NPCDataInfoCollection npcDataInfoCollection = npcDataInfoCollections.FirstOrDefault(temp => string.Equals(temp.sceneName, sceneName)); if (npcDataInfoCollection != null) { return(npcDataInfoCollection.NPCDataInfos.Where(temp => temp.NPCID == npcID).FirstOrDefault()); } return(null); }
/// <summary> /// 通过场景名获取该场景的所有NPC信息 /// </summary> /// <param name="sceneName"></param> /// <returns></returns> public NPCDataInfo[] GetNPCDataInfos(string sceneName) { NPCDataInfoCollection npcDataInfoCollection = npcDataInfoCollections.FirstOrDefault(temp => string.Equals(temp.sceneName, sceneName)); if (npcDataInfoCollection != null && npcDataInfoCollection.NPCDataInfos != null) { return(npcDataInfoCollection.NPCDataInfos.ToArray()); } return(new NPCDataInfo[0]); }
private void OnDestroy() { if (nowNPCDataInfoCollection != null) { if (nowNPCDataInfoCollection.NPCDataInfos != null) { foreach (NPCDataInfo npcDataInfo in nowNPCDataInfoCollection.NPCDataInfos) { if (npcDataInfo != null) { GameObject.DestroyImmediate(npcDataInfo.NPCObj); } } } } nowNPCDataInfoCollection = null; }
private void Update() { Scene nowScene = SceneManager.GetActiveScene(); if (!string.Equals(nowScene.name, this.nowSceneName)) { this.nowSceneName = nowScene.name; //清理之前创建的npc对象并重新加载新的 if (nowNPCDataInfoCollection != null) { foreach (NPCDataInfo npcDataInfo in nowNPCDataInfoCollection.NPCDataInfos) { if (npcDataInfo != null) { GameObject.DestroyImmediate(npcDataInfo.NPCObj); } } } nowNPCDataInfoCollection = null; LoadNowAssetObj(); } }
void OnGUI() { EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("路径信息请在NPCManagerEditor内自行设置"); EditorGUILayout.LabelField("当前场景:" + nowSceneName); if (nowNPCDataInfoCollection == null)//如果当前场景不存在数据 { if (GUILayout.Button("创建该场景的NPC信息数据")) { nowNPCDataInfoCollection = new NPCDataInfoCollection(); nowNPCDataInfoCollection.sceneName = nowSceneName; if (File.Exists(dataAllPath + "\\" + nowSceneName + ".txt")) { if (EditorUtility.DisplayDialog("警告!", "存在相同的文件" + nowSceneName + ".txt\r\n是否覆盖?", "确认覆盖", "取消覆盖")) { string valueText = SerializeNow <NPCDataInfoCollection>(nowNPCDataInfoCollection); File.WriteAllText(dataAllPath + "\\" + nowSceneName + ".txt", valueText, Encoding.UTF8); } else { nowNPCDataInfoCollection = null; } } else { File.Create(dataAllPath + "\\" + nowSceneName + ".txt").Close(); string valueText = SerializeNow <NPCDataInfoCollection>(nowNPCDataInfoCollection); File.WriteAllText(dataAllPath + "\\" + nowSceneName + ".txt", valueText, Encoding.UTF8); } } } else//如果当前场景存在数据 { if (showNextWindow) { EditorGUILayout.LabelField("请先关闭(处理完成)弹出窗体后再继续"); } if (GUILayout.Button("保存数据") && !showNextWindow) { if (nowNPCDataInfoCollection != null && File.Exists(dataAllPath + "\\" + nowNPCDataInfoCollection.sceneName + ".txt")) { foreach (NPCDataInfo tempNPCDataInfo in nowNPCDataInfoCollection.NPCDataInfos) { if (tempNPCDataInfo.NPCObj != null) { tempNPCDataInfo.NPCLocation = tempNPCDataInfo.NPCObj.transform.position; tempNPCDataInfo.NPCAngle = tempNPCDataInfo.NPCObj.transform.eulerAngles; } } string valueText = SerializeNow <NPCDataInfoCollection>(nowNPCDataInfoCollection); File.WriteAllText(dataAllPath + "\\" + nowNPCDataInfoCollection.sceneName + ".txt", valueText, Encoding.UTF8); EditorUtility.DisplayDialog("保存数据", "保存成功!", "确认"); } } scrollPostion = EditorGUILayout.BeginScrollView(scrollPostion); NPCDataInfo[] npcDataInfos = nowNPCDataInfoCollection.NPCDataInfos.ToArray(); foreach (NPCDataInfo npcDataInfo in npcDataInfos) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("ID:", GUILayout.Width(20)); npcDataInfo.NPCID = EditorGUILayout.IntField(npcDataInfo.NPCID, GUILayout.Width(30)); EditorGUILayout.Space(); EditorGUILayout.LabelField("Name:", GUILayout.Width(30)); npcDataInfo.NPCName = EditorGUILayout.TextField(npcDataInfo.NPCName); EditorGUILayout.Space(); EditorGUILayout.LabelField("Target:", GUILayout.Width(30)); EditorGUILayout.ObjectField(npcDataInfo.NPCObj, typeof(GameObject), true); EditorGUILayout.Space(); if (GUILayout.Button("编辑") && !showNextWindow) { EditorNPCDataInfoWindow changeNPCDataInfoWindow = EditorWindow.GetWindow <EditorNPCDataInfoWindow>(true, "修改NPC信息", true); showNextWindow = true; changeNPCDataInfoWindow.ResultHandle += (_npcDataInfo) => { showNextWindow = false; }; changeNPCDataInfoWindow.npcDataDic = npcDataDic; changeNPCDataInfoWindow.nowIDList = nowNPCDataInfoCollection.NPCDataInfos.Select(temp => temp.NPCID).Where(temp => temp != npcDataInfo.NPCID).ToList(); changeNPCDataInfoWindow.SetNPCDataInfo(npcDataInfo); changeNPCDataInfoWindow.Show(); } if (GUILayout.Button("删除") && !showNextWindow) { if (EditorUtility.DisplayDialog("警告!", "请再次确认是否删除?", "确认删除", "取消删除")) { if (npcDataInfo != null) { if (npcDataInfo.NPCObj != null) { GameObject.DestroyImmediate(npcDataInfo.NPCObj); } nowNPCDataInfoCollection.NPCDataInfos.Remove(npcDataInfo); } } } EditorGUILayout.EndHorizontal(); } if (GUILayout.Button("添加") && !showNextWindow) { EditorNPCDataInfoWindow newNPCDataInfoWindow = EditorWindow.GetWindow <EditorNPCDataInfoWindow>(true, "添加NPC信息", true); showNextWindow = true; NPCDataInfoCollection tempNowNpcDataInfoCollection = nowNPCDataInfoCollection; newNPCDataInfoWindow.ResultHandle += (npcDataInfo) => { showNextWindow = false; if (npcDataInfo != null && object.Equals(tempNowNpcDataInfoCollection, nowNPCDataInfoCollection)) { tempNowNpcDataInfoCollection.NPCDataInfos.Add(npcDataInfo); } }; newNPCDataInfoWindow.npcDataDic = npcDataDic; newNPCDataInfoWindow.nowIDList = nowNPCDataInfoCollection.NPCDataInfos.Select(temp => temp.NPCID).ToList(); newNPCDataInfoWindow.Show(); } EditorGUILayout.EndScrollView(); } EditorGUILayout.EndVertical(); }