示例#1
0
        private void StartGame()
        {
            DestroyGame();

            m_editorObjects        = ExposeToEditor.FindAll(m_editor, ExposeToEditorObjectType.EditorMode, true).Select(go => go.GetComponent <ExposeToEditor>()).OrderBy(exp => exp.transform.GetSiblingIndex()).ToArray();
            m_enabledEditorObjects = m_editorObjects.Where(eo => eo.gameObject.activeSelf).ToArray();
            m_editorSelection      = m_editor.Selection.objects;

            HashSet <GameObject> selectionHS       = new HashSet <GameObject>(m_editor.Selection.gameObjects != null ? m_editor.Selection.gameObjects : new GameObject[0]);
            List <GameObject>    playmodeSelection = new List <GameObject>();

            for (int i = 0; i < m_editorObjects.Length; ++i)
            {
                ExposeToEditor editorObj = m_editorObjects[i];
                if (editorObj.Parent != null)
                {
                    continue;
                }

                GameObject playmodeObj = Instantiate(editorObj.gameObject, editorObj.transform.position, editorObj.transform.rotation);

                ExposeToEditor playModeObjExp = playmodeObj.GetComponent <ExposeToEditor>();
                playModeObjExp.ObjectType = ExposeToEditorObjectType.PlayMode;
                playModeObjExp.SetName(editorObj.name);
                playModeObjExp.Init();

                ExposeToEditor[] editorObjAndChildren   = editorObj.GetComponentsInChildren <ExposeToEditor>(true);
                ExposeToEditor[] playModeObjAndChildren = playmodeObj.GetComponentsInChildren <ExposeToEditor>(true);
                for (int j = 0; j < editorObjAndChildren.Length; j++)
                {
                    if (selectionHS.Contains(editorObjAndChildren[j].gameObject))
                    {
                        playmodeSelection.Add(playModeObjAndChildren[j].gameObject);
                    }
                }

                editorObj.gameObject.SetActive(false);
            }

            bool isEnabled = m_editor.Undo.Enabled;

            m_editor.Undo.Enabled      = false;
            m_editor.Selection.objects = playmodeSelection.ToArray();
            m_editor.Undo.Enabled      = isEnabled;
            m_editor.Undo.Store();
        }
        private void OnPlaymodeStateChanging()
        {
            if (m_editor.IsPlaying)
            {
                m_playModeCache   = new HashSet <ExposeToEditor>();
                m_enabledObjects  = m_editModeCache.Where(eo => eo != null && eo.gameObject.activeSelf && !eo.MarkAsDestroyed).ToArray();
                m_selectedObjects = m_editor.Selection.objects;

                HashSet <GameObject> selectionHS       = new HashSet <GameObject>(m_editor.Selection.gameObjects != null ? m_editor.Selection.gameObjects : new GameObject[0]);
                List <GameObject>    playmodeSelection = new List <GameObject>();

                GameObject fakeRoot = new GameObject("FakeRoot");
                fakeRoot.SetActive(false);
                foreach (ExposeToEditor editorObj in m_editModeCache.OrderBy(eo => eo.transform.GetSiblingIndex()))
                {
                    if (editorObj.GetParent() != null)
                    {
                        continue;
                    }

                    editorObj.gameObject.SetActive(false);
                    editorObj.transform.SetParent(fakeRoot.transform);
                }

                GameObject fakeRootPlayMode = Instantiate(fakeRoot);
                for (int i = 0; i < fakeRootPlayMode.transform.childCount; ++i)
                {
                    ExposeToEditor playModeObj = fakeRootPlayMode.transform.GetChild(i).GetComponent <ExposeToEditor>();
                    ExposeToEditor editorObj   = fakeRoot.transform.GetChild(i).GetComponent <ExposeToEditor>();

                    playModeObj.SetName(editorObj.name);
                    playModeObj.Init();
                    m_playModeCache.Add(playModeObj);

                    ExposeToEditor[] editorObjAndChildren   = editorObj.GetComponentsInChildren <ExposeToEditor>(true);
                    ExposeToEditor[] playModeObjAndChildren = playModeObj.GetComponentsInChildren <ExposeToEditor>(true);
                    for (int j = 0; j < editorObjAndChildren.Length; j++)
                    {
                        if (selectionHS.Contains(editorObjAndChildren[j].gameObject))
                        {
                            playmodeSelection.Add(playModeObjAndChildren[j].gameObject);
                        }
                    }
                }


                fakeRoot.transform.DetachChildren();
                fakeRootPlayMode.transform.DetachChildren();
                Destroy(fakeRoot);
                Destroy(fakeRootPlayMode);

                foreach (ExposeToEditor playModeObj in m_playModeCache.ToArray())
                {
                    playModeObj.gameObject.SetActive(true);
                }

                bool isEnabled = m_editor.Undo.Enabled;
                m_editor.Undo.Enabled      = false;
                m_editor.Selection.objects = playmodeSelection.ToArray();
                m_editor.Undo.Enabled      = isEnabled;
                m_editor.Undo.Store();
            }
            else
            {
                foreach (ExposeToEditor playObj in m_playModeCache)
                {
                    if (playObj != null)
                    {
                        playObj.SendMessage("OnRuntimeDestroy", SendMessageOptions.DontRequireReceiver);
                        DestroyImmediate(playObj.gameObject);
                    }
                }

                for (int i = 0; i < m_enabledObjects.Length; ++i)
                {
                    ExposeToEditor editorObj = m_enabledObjects[i];
                    if (editorObj != null)
                    {
                        editorObj.gameObject.SetActive(true);
                    }
                }

                bool isEnabled = m_editor.Undo.Enabled;
                m_editor.Undo.Enabled      = false;
                m_editor.Selection.objects = m_selectedObjects;
                m_editor.Undo.Enabled      = isEnabled;
                m_editor.Undo.Restore();

                m_playModeCache   = null;
                m_enabledObjects  = null;
                m_selectedObjects = null;
            }
        }