コード例 #1
0
 private void InitializeDefaultTerrain(LE_ConfigTerrain p_LEConfTerrain)
 {
     // initialize custom default terrain
     if (p_LEConfTerrain.CustomDefaultTerrain != null && p_LEConfTerrain.CustomDefaultTerrain.terrainData != null)
     {
         // save a reference to the default data prefab
         m_GUI3dTerrain.DefaultTerrainDataPrefab = p_LEConfTerrain.CustomDefaultTerrain.terrainData;
         // clone the terrain data so that the asset is not broken when testing in Unity Editor
         p_LEConfTerrain.CustomDefaultTerrain.enabled     = false;
         p_LEConfTerrain.CustomDefaultTerrain.terrainData = m_GUI3dTerrain.GetDefaultTerrainDataDeepCopy();
         if (p_LEConfTerrain.CustomDefaultTerrain.GetComponent <TerrainCollider>() != null)
         {
             p_LEConfTerrain.CustomDefaultTerrain.GetComponent <TerrainCollider>().terrainData = p_LEConfTerrain.CustomDefaultTerrain.terrainData;
         }
         else
         {
             Debug.LogError("LE_LevelEditorMain: the CustomDefaultTerrain assigned to LE_ConfigTerrain must have a collider!");
         }
         p_LEConfTerrain.CustomDefaultTerrain.Flush();
         p_LEConfTerrain.CustomDefaultTerrain.enabled = true;
         // access the custom predefined terrain data
         // and wrap it with a terrain manager, which is then assigned to the GUI3dTerrain instance
         m_GUI3dTerrain.SetTerrain(p_LEConfTerrain.CustomDefaultTerrain);
         // your terrain must be in the LE_ConfigTerrain.TerrainLayer layer, you can set this in the game object,
         // but it is included here so that you cannot forget it
         p_LEConfTerrain.CustomDefaultTerrain.gameObject.layer = p_LEConfTerrain.TerrainLayer;
         // just to be on the safe side call this event and notify listeners that the level data was changed
         if (LE_EventInterface.OnChangeLevelData != null)
         {
             LE_EventInterface.OnChangeLevelData(p_LEConfTerrain.CustomDefaultTerrain.gameObject, new LE_LevelDataChangedEvent(LE_ELevelDataChangeType.TERRAIN_LOADED_DEFAULT));
         }
         // a terrain does exist -> activate edit terrain UI
         if (LE_GUIInterface.Instance.delegates.SetTerrainUIMode != null)
         {
             LE_GUIInterface.Instance.delegates.SetTerrainUIMode(LE_GUIInterface.Delegates.ETerrainUIMode.EDIT);
         }
         else
         {
             Debug.LogWarning("LE_LevelEditorMain: you have not set the LE_GUIInterface.delegates.SetTerrainUIMode delegate. You need to set it for example if you want to disable the create UI if the default Unity terrain is set!");
         }
     }
     else
     {
         // there is no terrain -> activate create terrain UI
         if (LE_GUIInterface.Instance.delegates.SetTerrainUIMode != null)
         {
             LE_GUIInterface.Instance.delegates.SetTerrainUIMode(LE_GUIInterface.Delegates.ETerrainUIMode.CREATE);
         }
         else
         {
             Debug.LogWarning("LE_LevelEditorMain: you have not set the LE_GUIInterface.delegates.SetTerrainUIMode delegate. You need to set it for example if you want to show the create UI if there is no default Unity terrain set!");
         }
     }
 }
