Ejemplo n.º 1
0
        /// <summary>
        /// 删除Asset
        /// </summary>
        /// <param name="groupAsset"></param>
        public static void DeleteGroupAsset(MapGroupAsset groupAsset)
        {
            //先卸载所有已经存在场上的Group?
            //可能会有多余场景残留?
            //姑且先解绑prefab
            foreach (MapGroup group in MapSystem.groupList)
            {
                if (group != null && group.groupName == groupAsset.groupName)
                {
                    PrefabUtility.DisconnectPrefabInstance(group.gameObject);

                    group.dirty = true;
                    //转移,删除场景
                    if (group.transform.parent == null)
                    {
                        Scene toDelete = group.gameObject.scene;
                        group.transform.SetParent(MapSystem.mapSystemComponent.transform);
                        EditorSceneManager.CloseScene(toDelete, true);
                    }
                }
            }

            //取消注册
            MapGroupAssets.Remove(groupAsset);

            //删除文件夹
            AssetDatabase.DeleteAsset(AssetPath + groupAsset.groupName + ".asset");
            //AssetDatabase.DeleteAsset(AssetPath + groupAsset.groupName);

            AssetDatabase.MoveAssetToTrash(AssetPath + groupAsset.groupName);

            EditorUtility.SetDirty(EditorMatrix.Prefs);
            AssetDatabase.SaveAssets();
        }
Ejemplo n.º 2
0
        private void OnGroupAssetGUI()
        {
            if (target == null)
            {
                FocusOn(null, SelectionType.Map);
                return;
            }
            MapGroupAsset groupAsset = target as MapGroupAsset;

            DrawPreview();
            GUILayout.Label(groupAsset.groupName, Prefs.nameStyle);
            groupAsset.information = EditorGUILayout.TextArea(groupAsset.information, Prefs.informationStyle);
            if (GUILayout.Button("", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true)))
            {
                GUI.FocusControl("");
            }
            GUILayout.BeginHorizontal(Prefs.mapAssetBackgroundStyle, GUILayout.Height(28));
            {
                GUILayout.FlexibleSpace();
                if (!groupAsset.baked && GUILayout.Button("烘焙", GUILayout.Width(60)))
                {
                    MapGroupAssetEditor.BakeGroupAsset(groupAsset);
                }
                if (GUILayout.Button("加载组", GUILayout.Width(60)))
                {
                    MapGroupAssetEditor.LoadGroup(MapAssetManager.CurrentAsset, MapSystem.currentGroupIndex); FocusOn(MapSystem.groupList[MapSystem.currentGroupIndex], SelectionType.Group);
                }
            }
            GUILayout.EndHorizontal();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 生成烘焙数据
        /// </summary>
        public static void BakeGroupAsset(MapGroupAsset groupAsset)
        {
            //TODO:设置实时光照配置
            //保护现场
            SceneSetup[] sceneSetup = EditorSceneManager.GetSceneManagerSetup();

            //Setting
            for (int i = 0; i < 3; i++)
            {
                Scene s = EditorSceneManager.OpenScene(GetScenePath(groupAsset, i), OpenSceneMode.Single);
                {
                    Lightmapping.bakedGI    = false;
                    Lightmapping.realtimeGI = true;
                    LightmapEditorSettings.realtimeResolution = 4;
                }
                EditorSceneManager.SaveScene(s);
            }

            //恢复现场
            EditorSceneManager.RestoreSceneManagerSetup(sceneSetup);

            //Bake
            string[] paths = { GetScenePath(groupAsset, 0), GetScenePath(groupAsset, 1), GetScenePath(groupAsset, 2) };
            Lightmapping.BakeMultipleScenes(paths); //这个API是异步的!

            groupAsset.baked = true;
        }
