/// <summary> /// This method allows to change all around connected socket(s) state. /// </summary> public void ChangeAreaState(OccupancyType state, LayerMask freeLayer, LayerMask socketLayer, PartBehaviour placedPart) { if (placedPart == null) { return; } if (placedPart.UseConditionalPhysics) { if (!placedPart.CheckStability()) { return; } } Collider[] Colliders = Physics.OverlapBox(placedPart.GetWorldPartMeshBounds().center, placedPart.GetWorldPartMeshBounds().extents, placedPart.transform.rotation, socketLayer, QueryTriggerInteraction.Collide); for (int i = 0; i < Colliders.Length; i++) { SocketBehaviour Socket = Colliders[i].GetComponent <SocketBehaviour>(); if (Socket != null) { if (state == OccupancyType.Busy) { if (Socket.AllowPart(placedPart) && Socket.CheckState(freeLayer, placedPart) == OccupancyType.Busy) { if (Vector3.Distance(placedPart.transform.position, Socket.transform.position) < placedPart.MeshBounds.extents.magnitude) { placedPart.LinkPart(Socket.AttachedPart); Socket.ChangeState(OccupancyType.Busy, placedPart); } } } else { Socket.ChangeState(OccupancyType.Free, placedPart); } } } Colliders = Physics.OverlapSphere(transform.position, Radius, freeLayer); for (int i = 0; i < Colliders.Length; i++) { if (Colliders[i].GetComponent <SocketBehaviour>() == null) { PartBehaviour Part = Colliders[i].GetComponentInParent <PartBehaviour>(); if (Part != null) { if (Part != AttachedPart) { if (AllowPart(Part)) { ChangeState(OccupancyType.Busy, Part); } } } } } }
public override void OnInspectorGUI() { serializedObject.Update(); GUILayout.Space(10); #region Inspector GUILayout.BeginVertical("Easy Build System - Blueprint Data", "window", GUILayout.Height(10)); GUILayout.BeginVertical("box"); GUI.color = MainEditor.GetEditorColor; GUILayout.BeginHorizontal(); GUILayout.Label("Blueprint Data Settings", EditorStyles.largeLabel); #region Blueprint Data Settings GUILayout.FlexibleSpace(); if (GUILayout.Button(DefaultInspector ? "Advanced Inspector" : "Default Inspector", GUILayout.Width(130))) { DefaultInspector = !DefaultInspector; } if (GUILayout.Button(Help ? "Hide Help" : "Show Help", GUILayout.Width(100))) { Help = !Help; } GUI.color = Color.white; GUILayout.EndHorizontal(); if (Help) { EditorGUILayout.HelpBox("This component allows to save and load of Group Behaviour which contains of Parts Behaviour during the edit/runtime.\n" + "The data below can be shared with the people which use the same Parts Collection.\n" + "You can consult the documentation to find more information about this feature.", MessageType.Info); GUI.color = MainEditor.GetEditorColor; if (GUILayout.Button("Open Documentation Link")) { Application.OpenURL(Constants.DOCS_LINK); } GUI.color = Color.white; } if (DefaultInspector) { DrawDefaultInspector(); GUILayout.EndVertical(); GUILayout.EndVertical(); serializedObject.ApplyModifiedProperties(); GUILayout.Space(10); return; } GUI.enabled = false; EditorGUILayout.ObjectField("Script", target, typeof(BlueprintData), true); GUI.enabled = true; GUI.color = Color.white; GUILayout.BeginVertical(); if (Target.Model == null) { GUILayout.BeginHorizontal("box"); GUI.color = new Color(1.5f, 1.5f, 0f); GUILayout.Label("The list does not contains of part(s)."); GUI.color = Color.white; GUILayout.EndHorizontal(); } else { EditorGUILayout.HelpBox("It is important to use the same Parts Collection that was used during the save of the blueprint for loading.", MessageType.Info); GUILayout.Label("Blueprint Data :"); EditorGUI.BeginChangeCheck(); Target.Data = EditorGUILayout.TextArea(Target.Data); if (EditorGUI.EndChangeCheck()) { UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty(); } GUI.color = MainEditor.GetEditorColor; GUI.enabled = Target.Model != null; if (GUILayout.Button("Load Blueprint in Editor")) { BuildManager Manager = FindObjectOfType <BuildManager>(); if (Manager == null) { Debug.Log("<b><color=red>[Easy Build System]</color></b> : The build manager does not exists."); return; } List <PartModel.SerializedPart> Parts = Target.Model.DecodeToStr(Target.Data); GroupBehaviour Parent = new GameObject("(Editor) Blueprint").AddComponent <GroupBehaviour>(); for (int i = 0; i < Parts.Count; i++) { PartBehaviour Part = Manager.GetPart(Parts[i].Id); PartBehaviour PlacedPart = Manager.PlacePrefab(Part, PartModel.ParseToVector3(Parts[i].Position), PartModel.ParseToVector3(Parts[i].Rotation), PartModel.ParseToVector3(Parts[i].Scale), Parent); PlacedPart.ChangeAppearance(Parts[i].AppearanceIndex); } } GUI.enabled = true; GUI.enabled = Application.isPlaying; if (GUILayout.Button("Load Blueprint in Runtime")) { BuildManager Manager = FindObjectOfType <BuildManager>(); if (Manager == null) { Debug.Log("<b><color=red>[Easy Build System]</color></b> : The build manager does not exists."); return; } List <PartModel.SerializedPart> Parts = Target.Model.DecodeToStr(Target.Data); GroupBehaviour Group = new GameObject("(Runtime) Blueprint").AddComponent <GroupBehaviour>(); for (int i = 0; i < Parts.Count; i++) { PartBehaviour Part = Manager.GetPart(Parts[i].Id); PartBehaviour PlacedPart = Manager.PlacePrefab(Part, PartModel.ParseToVector3(Parts[i].Position), PartModel.ParseToVector3(Parts[i].Rotation), PartModel.ParseToVector3(Parts[i].Scale), Group); PlacedPart.ChangeAppearance(Parts[i].AppearanceIndex); } } GUI.enabled = true; GUI.color = Color.white; } GUILayout.EndVertical(); #endregion Blueprint Data Settings GUILayout.EndVertical(); GUILayout.EndVertical(); #endregion Inspector serializedObject.ApplyModifiedProperties(); GUILayout.Space(10); }
public PartOffset(PartBehaviour part) { Part = part; }
public static void CreateNewPart() { if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent <BuildManager>() == null) { if (Selection.activeGameObject.GetComponentInParent <PartBehaviour>() == null) { GameObject Parent = new GameObject("Part " + Selection.activeGameObject.name); string LocalPath = EditorUtility.SaveFilePanel("Save Path to Part (" + Parent.name + ")", "", Parent.name + ".prefab", "prefab"); if (LocalPath == string.Empty) { DestroyImmediate(Parent); return; } try { LocalPath = LocalPath.Substring(LocalPath.LastIndexOf("Assets")); } catch { return; } if (LocalPath != string.Empty) { Selection.activeGameObject.transform.SetParent(Parent.transform, false); Selection.activeGameObject.transform.position = Vector3.zero; PartBehaviour Temp = Parent.AddComponent <PartBehaviour>(); Temp.Name = Selection.activeGameObject.name; Temp.MeshBounds = Parent.GetChildsBounds(); Debug.Log("<b><color=cyan>[Easy Build System]</color></b> : Bounds generated " + Temp.MeshBounds.size.ToString() + "."); #if UNITY_2018_3 || UNITY_2019 UnityEngine.Object AssetPrefab = PrefabUtility.SaveAsPrefabAssetAndConnect(Temp.gameObject, LocalPath, InteractionMode.UserAction); EditorGUIUtility.PingObject(AssetPrefab); #else UnityEngine.Object AssetPrefab = PrefabUtility.CreateEmptyPrefab(LocalPath); GameObject Asset = PrefabUtility.ReplacePrefab(Parent, AssetPrefab, ReplacePrefabOptions.ConnectToPrefab); AssetDatabase.Refresh(); EditorGUIUtility.PingObject(Asset); #endif SceneHelper.Focus(Parent, DrawCameraMode.Textured); Debug.Log("<b><color=cyan>[Easy Build System]</color></b> : The part has been created !"); } } else { Debug.LogError("<b><color=red>[Easy Build System]</color></b> : This selected object has already a Base Part component."); } } else { if (!EditorUtility.DisplayDialog("Easy Build System - Information", "You've not selected object.\nDo you want create a empty Base Part ?", "Yes", "No")) { return; } GameObject Parent = new GameObject("New Part"); string LocalPath = EditorUtility.SaveFilePanel("Save Path to Part (" + Parent.name + ")", "", Parent.name + ".prefab", "prefab"); if (LocalPath == string.Empty) { DestroyImmediate(Parent); return; } try { LocalPath = LocalPath.Substring(LocalPath.LastIndexOf("Assets")); } catch { return; } if (LocalPath != string.Empty) { Parent.AddComponent <PartBehaviour>(); #if UNITY_2018_3 GameObject Asset = PrefabUtility.SaveAsPrefabAssetAndConnect(Parent, LocalPath, InteractionMode.UserAction); EditorGUIUtility.PingObject(Asset); #elif UNITY_2018 UnityEngine.Object AssetPrefab = PrefabUtility.CreateEmptyPrefab(LocalPath); GameObject Asset = PrefabUtility.ReplacePrefab(Parent, AssetPrefab, ReplacePrefabOptions.ConnectToPrefab); AssetDatabase.Refresh(); EditorGUIUtility.PingObject(Asset); #endif SceneHelper.Focus(Parent, DrawCameraMode.Textured); Debug.Log("<b><color=cyan>[Easy Build System]</color></b> : The part parent has been created, you can now add your mesh(s) and configure it."); } } }
/// <summary> /// This method allows to move the preview only on available socket(s). /// </summary> public void UpdateSnapsMovement(SocketBehaviour[] sockets) { if (CurrentPreview == null) { return; } float ClosestAngle = Mathf.Infinity; CurrentSocket = null; for (int i = 0; i < sockets.Length; i++) { PartBehaviour Part = sockets[i].AttachedPart; if (Part == null || Part.Sockets.Length == 0) { continue; } for (int x = 0; x < Part.Sockets.Length; x++) { SocketBehaviour Socket = RayDetection == DetectionType.Overlap ? Part.Sockets[x] : sockets[i]; if (Socket != null) { if (Socket.gameObject.activeSelf && !Socket.IsDisabled) { if (Socket.AllowPart(SelectedPrefab) && !Part.AvoidAnchoredOnSocket) { if (RayDetection == DetectionType.Overlap) { if ((Socket.transform.position - (CameraType != RayType.TopDown ? GetTransform.position : TopDownHit.point)).sqrMagnitude < Mathf.Pow(CameraType != RayType.TopDown ? ActionDistance : SnapThreshold, 2)) { float Angle = Vector3.Angle(GetRay().direction, Socket.transform.position - GetRay().origin); if (Angle < ClosestAngle && Angle < OverlapAngles) { ClosestAngle = Angle; if (CameraType != RayType.TopDown && CurrentSocket == null) { CurrentSocket = Socket; } else { CurrentSocket = Socket; } } } } else { CurrentSocket = Socket; } } } } } } if (CurrentSocket != null) { PartOffset Offset = CurrentSocket.GetOffsetPart(SelectedPrefab.Id); if (CurrentSocket.CheckOccupancy(SelectedPrefab)) { return; } if (Offset != null) { CurrentPreview.transform.position = CurrentSocket.transform.position + CurrentSocket.transform.TransformVector(Offset.Position); CurrentPreview.transform.rotation = CurrentSocket.transform.rotation * (CurrentPreview.RotateOnSockets ? Quaternion.Euler(Offset.Rotation + CurrentRotationOffset) : Quaternion.Euler(Offset.Rotation)); if (Offset.UseCustomScale) { CurrentPreview.transform.localScale = Offset.Scale; } LastSocket = CurrentSocket; HasSocket = true; return; } } UpdateFreeMovement(); }
//initialize #region public void Initialize(string _name, List <Connection> _connections, int?_id, int _templateId, Mesh _geometry = null, List <Mesh> _collider = null, int?_parent = null) { name = _name; parent = _parent; id = _id; templateID = _templateId; geometry = _geometry; int count = 0; foreach (Connection _conn in _connections) { _conn.Pln.Parent = transform; connections.Add(_conn); activeConnections.Add(count); ++count; _conn.GenerateRulesTable(GlobalReferences.Rules); _conn.ParentPart = this; } //check geometry and colliders #region if (geometry != null && (gameObject.GetComponent <MeshFilter>() != null || gameObject.GetComponent <MeshRenderer>() != null)) { if (gameObject.GetComponent <MeshFilter>() != null) { Destroy(gameObject.GetComponent <MeshFilter>()); } if (gameObject.GetComponent <MeshRenderer>() != null) { Destroy(gameObject.GetComponent <MeshRenderer>()); } } if (geometry != null) { MeshFilter mf = gameObject.AddComponent <MeshFilter>(); mf.mesh = geometry; MeshRenderer mr = gameObject.AddComponent <MeshRenderer>(); mr.material = Resources.Load <Material>("Materials/unaffectedMaterial"); } else if (geometry == null && (gameObject.GetComponent <MeshFilter>() == null || gameObject.GetComponent <MeshRenderer>() == null)) { throw new System.Exception("Get your Geometry straight!"); } else { geometry = gameObject.GetComponent <MeshFilter>().sharedMesh; } if (_collider == null && gameObject.GetComponent <MeshCollider>() == null && gameObject.GetComponent <BoxCollider>() == null && gameObject.GetComponent <SphereCollider>() == null && gameObject.GetComponent <CapsuleCollider>() == null) { MeshCollider mc = gameObject.AddComponent <MeshCollider>(); mc.convex = true; mc.sharedMesh = geometry; } else if (_collider != null) { int j = 0; foreach (Mesh m in _collider) { MeshCollider mc = gameObject.AddComponent <MeshCollider>(); mc.convex = true; mc.sharedMesh = m; ++j; } } #endregion //Add Components #region if (gameObject.GetComponent <Rigidbody>() == null) { Rigidbody rg = gameObject.AddComponent <Rigidbody>(); rg.useGravity = false; rg.drag = 1; rg.angularDrag = 3; } if (gameObject.GetComponent <ConstantForce>() == null) { gameObject.AddComponent <ConstantForce>(); } if (gameObject.GetComponent <PartBehaviour>() == null) { PartBehaviour behav = gameObject.AddComponent <PartBehaviour>(); behav.enabled = false; } if (gameObject.GetComponent <ConnectionScanning>() == null) { ConnectionScanning scan = gameObject.AddComponent <ConnectionScanning>(); scan.enabled = false; } #endregion }
private void OnDestroyedPart(PartBehaviour part) { UpdateMeshData(); }
private void Awake() { //Get the Base Part component in this object. Part = GetComponent <PartBehaviour>(); }
public Occupancy(PartBehaviour part) { Part = part; }
private void OnPlacedPart(PartBehaviour part, SocketBehaviour socket) { UpdateMeshData(); }
private void Awake() { Part = GetComponent <PartBehaviour>(); }
/// <summary> /// This method allows to check if the part (by id) exists in the AllowPartPlacement list. /// </summary> public bool CheckAllowedPart(PartBehaviour part) { return(AllowPartPlacement.Find(entry => entry.Id == part.Id)); }
public void SetActivePart(PartBehaviour partBehaviour) { activePart = partBehaviour; }
public void GoToPickState() { activePart = null; partPickerManager.enabled = true; cameraBehaviour.target = null; }
private void Awake() { part = GetComponent <PartBehaviour>(); InvokeRepeating("UpdateAppearance", 1f, 1f); }