public void MarkSnapPointAsUsed(LE_Object p_sourceObj, LE_Object p_destinationObj, int snapPointIndex) { if (p_sourceObj != null) { if (snapPointIndex != -1) { string snapPointUID = p_sourceObj.UID + "_" + snapPointIndex; if (!m_snapPointUIDsToObjUIDs.ContainsKey(snapPointUID)) { m_snapPointUIDsToObjUIDs.Add(snapPointUID, p_destinationObj.UID); } else { Debug.LogError("LE_GUI3dObject: MarkSnapPointAsUsed: duplicate snapping on snapPointUID(" + snapPointUID + ") new (ignored) objID(" + p_destinationObj.UID + ") and old objID(" + m_snapPointUIDsToObjUIDs[snapPointUID] + ")"); } } else { Debug.LogError("LE_GUI3dObject: MarkSnapPointAsUsed: could not find LE_ObjectSnapPoint of snap source!"); } } else { Debug.LogError("LE_GUI3dObject: MarkSnapPointAsUsed: could not find LE_Object of snap source!"); } }
public static void AddObjectSnapping(LE_GUI3dObject p_gui3d, LE_Object p_newObject) { for (int j = 0; j < p_newObject.ObjectSnapPoints.Length; j++) { if (p_newObject.ObjectSnapPoints[j] != null) { Material matLine = null; Material matFill = null; if (p_newObject.IsDrawSnapToObjectUI) { matLine = (Material)Resources.Load("SnapToObjectUIMaterial_Line"); matFill = (Material)Resources.Load("SnapToObjectUIMaterial_Fill"); } S_SnapToObject snapInstance = p_newObject.ObjectSnapPoints[j].InstatiateSnapSystem((GameObject)GameObject.Instantiate(Resources.Load("ObjectSnapButtonVisuals")), p_newObject.IsDrawSnapToObjectUI, matLine, matFill); if (snapInstance != null) { string snapPointUID = GetSnapPointUID(p_newObject.UID, j); p_gui3d.AddSnapPoint(snapPointUID, snapInstance); // deactivate if snap UI is hidden right now if (!p_gui3d.IsSnapToObjectActive) { snapInstance.gameObject.SetActive(false); } // restore the already snapped object states p_gui3d.LoadSnapCounter(snapPointUID, snapInstance); } } else { Debug.LogError("LE_GUI3dObject: AddObjectSnapping: object '" + p_newObject.name + "' has a nullpointer in the ObjectSnapPoints array at index '" + j + "'!"); } } }
public static void ApplyColor(LE_Object p_instance, Color p_color) { if (p_instance.IsWithColorProperty) { p_instance.ColorProperty = p_color; } }
public void SelectObject(LE_Object p_object) { LE_Object priorSelectedObject = null; if (m_selectedObject != null && m_selectedObject != p_object) { m_selectedObject.EditMode = LE_EObjectEditMode.NO_EDIT; m_selectedObject.IsSelected = false; priorSelectedObject = m_selectedObject; } if (LE_LevelEditorMain.Instance != null && LE_LevelEditorMain.Instance.EditMode != LE_EEditMode.OBJECT) { // selection allowed only in object edit mode p_object = null; } m_selectedObject = p_object; if (m_selectedObject != null) { m_selectedObject.IsSelected = true; } if (LE_EventInterface.OnObjectSelectedInScene != null) { LE_EventInterface.OnObjectSelectedInScene(this, new LE_ObjectSelectedEvent(m_selectedObject, priorSelectedObject)); } }
public bool IsObjectPlaceable(LE_Object p_object, string p_resourcePath) { // check if there is an instance count limit m_isSceneInstanceFound = false; if (p_object.MaxInstancesInLevel != 0) { int count = 0; LE_Object[] objects = Object.FindObjectsOfType <LE_Object>(); for (int i = 0; i < objects.Length; i++) { if (objects[i].name == p_resourcePath) { m_isSceneInstanceFound = true; count++; if (p_object.MaxInstancesInLevel <= count) { return(false); // the limit (MaxInstancesInLevel) is already reached } } } } else { m_isSceneInstanceFound = GameObject.Find(m_objectResourcePath); } return(true); }
public static void AddTerrainSnapping(LE_GUI3dObject p_gui3d, LE_Object p_newObject) { // add and configurate world snapper S_SnapToWorld worldSnap = p_newObject.gameObject.AddComponent <S_SnapToWorld>(); worldSnap.SnapToLayers = 1 << p_gui3d.TERRAIN_LAYER; worldSnap.SnapFrameRate = -1; worldSnap.IsRotationSnap = p_newObject.IsPlacementRotationByNormal; // snap after create worldSnap.DoSnap(); // update snapper every time the terrain is changed or the object is moved System.EventHandler <LE_LevelDataChangedEvent> snapUpdateFunct = (object p_object, LE_LevelDataChangedEvent p_args) => { if (p_args.ChangeType == LE_ELevelDataChangeType.TERRAIN_HEIGHTS || (p_object is LE_ObjectEditHandle && (((LE_ObjectEditHandle)p_object).Target == p_newObject.transform || ((LE_ObjectEditHandle)p_object).transform.IsChildOf(p_newObject.transform)))) { worldSnap.DoSnap(); } }; LE_EventInterface.OnChangeLevelData += snapUpdateFunct; p_newObject.gameObject.AddComponent <UtilityOnDestroyHandler>().m_onDestroy = () => { LE_EventInterface.OnChangeLevelData -= snapUpdateFunct; }; }
public static void AddSnappingScripts(LE_GUI3dObject p_gui3d, LE_Object p_newObject) { if (p_newObject != null) { // handle snapping if (p_newObject.SnapType == LE_Object.ESnapType.SNAP_TO_OBJECT) // object snapping { AddObjectSnapping(p_gui3d, p_newObject); } else if (p_newObject.SnapType == LE_Object.ESnapType.SNAP_TO_TERRAIN) // terrain snapping { AddTerrainSnapping(p_gui3d, p_newObject); } else { if (p_newObject.SnapType == LE_Object.ESnapType.SNAP_TO_3D_GRID || p_newObject.SnapType == LE_Object.ESnapType.SNAP_TO_2D_GRID_AND_TERRAIN) { // add grid snapping AddGridSnapping(p_gui3d, p_newObject, false); } } } else { Debug.LogError("LE_GUI3dObject: AddSnappingScripts: passed object is null!"); } }
public LE_CmdTransformObject(LE_Object p_object, Vector3 p_deltaPos, Quaternion p_deltaRot, Vector3 p_deltaLocalScale) { m_object.Obj = p_object; m_deltaPos = p_deltaPos; m_deltaRot = p_deltaRot; m_deltaLocalScale = p_deltaLocalScale; m_isExecuted = true; }
public LE_CmdPlaceObject(LE_GUI3dObject p_gui3d, LE_Object p_prefab, Transform p_copyTransform, string p_objectResourcePath) { m_gui3d = p_gui3d; m_prefab = p_prefab; m_position = p_copyTransform.position; m_rotation = p_copyTransform.rotation; m_scale = p_copyTransform.localScale; m_objectResourcePath = p_objectResourcePath; }
public static void OnNewObjectPlaced(LE_GUI3dObject p_gui3d, LE_Object p_newInstance) { ApplyRandomity(p_newInstance); ApplyColor(p_newInstance, p_newInstance.ColorProperty); ApplyVariation(p_newInstance, p_newInstance.VariationsDefaultIndex); p_newInstance.SolveCollisionAndDeactivateRigidbody(); // solve placement of rigidbodies AddSnappingScripts(p_gui3d, p_newInstance); SelectNewObjectAndNotifyListeners(p_gui3d, p_newInstance); }
private void OnPreviewObjectInstantiated(object p_sender, S_SnapToObjectEventArgs p_args) { LE_Object leObj = p_args.NewInstance.GetComponent <LE_Object>(); if (leObj != null) { Destroy(leObj); } }
public static LE_Object InstantiateObject(LE_Object p_prefab, Vector3 p_position, Quaternion p_rotation, Vector3 p_scale, string p_objectResourcePath) { LE_Object instance = (LE_Object)GameObject.Instantiate(p_prefab); instance.name = p_objectResourcePath; instance.transform.position = p_position; instance.transform.rotation = p_rotation; instance.transform.localScale = p_scale; return(instance); }
private void OnBeforeObjectSnapped(object p_sender, S_SnapToObjectBeforePlacementEventArgs p_args) { p_args.IsDelayedPlacePrefab = true; LE_Object sourceObj = p_args.Source.GetComponentInParent <LE_Object>(); int snapPointIndex = GetSnapPointIndex(sourceObj, p_args.Source); if (snapPointIndex >= 0) // error was written to console already { UR_CommandMgr.Instance.Execute(new LE_CmdSnapObjectToObject(this, sourceObj.UID, snapPointIndex, p_args.SnapPrefab)); } }
public void MarkSnapPointAsUsed(LE_Object p_sourceObj, LE_Object p_destinationObj, S_SnapToObject p_snapScript) { if (p_sourceObj != null) { int snapPointIndex = GetSnapPointIndex(p_sourceObj, p_snapScript); MarkSnapPointAsUsed(p_sourceObj, p_destinationObj, snapPointIndex); } else { Debug.LogError("LE_GUI3dObject: MarkSnapPointAsUsed: could not find LE_Object of snap source!"); } }
public void SetDraggableObject(LE_Object p_object, string p_objectResourcePath) { if (m_object != p_object) { m_isSceneInstanceFound = false; m_dragMessage = ""; SetDragMessageInUI(); m_object = p_object; m_objectResourcePath = p_objectResourcePath; m_isObjectPlaceable = IsObjectPlaceable(); } }
public LE_CmdDeleteObject(LE_GUI3dObject p_gui3d, LE_Object p_selectedObject) { m_gui3d = p_gui3d; m_objectInstance.Obj = p_selectedObject; m_prefab = Resources.Load <LE_Object>(p_selectedObject.name); m_objectResourcePath = p_selectedObject.name; m_UID = p_selectedObject.UID; if (m_prefab == null) { Debug.LogWarning("LE_CmdDeleteObject: '" + p_selectedObject.name + "' is not a valid resource path! Command will fail on execution!"); } }
public LE_ObjectDragEvent( LE_Object p_objectPrefab, LE_Object p_objectPreview, bool p_isObjectPlaceable, string p_message, UnityEngine.RaycastHit p_cursorHitInfo) { m_objectPrefab = p_objectPrefab; m_objectPreview = p_objectPreview; m_isObjectPlaceable = p_isObjectPlaceable; m_message = p_message; m_cursorHitInfo = p_cursorHitInfo; }
private void SmartMove(LE_Object p_obj) { // draw the 3d object at the place where it would be placed p_obj.transform.position = m_cursorHitInfo.point; // if object is snapped to grid apply a small offset (needed for example when stacking grid cubes) if (p_obj.SnapType == LE_Object.ESnapType.SNAP_TO_3D_GRID || p_obj.SnapType == LE_Object.ESnapType.SNAP_TO_2D_GRID_AND_TERRAIN) { p_obj.transform.position += m_cursorHitInfo.normal * 0.005f; } // if placement option IsPlacementRotationByNormal is true rotate the object accordingly if (p_obj.IsPlacementRotationByNormal) { p_obj.transform.up = m_cursorHitInfo.normal; } }
// STATIC LOGIC ------------------------------------------------------------------------------------------------------------------- public static void OnNewObjectSnapped(LE_GUI3dObject p_gui3d, LE_Object p_newObject, S_SnapToObjectEventArgs p_args) { if (p_newObject != null) { LE_Object sourceObj = p_args.Source.GetComponentInParent <LE_Object>(); LE_Object destinationObj = p_args.NewInstance.GetComponent <LE_Object>(); // mark source snap point as used p_gui3d.MarkSnapPointAsUsed(sourceObj, destinationObj, p_args.Source); // mark destination snap point as used if (destinationObj.RootSnapPointIndex != -1) { p_gui3d.MarkSnapPointAsUsed(destinationObj, sourceObj, destinationObj.RootSnapPointIndex); } // setup new object OnNewObjectPlaced(p_gui3d, p_newObject); } }
public static void SelectNewObjectAndNotifyListeners(LE_GUI3dObject p_gui3d, LE_Object p_newInstance) { // select new object p_gui3d.SelectObject(p_newInstance); // check if more objects of this kind can be placed p_gui3d.UpdateIsObjectPlaceable(); // notify listeners that the level data was changed if (LE_EventInterface.OnChangeLevelData != null) { LE_EventInterface.OnChangeLevelData(p_newInstance, new LE_LevelDataChangedEvent(LE_ELevelDataChangeType.OBJECT_PLACE)); } // notify listeners that an object has been placed if (LE_EventInterface.OnObjectPlaced != null) { LE_EventInterface.OnObjectPlaced(p_gui3d, new LE_ObjectPlacedEvent(p_newInstance)); } }
public static LE_Object PlaceObject(LE_GUI3dObject p_gui3d, LE_Object p_prefab, Vector3 p_position, Quaternion p_rotation, Vector3 p_scale, string p_objectResourcePath, bool p_isDestroyClonedScripts, int p_customUID) { LE_Object instance = InstantiateObject(p_prefab, p_position, p_rotation, p_scale, p_objectResourcePath); if (p_customUID > 0) { instance.UID = p_customUID; } if (p_isDestroyClonedScripts) { // remove cloned LE_ObjectEditHandle LE_ObjectEditHandle handle = instance.GetComponentInChildren <LE_ObjectEditHandle>(); if (handle != null) { GameObject.Destroy(handle.gameObject); } // remove cloned S_SnapToWorld S_SnapToWorld worldSnap = instance.GetComponent <S_SnapToWorld>(); if (worldSnap != null) { GameObject.Destroy(worldSnap); } // remove cloned S_SnapToGrid S_SnapToGrid gridSnap = instance.GetComponent <S_SnapToGrid>(); if (gridSnap != null) { GameObject.Destroy(gridSnap); } // remove cloned S_SnapToObject S_SnapToObject[] objectSnapArray = instance.GetComponentsInChildren <S_SnapToObject>(true); for (int i = 0; i < objectSnapArray.Length; i++) { LE_ObjectSnapPoint.DestroySnapSystem(objectSnapArray[i]); } // remove cloned UtilityOnDestroyHandler UtilityOnDestroyHandler destroyHandler = instance.GetComponent <UtilityOnDestroyHandler>(); if (destroyHandler != null) { destroyHandler.DestroyWithoutHandling(); } } OnNewObjectPlaced(p_gui3d, instance); return(instance); }
public static void DeleteObject(LE_GUI3dObject p_gui3d, LE_Object p_selectedObject) { if (p_selectedObject != null) { // if this object was snapped to any other object then reactivate the snap point to which this object was attached p_gui3d.ReactivateSnapPoints(p_selectedObject.UID, p_selectedObject.ObjectSnapPoints.Length); // destroy game object GameObject.Destroy(p_selectedObject.gameObject); // some script could search this kind of objects -> mark as deleted p_selectedObject.name = "deleted"; // IsObjectPlaceable could have changed p_gui3d.UpdateIsObjectPlaceable(); // notify listeners that the level data was changed if (LE_EventInterface.OnChangeLevelData != null) { LE_EventInterface.OnChangeLevelData(p_selectedObject.gameObject, new LE_LevelDataChangedEvent(LE_ELevelDataChangeType.OBJECT_DELETE)); } } }
public static void ApplyVariation(LE_Object p_instance, int p_variationIndex) { if (p_instance.Variations.Length == 1) { p_instance.Variations[0].Apply(p_instance); ApplyColor(p_instance, p_instance.ColorProperty); // apply color to the variation } else if (p_instance.Variations.Length > 1) { if (p_variationIndex < 0 || p_variationIndex >= p_instance.Variations.Length) { Debug.LogError("LE_LogicObjects: ApplyVariation: p_variationIndex '" + p_variationIndex + "' is out of bounds [0," + (p_instance.Variations.Length - 1) + "]"); return; } p_instance.VariationsDefaultIndex = p_variationIndex; p_instance.Variations[p_variationIndex].Apply(p_instance); ApplyColor(p_instance, p_instance.ColorProperty); // apply color to the variation } }
public static void AddGridSnapping(LE_GUI3dObject p_gui3d, LE_Object p_newObject, bool p_isPreview) { S_SnapToGrid gridSnap = p_newObject.gameObject.AddComponent <S_SnapToGrid>(); gridSnap.GridOffset = p_newObject.SnapGridOffset; gridSnap.GridCellSize = p_newObject.SnapGridCellSize; gridSnap.SnapCondition = p_isPreview ? S_SnapToGrid.ESnapCondition.ON_UPDATE : S_SnapToGrid.ESnapCondition.WHEN_STILL; gridSnap.IsInstantSnap = p_isPreview; gridSnap.IsSnapAxisXRotation = gridSnap.IsSnapAxisYRotation = gridSnap.IsSnapAxisZRotation = false; // under construction if (p_newObject.SnapType == LE_Object.ESnapType.SNAP_TO_2D_GRID_AND_TERRAIN) { // disable y axis snapping and activate terrain snapping gridSnap.IsSnapAxisY = false; if (!p_isPreview) { AddTerrainSnapping(p_gui3d, p_newObject); } } }
private int GetSnapPointIndex(LE_Object p_obj, S_SnapToObject p_snapScript) { if (p_obj != null) { for (int i = 0; i < p_obj.ObjectSnapPoints.Length; i++) { if (p_obj.ObjectSnapPoints[i].SnapSystemInstance == p_snapScript) { return(i); } } Debug.LogError("LE_GUI3dObject: GetSnapPointIndex: p_snapScript is not a SnapSystemInstance of p_obj!"); return(-1); } else { Debug.LogError("LE_GUI3dObject: GetSnapPointIndex: p_obj is null!"); return(-1); } }
public void SelectNFocusPrefabInstanceInScene() { bool isInstanceOfPrefabSelected = m_selectedObject != null && m_object != null && m_selectedObject.name == m_objectResourcePath; LE_Object firstInstanceOfPrefab = null; // find instances of the selected prefab LE_Object[] objects = Object.FindObjectsOfType <LE_Object>(); for (int i = 0; i < objects.Length; i++) { // check if this is an instance of the selected prefab if (objects[i].name == m_objectResourcePath) { if (isInstanceOfPrefabSelected) { // save the first instance (it will be selected if no later instance is found) if (firstInstanceOfPrefab == null) { firstInstanceOfPrefab = objects[i]; } // an instance of the selected prefab was already selected -> try to find it and slip all instances before this instance if (m_selectedObject == objects[i]) { isInstanceOfPrefabSelected = false; // found the selected instance -> use the next hit or the first found instance } } else { // there was no instance of the selected prefab selected -> select and focus on this instance SelectObject(objects[i]); Focus(); return; } } } // select the first instance if no better match found if (firstInstanceOfPrefab != null) { SelectObject(firstInstanceOfPrefab); Focus(); } }
private bool GetMinMaxBounds(LE_Object p_object, out Vector3 o_min, out Vector3 o_max) { o_min = Vector3.one * 9999999; o_max = -Vector3.one * 9999999; Collider[] colliders = p_object.GetComponentsInChildren <Collider>(); if (colliders.Length > 0) { bool isColliderFound = false; for (int j = 0; j < colliders.Length; j++) { if (colliders[j].GetComponent <LE_ObjectEditHandleCollider>() == null) { isColliderFound = true; Bounds bounds = colliders[j].bounds; o_min = Vector3.Min(o_min, bounds.min); o_max = Vector3.Max(o_max, bounds.max); } } return(isColliderFound); } return(false); }
public static void ApplyRandomity(LE_Object p_newInstance) { // apply rotation randomity Vector3 rndRotation = new Vector3( Mathf.Clamp(p_newInstance.RotationRndEulerX * 0.5f, 0f, 360f), Mathf.Clamp(p_newInstance.RotationRndEulerY * 0.5f, 0f, 360f), Mathf.Clamp(p_newInstance.RotationRndEulerZ * 0.5f, 0f, 360f)); if (rndRotation.sqrMagnitude > 0.0001f) { p_newInstance.transform.localEulerAngles += Vector3.Scale(rndRotation, Random.insideUnitSphere); } // apply scale randomity if (p_newInstance.UniformScaleRnd > 0) { Vector3 axes = new Vector3( p_newInstance.IsScaleableOnX?1f:0f, p_newInstance.IsScaleableOnY?1f:0f, p_newInstance.IsScaleableOnZ?1f:0f); p_newInstance.transform.localScale += axes * p_newInstance.UniformScaleRnd * (Random.value - 0.5f); } }
private void OnAfterObjectSnapped(object p_sender, S_SnapToObjectEventArgs p_args) { if (m_gui3d == null) { Debug.LogError("LE_CmdSnapObjectToObject: OnAfterObjectSnapped: m_gui3d is null!"); return; } LE_Object newObj = p_args.NewInstance.GetComponent <LE_Object>(); if (newObj != null) { if (m_snappedObj.UID > 0) { newObj.UID = m_snappedObj.UID; // reuse snap object UID of the first creation } m_snappedObj.Obj = newObj; LE_LogicObjects.OnNewObjectSnapped(m_gui3d, newObj, p_args); } else { Debug.LogError("LE_CmdSnapObjectToObject: OnAfterObjectSnapped: LE_Object is not attached to the root of the new object! This object will not be saved if it has no LE_Object attached!"); } }