public override void Drop(object[] dragObjects, PointerEventData pointerEventData)
        {
            base.Drop(dragObjects, pointerEventData);

            m_isSpawningPrefab = true;
            if (CanDrop(dragObjects))
            {
                for (int i = 0; i < dragObjects.Length; ++i)
                {
                    object    dragObject = dragObjects[i];
                    AssetItem assetItem  = dragObject as AssetItem;

                    if (assetItem != null)
                    {
                        m_project.Load(assetItem, (error, obj) =>
                        {
                            if (obj is GameObject)
                            {
                                GameObject prefab     = (GameObject)obj;
                                bool wasPrefabEnabled = prefab.activeSelf;
                                prefab.SetActive(false);

                                GameObject prefabInstance = Instantiate(prefab, Vector3.zero, Quaternion.identity, ((GameObject)m_treeView.DropTarget).transform);

                                prefab.SetActive(wasPrefabEnabled);

                                ExposeToEditor exposeToEditor = prefabInstance.GetComponent <ExposeToEditor>();
                                if (exposeToEditor == null)
                                {
                                    exposeToEditor = prefabInstance.AddComponent <ExposeToEditor>();
                                }

                                exposeToEditor.SetName(obj.name);
                                exposeToEditor.Parent = ((GameObject)m_treeView.DropTarget).GetComponent <ExposeToEditor>();
                                prefabInstance.SetActive(true);

                                RuntimeUndo.BeginRecord();
                                RuntimeUndo.RecordSelection();
                                RuntimeUndo.BeginRegisterCreateObject(prefabInstance);
                                RuntimeUndo.EndRecord();

                                bool isEnabled      = RuntimeUndo.Enabled;
                                RuntimeUndo.Enabled = false;
                                RuntimeSelection.activeGameObject = prefabInstance;
                                RuntimeUndo.Enabled = isEnabled;

                                RuntimeUndo.BeginRecord();
                                RuntimeUndo.RegisterCreatedObject(prefabInstance);
                                RuntimeUndo.RecordSelection();
                                RuntimeUndo.EndRecord();

                                m_isSpawningPrefab = false;
                            }
                        });
                    }
                }
                m_treeView.ExternalItemDrop();
            }
        }
        protected override void UpdateOverride()
        {
            base.UpdateOverride();
            if (!RuntimeEditorApplication.IsPointerOverWindow(this))
            {
                return;
            }

            if (InputController.GetKeyDown(SelectAllKey))
            {
                if (InputController.GetKey(ModifierKey))
                {
                    if (RuntimeEditorApplication.IsActiveWindow(this))
                    {
                        m_treeView.SelectedItems = m_treeView.Items;
                    }
                }
            }

            if (RuntimeTools.SpawnPrefab == null)
            {
                return;
            }

            m_treeView.ExternalItemDrag(Input.mousePosition);

            if (Input.GetMouseButtonUp(0))
            {
                m_isSpawningPrefab = true;
                GameObject prefabInstance = RuntimeTools.SpawnPrefab.InstantiatePrefab(Vector3.zero, Quaternion.identity);
                prefabInstance.SetActive(true);

                ExposeToEditor exposeToEditor = prefabInstance.GetComponent <ExposeToEditor>();
                if (exposeToEditor == null)
                {
                    exposeToEditor = prefabInstance.AddComponent <ExposeToEditor>();
                }

                exposeToEditor.SetName(RuntimeTools.SpawnPrefab.name);
                RuntimeUndo.BeginRecord();
                RuntimeUndo.RecordSelection();
                RuntimeUndo.BeginRegisterCreateObject(prefabInstance);
                RuntimeUndo.EndRecord();

                bool isEnabled = RuntimeUndo.Enabled;
                RuntimeUndo.Enabled = false;
                RuntimeSelection.activeGameObject = prefabInstance;
                RuntimeUndo.Enabled = isEnabled;

                RuntimeUndo.BeginRecord();
                RuntimeUndo.RegisterCreatedObject(prefabInstance);
                RuntimeUndo.RecordSelection();
                RuntimeUndo.EndRecord();

                RuntimeTools.SpawnPrefab = null;
            }
        }
 protected virtual void RecordTransform()
 {
     if (EnableUndo)
     {
         RuntimeUndo.BeginRecord();
         for (int i = 0; i < m_activeRealTargets.Length; ++i)
         {
             RuntimeUndo.RecordTransform(m_activeRealTargets[i]);
         }
         RuntimeUndo.EndRecord();
     }
 }
        public void Spawn()
        {
            m_editor = EditorDemo.Instance;
            if (m_editor == null)
            {
                Debug.LogError("Editor.Instance is null");
                return;
            }

            Vector3 point;

            m_dragPlane = new Plane(Vector3.up, m_editor.Pivot);
            if (GetPointOnDragPlane(out point))
            {
                m_instance = Prefab.InstantiatePrefab(point, Quaternion.identity);
                enabled    = true;
                m_spawn    = true;
            }
            else
            {
                m_instance = Prefab.InstantiatePrefab(m_editor.Pivot, Quaternion.identity);
            }

            ExposeToEditor exposeToEditor = m_instance.GetComponent <ExposeToEditor>();

            if (!exposeToEditor)
            {
                exposeToEditor = m_instance.AddComponent <ExposeToEditor>();
            }
            exposeToEditor.SetName(Prefab.name);
            m_instance.SetActive(true);

            RuntimeUndo.BeginRecord();
            RuntimeUndo.RecordSelection();
            RuntimeUndo.BeginRegisterCreateObject(m_instance);
            RuntimeUndo.EndRecord();

            bool isEnabled = RuntimeUndo.Enabled;

            RuntimeUndo.Enabled = false;
            RuntimeSelection.activeGameObject = m_instance;
            RuntimeUndo.Enabled = isEnabled;

            RuntimeUndo.BeginRecord();
            RuntimeUndo.RegisterCreatedObject(m_instance);
            RuntimeUndo.RecordSelection();
            RuntimeUndo.EndRecord();
        }
