/// <summary>
 /// 加载当前场景的功能交互信息
 /// </summary>
 private void LoadNowAssetsObj()
 {
     if (dataDic.ContainsKey(this.nowSceneName))
     {
         TextAsset nowSceneTextAsset = dataDic[this.nowSceneName];
         string    assetText         = Encoding.UTF8.GetString(nowSceneTextAsset.bytes);
         ActionInteractiveDataInfoCollection actionInteractiveDataInfoCollection = DeSerializeNow <ActionInteractiveDataInfoCollection>(assetText);
         if (actionInteractiveDataInfoCollection != null)
         {
             nowActionInteractiveDataInfoCollection = actionInteractiveDataInfoCollection;
             foreach (ActionInteractiveDataInfo actionInteractiveDataInfo in nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos)
             {
                 if (string.IsNullOrEmpty(actionInteractiveDataInfo.actionInteractivePrefabName))
                 {
                     continue;
                 }
                 if (!prefabDataDic.ContainsKey(actionInteractiveDataInfo.actionInteractivePrefabName))
                 {
                     continue;
                 }
                 actionInteractiveDataInfo.Load(true);
             }
         }
     }
 }
    private void Update()
    {
        Scene nowScene = SceneManager.GetActiveScene();

        if (!string.Equals(nowScene.name, this.nowSceneName))
        {
            this.nowSceneName = nowScene.name;
            //清理之前创建的对象并重新加载新的
            if (nowActionInteractiveDataInfoCollection != null)
            {
                if (nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos != null)
                {
                    foreach (ActionInteractiveDataInfo actionInteractiveDataInfo in nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos)
                    {
                        if (actionInteractiveDataInfo != null)
                        {
                            GameObject.DestroyImmediate(actionInteractiveDataInfo.ActionInteractiveObj);
                        }
                    }
                }
            }
            nowActionInteractiveDataInfoCollection = null;
            LoadNowAssetsObj();
        }
    }
    /// <summary>
    /// 通过场景名获取该场景的所有功能信息交互对象
    /// </summary>
    /// <param name="sceneName"></param>
    /// <returns></returns>
    public ActionInteractiveDataInfo[] GetActionInteractiveDataInfos(string sceneName)
    {
        ActionInteractiveDataInfoCollection actionInteractiveDataInfoCollection = actionInteractiveDataInfoCollections.FirstOrDefault(temp => string.Equals(temp.sceneName, sceneName));

        if (actionInteractiveDataInfoCollection != null && actionInteractiveDataInfoCollection.ActionInteractiveDataInfos != null)
        {
            return(actionInteractiveDataInfoCollection.ActionInteractiveDataInfos.ToArray());
        }
        return(new ActionInteractiveDataInfo[0]);
    }
    /// <summary>
    /// 通过场景名和功能交互对象id获取功能交互对象信息
    /// </summary>
    /// <param name="sceneName"></param>
    /// <param name="ID"></param>
    /// <returns></returns>
    public ActionInteractiveDataInfo GetActionInteractiveDataInfo(string sceneName, int ID)
    {
        ActionInteractiveDataInfoCollection actionInteractiveDataInfoCollection = actionInteractiveDataInfoCollections.FirstOrDefault(temp => string.Equals(temp.sceneName, sceneName));

        if (actionInteractiveDataInfoCollection != null)
        {
            return(actionInteractiveDataInfoCollection.ActionInteractiveDataInfos.FirstOrDefault(temp => temp.ActionInteractiveID == ID));
        }
        return(null);
    }
    public void Load()
    {
        TextAsset[] allTextAssets = Resources.LoadAll <TextAsset>(ActionInteractiveData.dataDirectoryPath);//加载所有场景的功能交互信息
        List <ActionInteractiveDataInfoCollection> actionInteractiveDataInfoCollectionList = new List <ActionInteractiveDataInfoCollection>();

        foreach (TextAsset textAsset in allTextAssets)
        {
            string assetName = textAsset.name;
            string assetText = Encoding.UTF8.GetString(textAsset.bytes);
            ActionInteractiveDataInfoCollection actionInteractiveDataInfoCollection = DeSerializeNow <ActionInteractiveDataInfoCollection>(assetText);
            if (actionInteractiveDataInfoCollection != null)
            {
                actionInteractiveDataInfoCollectionList.Add(actionInteractiveDataInfoCollection);
            }
        }
        actionInteractiveDataInfoCollections = actionInteractiveDataInfoCollectionList.ToArray();
    }
 private void OnDestroy()
 {
     if (nowActionInteractiveDataInfoCollection != null)
     {
         if (nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos != null)
         {
             foreach (ActionInteractiveDataInfo actionInteractiveDataInfo in nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos)
             {
                 if (actionInteractiveDataInfo != null)
                 {
                     GameObject.DestroyImmediate(actionInteractiveDataInfo.ActionInteractiveObj);
                 }
             }
         }
     }
     nowActionInteractiveDataInfoCollection = null;
 }
    private void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        EditorGUILayout.LabelField("路径信息请在ActionInteractiveDataEditor内自行设置");
        EditorGUILayout.LabelField("当前场景:" + nowSceneName);
        if (nowActionInteractiveDataInfoCollection == null)//如果当前场景不存在数据
        {
            if (GUILayout.Button("创建该场景的功能交互信息数据"))
            {
                nowActionInteractiveDataInfoCollection           = new ActionInteractiveDataInfoCollection();
                nowActionInteractiveDataInfoCollection.sceneName = nowSceneName;
                if (File.Exists(dataAllPath + "\\" + nowSceneName + ".txt"))
                {
                    if (EditorUtility.DisplayDialog("警告!", "存在相同的文件" + nowSceneName + ".txt\r\n是否覆盖?", "确认覆盖", "取消覆盖"))
                    {
                        string valueText = SerializeNow <ActionInteractiveDataInfoCollection>(nowActionInteractiveDataInfoCollection);
                        File.WriteAllText(dataAllPath + "\\" + nowSceneName + ".txt", valueText, Encoding.UTF8);
                    }
                    else
                    {
                        nowActionInteractiveDataInfoCollection = null;
                    }
                }
                else
                {
                    File.Create(dataAllPath + "\\" + nowSceneName + ".txt").Close();
                    string valueText = SerializeNow <ActionInteractiveDataInfoCollection>(nowActionInteractiveDataInfoCollection);
                    File.WriteAllText(dataAllPath + "\\" + nowSceneName + ".txt", valueText, Encoding.UTF8);
                }
            }
        }
        else//如果当前场景存在数据
        {
            if (GUILayout.Button("保存数据"))
            {
                if (nowActionInteractiveDataInfoCollection != null && File.Exists(dataAllPath + "\\" + nowActionInteractiveDataInfoCollection.sceneName + ".txt"))
                {
                    foreach (ActionInteractiveDataInfo tempActionInteractiveDataInfo in nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos)
                    {
                        if (tempActionInteractiveDataInfo.ActionInteractiveObj != null)
                        {
                            tempActionInteractiveDataInfo.ActionInteractiveLocation = tempActionInteractiveDataInfo.ActionInteractiveObj.transform.position;
                            tempActionInteractiveDataInfo.ActionInteractiveAngle    = tempActionInteractiveDataInfo.ActionInteractiveObj.transform.eulerAngles;
                        }
                    }
                    string valueText = SerializeNow <ActionInteractiveDataInfoCollection>(nowActionInteractiveDataInfoCollection);
                    File.WriteAllText(dataAllPath + "\\" + nowActionInteractiveDataInfoCollection.sceneName + ".txt", valueText, Encoding.UTF8);
                    EditorUtility.DisplayDialog("保存数据", "保存成功!", "确认");
                }
            }

            scrollPostion = EditorGUILayout.BeginScrollView(scrollPostion);
            ActionInteractiveDataInfo[]      actionInteractiveDataInfos  = nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos.ToArray();
            List <EnumActionInteractiveType> actionInteractiveTypeValues = actionInteractiveTypeToNameList.Select(temp => temp.Key).ToList();
            string[] actionInteractiveTypeExplans = actionInteractiveTypeToNameList.Select(temp => temp.Value).ToArray();
            foreach (ActionInteractiveDataInfo actionInteractiveDataInfo in actionInteractiveDataInfos)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("ID:", GUILayout.Width(20));
                actionInteractiveDataInfo.ActionInteractiveID = EditorGUILayout.IntField(actionInteractiveDataInfo.ActionInteractiveID, GUILayout.Width(30));
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Name:", GUILayout.Width(35));
                actionInteractiveDataInfo.ActionInteractiveName = EditorGUILayout.TextField(actionInteractiveDataInfo.ActionInteractiveName, GUILayout.Width(100));
                if (actionInteractiveDataInfo.ActionInteractiveObj)
                {
                    actionInteractiveDataInfo.ActionInteractiveObj.name = actionInteractiveDataInfo.ActionInteractiveName;
                }
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Target:", GUILayout.Width(40));
                if (!string.IsNullOrEmpty(actionInteractiveDataInfo.actionInteractivePrefabName) && prefabDataDic.ContainsKey(actionInteractiveDataInfo.actionInteractivePrefabName))//如果存在该预设提则实例化并显示
                {
                    EditorGUILayout.ObjectField(actionInteractiveDataInfo.ActionInteractiveObj, typeof(GameObject), true, GUILayout.Width(100));
                    if (GUILayout.Button("×", GUILayout.Width(25)))
                    {
                        if (EditorUtility.DisplayDialog("警告!", "是否删除该游戏对象(重新选择预设提)", "是", "否"))
                        {
                            actionInteractiveDataInfo.actionInteractivePrefabName = "";
                            if (actionInteractiveDataInfo.ActionInteractiveObj != null)
                            {
                                GameObject.DestroyImmediate(actionInteractiveDataInfo.ActionInteractiveObj);
                            }
                            actionInteractiveDataInfo.ActionInteractiveObj = null;
                            actionInteractiveDataInfo.Load(true);
                        }
                    }
                }
                else
                {
                    List <string> names = prefabDataDic.Keys.ToList();
                    int           index = names.IndexOf(actionInteractiveDataInfo.actionInteractivePrefabName);
                    index = EditorGUILayout.Popup(index, names.ToArray(), GUILayout.Width(100));
                    if (index >= 0)
                    {
                        actionInteractiveDataInfo.actionInteractivePrefabName = names[index];
                        actionInteractiveDataInfo.Load(true);
                    }
                }
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("DataType:", GUILayout.Width(50));
                int actionInteractiveTypeIndex  = actionInteractiveTypeValues.IndexOf(actionInteractiveDataInfo.ActionInteractiveType);
                int _actionInteractiveTypeIndex = EditorGUILayout.Popup(actionInteractiveTypeIndex, actionInteractiveTypeExplans, GUILayout.Width(120));
                if (_actionInteractiveTypeIndex >= 0 && _actionInteractiveTypeIndex != actionInteractiveTypeIndex)
                {
                    actionInteractiveDataInfo.ActionInteractiveType = actionInteractiveTypeValues[_actionInteractiveTypeIndex];
                    switch (actionInteractiveDataInfo.ActionInteractiveType)
                    {
                    case EnumActionInteractiveType.None:
                        actionInteractiveDataInfo.OtherValue = null;
                        break;

                    case EnumActionInteractiveType.TreasureBox:
                        actionInteractiveDataInfo.OtherValue = new Dictionary <EnumGoodsType, ActionInteractiveDataInfo.TreasureBoxStruct>();
                        break;

                    case EnumActionInteractiveType.Other:
                        actionInteractiveDataInfo.OtherValue = new ActionInteractiveDataInfo.OtherDataStruct();
                        break;
                    }
                }
                switch (actionInteractiveDataInfo.ActionInteractiveType)
                {
                case EnumActionInteractiveType.TreasureBox:
                case EnumActionInteractiveType.Other:
                    if (GUILayout.Button("编辑", GUILayout.Width(35)))
                    {
                        ActionInteractiveDataStructEditor actionInteractiveDataStructEditor = EditorWindow.GetWindow <ActionInteractiveDataStructEditor>();
                        actionInteractiveDataStructEditor.Show();
                        actionInteractiveDataStructEditor.actionInteractiveDataInfo = actionInteractiveDataInfo;
                    }
                    break;
                }
                EditorGUILayout.Space();
                if (GUILayout.Button("删除"))
                {
                    if (EditorUtility.DisplayDialog("警告!", "请再次确认是否删除?", "确认", "取消"))
                    {
                        if (actionInteractiveDataInfo != null)
                        {
                            if (actionInteractiveDataInfo.ActionInteractiveObj != null)
                            {
                                GameObject.DestroyImmediate(actionInteractiveDataInfo.ActionInteractiveObj);
                            }
                            nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos.Remove(actionInteractiveDataInfo);
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndScrollView();
            if (GUILayout.Button("添加"))
            {
                nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos.Add(
                    new ActionInteractiveDataInfo()
                {
                    ActionInteractiveID = nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos.Count() > 0 ? (nowActionInteractiveDataInfoCollection.ActionInteractiveDataInfos.Max(temp => temp.ActionInteractiveID) + 1) : 0,
                    SceneName           = nowActionInteractiveDataInfoCollection.sceneName
                });
            }
        }
        EditorGUILayout.EndHorizontal();
    }