コード例 #2
0
        // some of the content in this method must be called in the first update and cannot be initialized in the start function
        private void Initialize_InFirstUpdate()
        {
            LE_ConfigLevel LEConfLevel = GetComponentInChildren <LE_ConfigLevel>();

            if (LEConfLevel == null)
            {
                Debug.LogError("LE_LevelEditorMain: a LE_ConfigLevel component must be added to the game object with the LE_LevelEditorMain script!");
                LEConfLevel = gameObject.AddComponent <LE_ConfigLevel>();
            }

            Camera           cam           = Cam;
            LE_ConfigTerrain LEConfTerrain = GetComponentInChildren <LE_ConfigTerrain>();

            // check further parameters that have been set in Start of other scripts
            if (LE_GUIInterface.Instance.delegates.IsCursorOverUI == null)
            {
                Debug.LogError("LE_LevelEditorMain: you have not setup LE_GUIInterface.delegates.IsCursorOverUI. Terrain might be edited behind UI while the user interacts with the UI, same is true for object placement!");
            }

            // initialize terrain logic
            if (IS_TERRAIN_EDITOR)
            {
                // init 3d UI
                m_GUI3dTerrain = gameObject.AddComponent <LE_GUI3dTerrain>();
                m_GUI3d.Add(m_GUI3dTerrain);

                // init default terrain
                InitializeDefaultTerrain(LEConfTerrain);

                // init logic
                m_logicTerrain = new LE_LogicTerrain(LEConfTerrain, m_GUI3dTerrain);
                m_logic.Add(m_logicTerrain);
            }
            else if (LEConfTerrain.CustomDefaultTerrain != null)
            {
                Debug.LogWarning("LE_LevelEditorMain: IS_TERRAIN_EDITOR is set to 'false', but you have provided a value for LE_ConfigTerrain.CustomDefaultTerrain! The value will be ignored!");
            }

            // initialize level logic
            m_logicLevel = new LE_LogicLevel(
                LEConfLevel,
                m_GUI3dTerrain,
                m_GUI3dObject,
                IS_OBLIQUE_FOCUS_CENTERING,
                LEConfTerrain.TerrainTextureConfig.TERRAIN_TEXTURES,
                LEConfTerrain.TerrainTextureConfig.TERRAIN_TEXTURE_SIZES,
                LEConfTerrain.TerrainTextureConfig.TERRAIN_TEXTURE_OFFSETS);
            m_logic.Add(m_logicLevel);

            // initialize object logic
            if (IS_OBJECT_EDITOR)
            {
                m_logic.Add(new LE_LogicObjects(m_GUI3dObject, ROOT_OBJECT_MAP));
            }

            // initialize camera gizmo
            if (cam != null && IS_WITH_CAMERA_PERSPECTIVE_GIZMO)
            {
                if (LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset != null)
                {
                    // calculate the screen rect of the camera perspective gizmo (used later for mouse over UI calculation)
                    const float relativeSize = 0.2f;
                    float       rectW        = relativeSize * (float)Screen.width / cam.aspect;
                    float       rectH        = relativeSize * (float)Screen.height;
                    m_perspectiveGizmoRect = new Rect(Screen.width - LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset() - rectW, 0f, rectW, rectH);
                    if (string.IsNullOrEmpty(LayerMask.LayerToName(CAMERA_PERSPECTIVE_GIZMO_LAYER)))
                    {
                        Debug.LogWarning("LE_LevelEditorMain: Inspector property 'CAMERA_PERSPECTIVE_GIZMO_LAYER' is set to '" + CAMERA_PERSPECTIVE_GIZMO_LAYER + "', but this layer has no name in 'Tags & Layers' set. Please set the name of this layer to 'CameraPerspectiveGizmo' or change the value of the 'CAMERA_PERSPECTIVE_GIZMO_LAYER' property! To open the 'Tags & Layers' manager select any game object, in the inspector click on the layer drop down at top right then click on 'Add Layer...'.");
                    }
                    else if (LayerMask.LayerToName(CAMERA_PERSPECTIVE_GIZMO_LAYER) != "CameraPerspectiveGizmo")
                    {
                        Debug.LogWarning("LE_LevelEditorMain: Inspector property 'CAMERA_PERSPECTIVE_GIZMO_LAYER' is set to '" + CAMERA_PERSPECTIVE_GIZMO_LAYER + "', but the name of this layer is '" + LayerMask.LayerToName(CAMERA_PERSPECTIVE_GIZMO_LAYER) + "'. Is this intended? If not please set the name of this layer to 'CameraPerspectiveGizmo' or change the value of the 'CAMERA_PERSPECTIVE_GIZMO_LAYER' property! To open the 'Tags & Layers' manager select any game object, in the inspector click on the layer drop down at top right then click on 'Add Layer...'.");
                    }
                    // create and setup the camera perspective gizmo
                    m_cameraPerspectiveGizmo = CPG_CameraPerspectiveGizmo.Create(cam, CAMERA_PERSPECTIVE_GIZMO_LAYER);
                    if (m_cameraPerspectiveGizmo != null)
                    {
                        // move the camera perspective gizmo far away (it will not be rendered anyway, but this is needed to prevent collision)
                        m_cameraPerspectiveGizmo.transform.position = Vector3.down * -10000f;
                        // set ortho offset of the gizmo
                        m_cameraPerspectiveGizmo.OrthoOffset = cam.farClipPlane * 0.2f;
                        // make sure that the main camera will not render the gizmo
                        cam.cullingMask = cam.cullingMask & ~(1 << CAMERA_PERSPECTIVE_GIZMO_LAYER);
                        // set position of the gizmo
                        m_cameraPerspectiveGizmo.RelativeScreenSize = relativeSize;
                        float rightMenuOffset = LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset() / (float)Screen.width;
                        m_cameraPerspectiveGizmo.RelativeScreenPos = new Vector2(1f - relativeSize * 0.5f / cam.aspect - rightMenuOffset, 1f - relativeSize * 0.5f);
                        // register for events of the camera perspective gizmo
                        m_cameraPerspectiveGizmo.m_onBeforeSwitchToOrthographic += OnCameraPerspectiveSwitchToOrthographic;
                        m_cameraPerspectiveGizmo.m_onAfterSwitchToPerspective   += OnCameraPerspectiveSwitchToPerspective;
                    }
                }
                else
                {
                    Debug.LogError("LE_LevelEditorMain: LE_GUIInterface.delegates.GetCameraPerspectiveGizmoRightPixelOffset was not set from the UI scripts! The camera perspective gizmo cannot be placed to the right screen position! To set this delegate use 'LE_GUIInterface.Instance.delegates.GetCameraPerspectiveGizmoRightPixelOffset = ...' in one of the Start functions of your scripts!");
                }
            }

            // apply an oblique camera projection to bring focused objects to the middle of the visible screen
            // instead of being in the middle of the rendered screen, which means close to the right menu UI
            if (IS_OBLIQUE_FOCUS_CENTERING && !SetObliqueFocusProjectionMatrix(true))
            {
                Debug.LogError("LE_LevelEditorMain: IS_OBLIQUE_FOCUS_CENTERING is true, but you have not provided the LE_GUIInterface.delegates.GetObliqueCameraPerspectiveRightPixelOffset delegate! Provide it to bring focused objects in the center of the visible (not covered by UI) screen area!");
            }
        }