Beispiel #5
0
        public void Duplicate()
        {
            Object[] selectedObjects = RuntimeSelection.objects;
            if (selectedObjects != null && selectedObjects.Length > 0)
            {
                RuntimeUndo.BeginRecord();
                Object[] duplicates = new Object[selectedObjects.Length];
                for (int i = 0; i < duplicates.Length; ++i)
                {
                    GameObject go          = selectedObjects[i] as GameObject;
                    Object     duplicate   = Instantiate(go, go.transform.position, go.transform.rotation);
                    GameObject duplicateGo = duplicate as GameObject;

                    if (go != null && duplicateGo != null)
                    {
                        duplicateGo.SetActive(true);
                        duplicateGo.SetActive(go.activeSelf);
                        if (go.transform.parent != null)
                        {
                            duplicateGo.transform.SetParent(go.transform.parent, true);
                        }
                    }

                    duplicates[i] = duplicate;
                    RuntimeUndo.BeginRegisterCreateObject(duplicateGo);
                }
                RuntimeUndo.RecordSelection();
                RuntimeUndo.EndRecord();

                bool isEnabled = RuntimeUndo.Enabled;
                RuntimeUndo.Enabled      = false;
                RuntimeSelection.objects = duplicates;
                RuntimeUndo.Enabled      = isEnabled;

                RuntimeUndo.BeginRecord();
                for (int i = 0; i < duplicates.Length; ++i)
                {
                    GameObject selectedObj = (GameObject)duplicates[i];
                    if (selectedObj != null)
                    {
                        RuntimeUndo.RegisterCreatedObject(selectedObj);
                    }
                }
                RuntimeUndo.RecordSelection();
                RuntimeUndo.EndRecord();
            }
        }
 private void OnItemBeginDrop(object sender, ItemDropCancelArgs e)
 {
     if (e.IsExternal)
     {
     }
     else
     {
         RuntimeUndo.BeginRecord();
         for (int i = 0; i < e.DragItems.Length; ++i)
         {
             Transform dragT = ((GameObject)e.DragItems[i]).transform;
             RuntimeUndo.RecordTransform(dragT, dragT.parent, dragT.GetSiblingIndex());
             RuntimeUndo.RecordObject(dragT.gameObject, m_treeView.IndexOf(dragT.gameObject), RestoreIndexFromUndoRecord);
         }
         RuntimeUndo.EndRecord();
     }
 }
