Ejemplo n.º 1
0
        private static void AddCamera()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                return;
            }

            GameObjectUndo.RecordSceneObject(so, false, "Added a Camera component");
            Camera cam = so.AddComponent <Camera>();

            cam.Main = true;

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
Ejemplo n.º 2
0
        private static void AddDirectionalLight()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                return;
            }

            GameObjectUndo.RecordSceneObject(so, false, "Added a Light component");
            Light light = so.AddComponent <Light>();

            light.Type = LightType.Directional;

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
Ejemplo n.º 3
0
        private static void AddAudioSource()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = UndoRedo.CreateSO("AudioSource", "New scene object");

                Selection.SceneObject = so;
                FocusOnHierarchyOrScene();
            }

            GameObjectUndo.RecordSceneObject(so, false, "Added a AudioSource component");
            so.AddComponent <AudioSource>();

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
Ejemplo n.º 4
0
        private static void AddCharacterController()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = UndoRedo.CreateSO("CharacterController", "New scene object");

                Selection.SceneObject = so;
                FocusOnHierarchyOrScene();
            }

            GameObjectUndo.RecordSceneObject(so, false, "Added a CharacterController component");
            so.AddComponent <CharacterController>();

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
Ejemplo n.º 5
0
        private static void AddAudioSource()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = new SceneObject("AudioSource");

                GameObjectUndo.RecordNewSceneObject(so);
                so.AddComponent <AudioSource>();

                FocusOnHierarchyOrScene();
            }
            else
            {
                GameObjectUndo.RecordSceneObject(so, false, "Added a AudioSource component");
                so.AddComponent <AudioSource>();
            }

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
Ejemplo n.º 6
0
        private static void AddSphericalJoint()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = new SceneObject("SphericalJoint");

                GameObjectUndo.RecordNewSceneObject(so);
                so.AddComponent <SphericalJoint>();

                FocusOnHierarchyOrScene();
            }
            else
            {
                GameObjectUndo.RecordSceneObject(so, false, "Added a SphericalJoint component");
                so.AddComponent <SphericalJoint>();
            }

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
Ejemplo n.º 7
0
        private static void AddPlaneCollider()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = new SceneObject("PlaneCollider");

                GameObjectUndo.RecordNewSceneObject(so);
                so.AddComponent <PlaneCollider>();

                FocusOnHierarchyOrScene();
            }
            else
            {
                GameObjectUndo.RecordSceneObject(so, false, "Added a PlaneCollider component");
                so.AddComponent <PlaneCollider>();
            }

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
Ejemplo n.º 8
0
        private void OnEditorUpdate()
        {
            if (currentType == InspectorType.SceneObject)
            {
                Component[] allComponents   = activeSO.GetComponents();
                bool        requiresRebuild = allComponents.Length != inspectorComponents.Count;

                if (!requiresRebuild)
                {
                    for (int i = 0; i < inspectorComponents.Count; i++)
                    {
                        if (inspectorComponents[i].uuid != allComponents[i].UUID)
                        {
                            requiresRebuild = true;
                            break;
                        }
                    }
                }

                if (requiresRebuild)
                {
                    SceneObject so = activeSO;
                    Clear();
                    SetObjectToInspect(so);
                }
                else
                {
                    RefreshSceneObjectFields(false);

                    InspectableState componentModifyState = InspectableState.NotModified;
                    for (int i = 0; i < inspectorComponents.Count; i++)
                    {
                        componentModifyState |= inspectorComponents[i].inspector.Refresh();
                    }

                    if (componentModifyState.HasFlag(InspectableState.ModifyInProgress))
                    {
                        EditorApplication.SetSceneDirty();
                    }

                    modifyState |= componentModifyState;
                }
            }
            else if (currentType == InspectorType.Resource)
            {
                inspectorResource.inspector.Refresh();
            }

            // Detect drag and drop
            bool isValidDrag = false;

            if (activeSO != null)
            {
                if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource)
                {
                    Vector2I windowPos     = ScreenToWindowPos(Input.PointerPosition);
                    Vector2I scrollPos     = windowPos;
                    Rect2I   contentBounds = inspectorLayout.Bounds;
                    scrollPos.x -= contentBounds.x;
                    scrollPos.y -= contentBounds.y;

                    bool   isInBounds = false;
                    Rect2I dropArea   = new Rect2I();
                    foreach (var bounds in dropAreas)
                    {
                        if (bounds.Contains(scrollPos))
                        {
                            isInBounds = true;
                            dropArea   = bounds;
                            break;
                        }
                    }

                    Type draggedComponentType = null;
                    if (isInBounds)
                    {
                        ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
                        if (dragData != null)
                        {
                            foreach (var resPath in dragData.Paths)
                            {
                                ResourceMeta meta = ProjectLibrary.GetMeta(resPath);
                                if (meta != null)
                                {
                                    if (meta.ResType == ResourceType.ScriptCode)
                                    {
                                        ScriptCode scriptFile = ProjectLibrary.Load <ScriptCode>(resPath);

                                        if (scriptFile != null)
                                        {
                                            Type[] scriptTypes = scriptFile.Types;
                                            foreach (var type in scriptTypes)
                                            {
                                                if (type.IsSubclassOf(typeof(Component)))
                                                {
                                                    draggedComponentType = type;
                                                    isValidDrag          = true;
                                                    break;
                                                }
                                            }

                                            if (draggedComponentType != null)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (isValidDrag)
                    {
                        scrollAreaHighlight.Bounds = dropArea;

                        if (DragDrop.DropInProgress)
                        {
                            GameObjectUndo.RecordSceneObject(activeSO, false, $"Added component \"{draggedComponentType.Name}\" to \"{activeSO.Name}\"");
                            activeSO.AddComponent(draggedComponentType);

                            modifyState = InspectableState.Modified;
                            EditorApplication.SetSceneDirty();
                        }
                    }
                }
            }

            if (scrollAreaHighlight != null)
            {
                scrollAreaHighlight.Active = isValidDrag;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Updates contents of the scene object specific fields (name, position, rotation, etc.)
        /// </summary>
        /// <param name="forceUpdate">If true, the GUI elements will be updated regardless of whether a change was
        ///                           detected or not.</param>
        internal void RefreshSceneObjectFields(bool forceUpdate)
        {
            if (activeSO == null)
            {
                return;
            }

            soNameInput.Text     = activeSO.Name;
            soActiveToggle.Value = activeSO.Active;
            soMobility.Value     = (ulong)activeSO.Mobility;

            SceneObject prefabParent = PrefabUtility.GetPrefabParent(activeSO);

            // Ignore prefab parent if scene root, we only care for non-root prefab instances
            bool hasPrefab = prefabParent != null && prefabParent.Parent != null;

            if (soHasPrefab != hasPrefab || forceUpdate)
            {
                int numChildren = soPrefabLayout.ChildCount;
                for (int i = 0; i < numChildren; i++)
                {
                    soPrefabLayout.GetChild(0).Destroy();
                }

                GUILabel prefabLabel = new GUILabel(new LocEdString("Prefab"), GUIOption.FixedWidth(50));
                soPrefabLayout.AddElement(prefabLabel);

                if (hasPrefab)
                {
                    GUIButton btnApplyPrefab  = new GUIButton(new LocEdString("Apply"), GUIOption.FixedWidth(60));
                    GUIButton btnRevertPrefab = new GUIButton(new LocEdString("Revert"), GUIOption.FixedWidth(60));
                    GUIButton btnBreakPrefab  = new GUIButton(new LocEdString("Break"), GUIOption.FixedWidth(60));

                    btnApplyPrefab.OnClick += () =>
                    {
                        PrefabUtility.ApplyPrefab(activeSO);
                    };
                    btnRevertPrefab.OnClick += () =>
                    {
                        GameObjectUndo.RecordSceneObject(activeSO, true, "Reverting \"" + activeSO.Name + "\" to prefab.");
                        PrefabUtility.RevertPrefab(activeSO);

                        GameObjectUndo.ResolveDiffs();
                        EditorApplication.SetSceneDirty();
                    };
                    btnBreakPrefab.OnClick += () =>
                    {
                        UndoRedo.BreakPrefab(activeSO, "Breaking prefab link for " + activeSO.Name);

                        EditorApplication.SetSceneDirty();
                    };

                    soPrefabLayout.AddElement(btnApplyPrefab);
                    soPrefabLayout.AddElement(btnRevertPrefab);
                    soPrefabLayout.AddElement(btnBreakPrefab);
                }
                else
                {
                    GUILabel noPrefabLabel = new GUILabel("None");
                    soPrefabLayout.AddElement(noPrefabLabel);
                }

                soHasPrefab = hasPrefab;
            }

            Vector3    position;
            Quaternion rotation;

            if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World)
            {
                position = activeSO.Position;
                rotation = activeSO.Rotation;
            }
            else
            {
                position = activeSO.LocalPosition;
                rotation = activeSO.LocalRotation;
            }

            Vector3 scale = activeSO.LocalScale;

            if (!soPos.HasInputFocus || forceUpdate)
            {
                soPos.Value = position;
            }

            // Avoid updating the rotation unless actually changed externally, since switching back and forth between
            // quaternion and euler angles can cause weird behavior
            if ((!soRot.HasInputFocus && rotation != lastRotation) || forceUpdate)
            {
                soRot.Value  = rotation.ToEuler();
                lastRotation = rotation;
            }

            if (!soScale.HasInputFocus || forceUpdate)
            {
                soScale.Value = scale;
            }
        }