Ejemplo n.º 4
0
        private static void LoadGroupDirect(MapGroupAsset asset, int index)
        {
            Scene    newScene = EditorSceneManager.OpenScene(GetScenePath(asset, index), OpenSceneMode.Additive);
            MapGroup newGroup = newScene.GetRootGameObjects()[0].GetComponent <MapGroup>();

            newGroup.index             = index;
            MapSystem.groupList[index] = newGroup;
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 加载一个Group到指定位置
 /// </summary>
 public static void LoadGroup(MapGroupAsset asset, int index)
 {
     if (groupList[index] != null && !UnLoadGroup(groupList[index]))
     {
         return;
     }
     LoadGroupDirect(asset, index);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 保存到Asset中
        /// </summary>
        public static void SaveToAsset(MapGroup group, MapGroupAsset asset)
        {
            int index = group.index;

            group.index = -1;
            Quaternion rot = group.transform.rotation;

            group.transform.rotation = Quaternion.identity;
            PrefabUtility.ReplacePrefab(group.gameObject, asset.groupPrefab, ReplacePrefabOptions.ConnectToPrefab);
            asset.baked = false;
            group.transform.rotation = rot;
            group.index = index;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 加载Group,返回加载进度
        /// </summary>
        /// <returns>返回加载进度</returns>
        public static AsyncOperation LoadGroup(MapGroupAsset group, int index)
        {
#if UNITY_EDITOR
            if (groupList[index] != null)
            {
                Debug.LogError("LoadGroup前请先UnloadGroup!");
                UnLoadGroup(groupList[index]);
            }
#endif

            loaded <<= 1;
            string sceneName   = group.groupName + index % 3;
            Scene  activeScene = SceneManager.GetActiveScene();

            AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

            StartCoroutine(loadGroup(operation, index, sceneName, activeScene));

            return(operation);
        }
Ejemplo n.º 8
0
        private void OnGUI()
        {
            toolbarSelected = GUILayout.Toolbar(toolbarSelected, toolbarTitle, Prefs.toolbarStyle);

            if (toolbarSelected == 0)
            {
                //物体

                if (PrefabInformation.Count == 0)
                {
                    //大小为空
                    GUILayout.Label("……", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true));
                }
                else
                {
                    //selection list
                    scollVector = GUILayout.BeginScrollView(scollVector);
                    {
                        List <string> assetNameList = new List <string>();
                        for (int i = 0; i < PrefabInformation.Count; i++)
                        {
                            assetNameList.Add(PrefabInformation.Keys[i].name);
                        }
                        EditorGUI.BeginChangeCheck();
                        prefabSelected = GUILayout.SelectionGrid(prefabSelected, assetNameList.ToArray(), 1, Prefs.mapAssetBackgroundStyle, GUILayout.Height(27 * PrefabInformation.Count));
                        if (EditorGUI.EndChangeCheck())
                        {
                            MapInspector.FocusOn(CurrentPrefab, MapInspector.SelectionType.Prefab);
                        }

                        GUILayout.Label("", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true));
                    }
                    GUILayout.EndScrollView();

                    //function
                    GameObject cp = CurrentPrefab;
                    GUILayout.BeginHorizontal(Prefs.mapAssetBackgroundStyle, GUILayout.Height(28));
                    {
                        GUILayout.FlexibleSpace();
                        if (GUILayout.Button("删除", GUILayout.Width(60)))
                        {
                            DeletePrefab(cp); prefabSelected--; MapInspector.FocusOn(CurrentPrefab, MapInspector.SelectionType.Prefab);
                        }
                    }
                    GUILayout.EndHorizontal();


                    //drag the prefab out
                    switch (Event.current.type)
                    {
                    case EventType.MouseDown:
                        tempPrefabInstance = PrefabUtility.InstantiatePrefab(CurrentPrefab) as GameObject;
                        Debug.Log(tempPrefabInstance);
                        break;

                    case EventType.MouseDrag:
                        if (tempPrefabInstance == null)
                        {
                            break;
                        }
                        Vector3 prefabPos = Vector3.zero;
                        if (mouseOverWindow == null)
                        {
                            validPos = false;
                        }
                        else if (mouseOverWindow.GetType() == typeof(MapManager))
                        {
                            MapManager mapManager = mouseOverWindow as MapManager;
                            Vector2    guiPos     = Event.current.mousePosition + position.position - mapManager.position.position;
                            prefabPos = mapManager.GetElementWorldPos(guiPos, 0);
                            validPos  = true;
                        }
                        else if (mouseOverWindow.GetType() == typeof(SceneView))
                        {
                            SceneView sv = SceneView.lastActiveSceneView;
                            Handles.SetCamera(SceneView.lastActiveSceneView.camera);
                            Vector2 guiPos = Event.current.mousePosition + new Vector2(position.xMin - sv.position.xMin, position.yMax - sv.position.yMax);
                            Ray     ray    = HandleUtility.GUIPointToWorldRay(guiPos);
                            float   yRate  = ray.origin.y / ray.direction.y;
                            prefabPos = new Vector3(ray.origin.x - ray.direction.x * yRate, 0, ray.origin.z - ray.direction.z * yRate);
                            validPos  = true;
                        }
                        else
                        {
                            validPos = false;
                        }
                        tempPrefabInstance.transform.position = prefabPos;
                        break;

                    case EventType.MouseUp:
                        DestroyImmediate(tempPrefabInstance);
                        break;

                    case EventType.Ignore:
                        if (validPos)
                        {
                            MapManager.SetMapObject(tempPrefabInstance);
                        }
                        else
                        {
                            DestroyImmediate(tempPrefabInstance);
                        }
                        break;
                    }
                }
            }
            else
            {
                //地图组预设
                if (MapGroupAssets.Count == 0)
                {
                    //大小为空
                    GUILayout.Label("……", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true));
                }
                else
                {
                    //selection list
                    scollVector = GUILayout.BeginScrollView(scollVector);
                    {
                        List <string> assetNameList = new List <string>();
                        for (int i = 0; i < MapGroupAssets.Count; i++)
                        {
                            assetNameList.Add(MapGroupAssets[i].groupName + (MapGroupAssets[i].baked ? "" : Prefs.mapAssetUnbakedStateMark));
                        }
                        EditorGUI.BeginChangeCheck();
                        mapAssetSelected = GUILayout.SelectionGrid(mapAssetSelected, assetNameList.ToArray(), 1, Prefs.mapAssetBackgroundStyle, GUILayout.Height(27 * MapGroupAssets.Count));
                        if (EditorGUI.EndChangeCheck())
                        {
                            MapInspector.FocusOn(CurrentAsset, MapInspector.SelectionType.GroupAsset);
                        }

                        GUILayout.Label("", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true));
                    }
                    GUILayout.EndScrollView();

                    //function
                    MapGroupAsset mga = CurrentAsset;
                    GUILayout.BeginHorizontal(Prefs.mapAssetBackgroundStyle, GUILayout.Height(28));
                    {
                        GUILayout.FlexibleSpace();
                        if (!mga.baked && GUILayout.Button("烘焙", GUILayout.Width(60)))
                        {
                            MapGroupAssetEditor.BakeGroupAsset(mga);
                        }
                        if (GUILayout.Button("删除", GUILayout.Width(60)))
                        {
                            MapGroupAssetEditor.DeleteGroupAsset(mga); mapAssetSelected = 0; MapInspector.FocusOn(CurrentAsset, MapInspector.SelectionType.GroupAsset);
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 创建Asset,请调用前确保没有重名Asset
        /// </summary>
        public static void CreateGroupAsset(MapGroup group)
        {
            string groupName = group.groupName;
            string path      = AssetPath + groupName + "/" + groupName;
            int    index     = group.index;

            //创建Asset
            MapGroupAsset asset = ScriptableObject.CreateInstance <MapGroupAsset>();

            asset.groupName = groupName;
            AssetDatabase.CreateFolder("Assets/Scenes/GroupAssets", groupName);

            //初始化
            group.transform.position = Vector3.zero;
            group.index = -1;
            group.transform.rotation = Quaternion.identity;
            bool isRootOfScene = group.transform.parent == null;

            group.transform.SetParent(null);

            //绑定Prefab
            asset.groupPrefab = PrefabUtility.CreatePrefab(path + ".prefab", group.gameObject, ReplacePrefabOptions.ConnectToPrefab);

            //创建场景
            Scene activeScene = EditorSceneManager.GetActiveScene();
            Scene scene       = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);

            EditorSceneManager.SetActiveScene(activeScene);

            Scene oldScene = group.gameObject.scene;

            EditorSceneManager.MoveGameObjectToScene(group.gameObject, scene);
            if (isRootOfScene)
            {
                EditorSceneManager.CloseScene(oldScene, true);
            }

            //保存场景
            //这里path的命名不是很懂,scene居然不能直接改名?
            {
                EditorSceneManager.SaveScene(scene, path + "0.unity");

                group.transform.rotation = Quaternion.Euler(0, -MapSystem.AnglePerGroup, 0);
                PrefabUtility.RecordPrefabInstancePropertyModifications(group.transform);
                EditorSceneManager.SaveScene(scene, path + "1.unity");

                group.transform.rotation = Quaternion.Euler(0, -MapSystem.AnglePerGroup * 2, 0);
                PrefabUtility.RecordPrefabInstancePropertyModifications(group.transform);
                EditorSceneManager.SaveScene(scene, path + "2.unity");
            }

            //重新载入
            EditorSceneManager.CloseScene(scene, true);
            LoadGroupDirect(asset, index);

            //保存
            AssetDatabase.CreateAsset(asset, AssetPath + groupName + ".asset");

            MapGroupAssets.Add(asset);
            EditorUtility.SetDirty(EditorMatrix.Prefs);
        }
Ejemplo n.º 10
0
        //Asset的创建/删除功能
        //底层维护方法

        public static string GetScenePath(MapGroupAsset asset, int index)
        {
            return(AssetPath + asset.groupName + "/" + asset.groupName + index % 3 + ".unity");
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 重命名
 /// </summary>
 public static void RenameGroupAsset(MapGroupAsset groupAsset)
 {
     throw new System.NotImplementedException();
 }