Beispiel #7
0
        public void Duplicate()
        {
            GameObject[] selection = RuntimeSelection.gameObjects;
            if (selection == null)
            {
                return;
            }

            RuntimeUndo.BeginRecord();
            for (int i = 0; i < selection.Length; ++i)
            {
                GameObject selectedObj = selection[i];
                if (selectedObj != null)
                {
                    Transform  p    = selectedObj.transform.parent;
                    GameObject copy = Instantiate(selectedObj, selectedObj.transform.position, selectedObj.transform.rotation);
                    copy.transform.SetParent(p, true);

                    selection[i] = copy;
                    RuntimeUndo.BeginRegisterCreateObject(copy);
                }
            }
            RuntimeUndo.RecordSelection();
            RuntimeUndo.EndRecord();

            bool isEnabled = RuntimeUndo.Enabled;

            RuntimeUndo.Enabled      = false;
            RuntimeSelection.objects = selection;
            RuntimeUndo.Enabled      = isEnabled;

            RuntimeUndo.BeginRecord();

            for (int i = 0; i < selection.Length; ++i)
            {
                GameObject selectedObj = selection[i];
                if (selectedObj != null)
                {
                    RuntimeUndo.RegisterCreatedObject(selectedObj);
                }
            }
            RuntimeUndo.RecordSelection();
            RuntimeUndo.EndRecord();
        }