コード例 #3
0
        private void Start()
        {
            // check if there is an instance of the LE_GUIInterface, which is required for the editor to work
            if (LE_GUIInterface.Instance == null)
            {
                Debug.LogError("LE_LevelEditorMain: a LE_GUIInterface object must be added to the scene!");
            }

            // search or create a default(will be not functional!) config
            LE_ConfigTerrain LEConfTerrain = GetComponentInChildren <LE_ConfigTerrain>();

            if (LEConfTerrain == null)
            {
                Debug.LogError("LE_LevelEditorMain: a LE_ConfigTerrain component must be added to the game object with the LE_LevelEditorMain script!");
                LEConfTerrain = gameObject.AddComponent <LE_ConfigTerrain>();
            }
            // check if everything is setup right
            if (LEConfTerrain.TerrainTextureConfig == null)
            {
                Debug.LogError("LE_LevelEditorMain: TerrainTextureConfig was not initialized! You need to set it in the inspector of LE_ConfigTerrain. Provide an empty terrain texture config if you do not want to use the terrain editor.");
            }
            if (!IS_TERRAIN_EDITOR)
            {
                if (LEConfTerrain.Brushes.Length > 0)
                {
                    Debug.LogWarning("LE_LevelEditorMain: IS_TERRAIN_EDITOR is set to 'false', but you have provided a non empty array for LE_ConfigTerrain.Brushes! This is performance waist and will increase loading time! Remove brushes or reenable terrain editing.");
                }
                if (LEConfTerrain.BrushProjector != null)
                {
                    Debug.LogWarning("LE_LevelEditorMain: IS_TERRAIN_EDITOR is set to 'false', but you have provided a value for LE_ConfigTerrain.BrushProjector! This is performance waist and will increase loading time! Remove brush projector from scene or reenable terrain editing.");
                }
            }

            Camera cam = Cam;

            if (cam == null)
            {
                Debug.LogError("LE_LevelEditorMain: Start: could not find main camera!");
            }

            // initialize command manager (undo/redo)
            InitializeCommandManager();

            // initialize object 3d GUI
            if (IS_OBJECT_EDITOR)
            {
                m_GUI3dObject = gameObject.AddComponent <LE_GUI3dObject>();
                m_GUI3dObject.TERRAIN_LAYER       = LEConfTerrain.TerrainLayer;
                m_GUI3dObject.OnFocused          += OnGUI3dObjectFocused;
                m_GUI3dObject.IsKeyComboFocus     = (ACTIVE_KEY_COMBOS & LE_EKeyCombo.FOCUS) != 0;
                m_GUI3dObject.IsKeyComboDuplicate = (ACTIVE_KEY_COMBOS & LE_EKeyCombo.DUPLICATE) != 0;
                m_GUI3d.Add(m_GUI3dObject);
            }

            // initialize input
            m_input = new LE_Input(this);

            // set pivot point
            m_camPivot = cam.transform.position + cam.transform.forward * 100f;

            // to monitor the last change frame number register to the OnChangeLevelData event
            LE_EventInterface.OnChangeLevelData += OnChangeLevelData;

            // register to UI events
            LE_GUIInterface.Instance.events.OnEditModeBtn += OnEditModeBtn;
            LE_GUIInterface.Instance.events.OnUndoBtn     += OnUndoBtn;
            LE_GUIInterface.Instance.events.OnRedoBtn     += OnRedoBtn;
        }