Beispiel #8
0
        public void Delete()
        {
            GameObject[] selection = RuntimeSelection.gameObjects;
            if (selection == null || selection.Length == 0)
            {
                return;
            }

            RuntimeUndo.BeginRecord();
            for (int i = 0; i < selection.Length; ++i)
            {
                GameObject selectedObj = selection[i];
                if (selectedObj != null)
                {
                    RuntimeUndo.BeginDestroyObject(selectedObj);
                }
            }
            RuntimeUndo.RecordSelection();
            RuntimeUndo.EndRecord();

            bool isEnabled = RuntimeUndo.Enabled;

            RuntimeUndo.Enabled      = false;
            RuntimeSelection.objects = null;
            RuntimeUndo.Enabled      = isEnabled;

            RuntimeUndo.BeginRecord();

            for (int i = 0; i < selection.Length; ++i)
            {
                GameObject selectedObj = selection[i];
                if (selectedObj != null)
                {
                    RuntimeUndo.DestroyObject(selectedObj);
                }
            }
            RuntimeUndo.RecordSelection();
            RuntimeUndo.EndRecord();
        }
        public void CompleteSpawn()
        {
            if (RuntimeEditorApplication.IsPointerOverWindow(RuntimeWindowType.SceneView))
            {
                if (m_resource is Material)
                {
                    Material material    = (Material)m_resource;
                    Camera   sceneCamera = RuntimeEditorApplication.ActiveSceneCamera;
                    if (sceneCamera)
                    {
                        Ray        ray = sceneCamera.ScreenPointToRay(Input.mousePosition);
                        RaycastHit hitInfo;
                        if (Physics.Raycast(ray, out hitInfo))
                        {
                            MeshRenderer        renderer  = hitInfo.collider.GetComponentInChildren <MeshRenderer>();
                            SkinnedMeshRenderer sRenderer = hitInfo.collider.GetComponentInChildren <SkinnedMeshRenderer>();
                            if (renderer != null || sRenderer != null)
                            {
                                RuntimeUndo.BeginRecord();
                            }

                            if (renderer != null)
                            {
                                RuntimeUndo.RecordValue(renderer, Strong.PropertyInfo((MeshRenderer x) => x.sharedMaterials));
                                Material[] materials = renderer.sharedMaterials;
                                for (int i = 0; i < materials.Length; ++i)
                                {
                                    materials[i] = material;
                                }
                                renderer.sharedMaterials = materials;
                            }

                            if (sRenderer != null)
                            {
                                RuntimeUndo.RecordValue(sRenderer, Strong.PropertyInfo((SkinnedMeshRenderer x) => x.sharedMaterials));
                                Material[] materials = sRenderer.sharedMaterials;
                                for (int i = 0; i < materials.Length; ++i)
                                {
                                    materials[i] = material;
                                }
                                sRenderer.sharedMaterials = materials;
                            }

                            if (renderer != null || sRenderer != null)
                            {
                                RuntimeUndo.EndRecord();
                            }

                            if (renderer != null || sRenderer != null)
                            {
                                RuntimeUndo.BeginRecord();
                            }

                            if (renderer != null)
                            {
                                RuntimeUndo.RecordValue(renderer, Strong.PropertyInfo((MeshRenderer x) => x.sharedMaterials));
                            }

                            if (sRenderer != null)
                            {
                                RuntimeUndo.RecordValue(sRenderer, Strong.PropertyInfo((SkinnedMeshRenderer x) => x.sharedMaterials));
                            }

                            if (renderer != null || sRenderer != null)
                            {
                                RuntimeUndo.EndRecord();
                            }
                        }
                    }
                }
                else
                {
                    if (m_state == State.AfterSpawn)
                    {
                        bool isEnabled = RuntimeUndo.Enabled;
                        RuntimeUndo.Enabled      = false;
                        RuntimeSelection.objects = m_selection;
                        RuntimeUndo.Enabled      = isEnabled;

                        RuntimeUndo.BeginRecord();
                        RuntimeUndo.RecordSelection();
                        RuntimeUndo.BeginRegisterCreateObject(m_instance);
                        RuntimeUndo.EndRecord();

                        RuntimeUndo.Enabled = false;
                        RuntimeSelection.activeGameObject = m_instance;
                        RuntimeUndo.Enabled = isEnabled;

                        RuntimeUndo.BeginRecord();
                        RuntimeUndo.RegisterCreatedObject(m_instance);
                        RuntimeUndo.RecordSelection();
                        RuntimeUndo.EndRecord();
                    }
                }
                EndSpawn();
                RuntimeEditorApplication.ActivateWindow(RuntimeWindowType.SceneView);
            }
            else
            {
                if (!RuntimeEditorApplication.IsPointerOverWindow(RuntimeWindowType.Hierarchy))
                {
                    EndSpawn();
                }
            }
        }
        private void OnItemDrop(object sender, ItemDropArgs e)
        {
            if (e.IsExternal)
            {
            }
            else
            {
                Transform dropT = ((GameObject)e.DropTarget).transform;
                if (e.Action == ItemDropAction.SetLastChild)
                {
                    RuntimeUndo.BeginRecord();
                    for (int i = 0; i < e.DragItems.Length; ++i)
                    {
                        Transform dragT = ((GameObject)e.DragItems[i]).transform;
                        dragT.SetParent(dropT, true);
                        dragT.SetAsLastSibling();

                        RuntimeUndo.RecordTransform(dragT, dropT, dragT.GetSiblingIndex());
                        RuntimeUndo.RecordObject(dragT.gameObject, m_treeView.IndexOf(dragT.gameObject), RestoreIndexFromUndoRecord);
                    }
                    RuntimeUndo.EndRecord();
                }
                else if (e.Action == ItemDropAction.SetNextSibling)
                {
                    RuntimeUndo.BeginRecord();
                    for (int i = e.DragItems.Length - 1; i >= 0; --i)
                    {
                        Transform dragT      = ((GameObject)e.DragItems[i]).transform;
                        int       dropTIndex = dropT.GetSiblingIndex();
                        if (dragT.parent != dropT.parent)
                        {
                            dragT.SetParent(dropT.parent, true);
                            dragT.SetSiblingIndex(dropTIndex + 1);
                        }
                        else
                        {
                            int dragTIndex = dragT.GetSiblingIndex();
                            if (dropTIndex < dragTIndex)
                            {
                                dragT.SetSiblingIndex(dropTIndex + 1);
                            }
                            else
                            {
                                dragT.SetSiblingIndex(dropTIndex);
                            }
                        }

                        RuntimeUndo.RecordTransform(dragT, dropT.parent, dragT.GetSiblingIndex());
                        RuntimeUndo.RecordObject(dragT.gameObject, m_treeView.IndexOf(dragT.gameObject), RestoreIndexFromUndoRecord);
                    }

                    RuntimeUndo.EndRecord();
                }
                else if (e.Action == ItemDropAction.SetPrevSibling)
                {
                    RuntimeUndo.BeginRecord();
                    for (int i = 0; i < e.DragItems.Length; ++i)
                    {
                        Transform dragT = ((GameObject)e.DragItems[i]).transform;
                        if (dragT.parent != dropT.parent)
                        {
                            dragT.SetParent(dropT.parent, true);
                        }

                        int dropTIndex = dropT.GetSiblingIndex();
                        int dragTIndex = dragT.GetSiblingIndex();
                        if (dropTIndex > dragTIndex)
                        {
                            dragT.SetSiblingIndex(dropTIndex - 1);
                        }
                        else
                        {
                            dragT.SetSiblingIndex(dropTIndex);
                        }

                        RuntimeUndo.RecordTransform(dragT, dropT.parent, dragT.GetSiblingIndex());
                        RuntimeUndo.RecordObject(dragT.gameObject, m_treeView.IndexOf(dragT.gameObject), RestoreIndexFromUndoRecord);
                    }
                    RuntimeUndo.EndRecord();
                }
            }
        }
        private void OnResetClick()
        {
            RuntimeUndo.BeginRecord();

            GameObject go = new GameObject();

            go.SetActive(false);

            Component component = go.GetComponent(Component.GetType());

            if (component == null)
            {
                component = go.AddComponent(Component.GetType());
            }

            bool isMonoBehavior = component is MonoBehaviour;

            PropertyDescriptor[] descriptors = GetDescriptors(m_converter);
            for (int i = 0; i < descriptors.Length; ++i)
            {
                PropertyDescriptor descriptor = descriptors[i];
                MemberInfo         memberInfo = descriptor.ComponentMemberInfo;
                if (memberInfo is PropertyInfo)
                {
                    PropertyInfo p            = (PropertyInfo)memberInfo;
                    object       defaultValue = p.GetValue(component, null);
                    RuntimeUndo.RecordValue(Component, memberInfo);
                    p.SetValue(Component, defaultValue, null);
                }
                else
                {
                    if (isMonoBehavior)
                    {
                        if (memberInfo is FieldInfo)
                        {
                            FieldInfo f            = (FieldInfo)memberInfo;
                            object    defaultValue = f.GetValue(component);
                            RuntimeUndo.RecordValue(Component, memberInfo);
                            f.SetValue(Component, defaultValue);
                        }
                    }
                }
            }

            for (int i = 0; i < descriptors.Length; ++i)
            {
                PropertyDescriptor descriptor     = descriptors[i];
                MemberInfo         memberInfo     = descriptor.MemberInfo;
                PropertyEditor     propertyEditor = GetPropertyEditor(memberInfo);
                if (propertyEditor != null)
                {
                    propertyEditor.Reload();
                }
            }

            Destroy(go);

            RuntimeUndo.EndRecord();

            RuntimeUndo.BeginRecord();

            for (int i = 0; i < descriptors.Length; ++i)
            {
                PropertyDescriptor descriptor = descriptors[i];
                MemberInfo         memberInfo = descriptor.ComponentMemberInfo;
                if (memberInfo is PropertyInfo)
                {
                    RuntimeUndo.RecordValue(Component, memberInfo);
                }
                else
                {
                    if (isMonoBehavior)
                    {
                        RuntimeUndo.RecordValue(Component, memberInfo);
                    }
                }
            }

            RuntimeUndo.EndRecord();
        }
        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (RuntimeTools.IsPointerOverGameObject())
                {
                    return;
                }

                if (SceneCamera == null)
                {
                    Debug.LogError("Camera is null");
                    return;
                }

                if (RuntimeTools.IsViewing)
                {
                    return;
                }

                if (RuntimeTools.ActiveTool != null)
                {
                    return;
                }

                if (RuntimeEditorApplication.ActiveSceneCamera != null && !RuntimeEditorApplication.IsPointerOverWindow(RuntimeWindowType.SceneView))
                {
                    return;
                }

                Vector2 pointer = Input.mousePosition;
                m_dragIndex = Hit(pointer, HandlesPositions, HandlesNormals);
                if (m_dragIndex >= 0 && OnBeginDrag(m_dragIndex))
                {
                    m_handlesTransform        = HandlesTransform;
                    m_handlesInverseTransform = Matrix4x4.TRS(Target.position, Target.rotation, Target.localScale).inverse;  // m_handlesTransform.inverse;
                    m_dragPlane  = GetDragPlane();
                    m_isDragging = GetPointOnDragPlane(Input.mousePosition, out m_prevPoint);
                    m_normal     = HandlesNormals[m_dragIndex].normalized;
                    if (m_isDragging)
                    {
                        RuntimeTools.ActiveTool = this;
                    }
                    if (EnableUndo)
                    {
                        bool isRecording = RuntimeUndo.IsRecording;
                        if (!isRecording)
                        {
                            RuntimeUndo.BeginRecord();
                        }
                        RecordOverride();
                        if (!isRecording)
                        {
                            RuntimeUndo.EndRecord();
                        }
                    }
                }
            }
            else if (Input.GetMouseButtonUp(0))
            {
                if (m_isDragging)
                {
                    OnDrop();
                    bool isRecording = RuntimeUndo.IsRecording;
                    if (!isRecording)
                    {
                        RuntimeUndo.BeginRecord();
                    }
                    RecordOverride();
                    if (!isRecording)
                    {
                        RuntimeUndo.EndRecord();
                    }
                    m_isDragging            = false;
                    RuntimeTools.ActiveTool = null;
                }
            }
            else
            {
                if (m_isDragging)
                {
                    Vector3 point;
                    if (GetPointOnDragPlane(Input.mousePosition, out point))
                    {
                        Vector3 offset = m_handlesInverseTransform.MultiplyVector(point - m_prevPoint);
                        offset = Vector3.Project(offset, m_normal);
                        if (InputController.GetKey(UnitSnapKey) || RuntimeTools.UnitSnapping)
                        {
                            Vector3 gridOffset = Vector3.zero;
                            if (Mathf.Abs(offset.x * 1.5f) >= GridSize)
                            {
                                gridOffset.x = GridSize * Mathf.Sign(offset.x);
                            }

                            if (Mathf.Abs(offset.y * 1.5f) >= GridSize)
                            {
                                gridOffset.y = GridSize * Mathf.Sign(offset.y);
                            }

                            if (Mathf.Abs(offset.z * 1.5f) >= GridSize)
                            {
                                gridOffset.z = GridSize * Mathf.Sign(offset.z);
                            }

                            if (gridOffset != Vector3.zero)
                            {
                                if (OnDrag(m_dragIndex, gridOffset))
                                {
                                    m_prevPoint = point;
                                }
                            }
                        }
                        else
                        {
                            if (OnDrag(m_dragIndex, offset))
                            {
                                m_prevPoint = point;
                            }
                        }
                    }
                }
            }

            UpdateOverride();
        }