public void OnAwake(NavigationMesh defaultNavMesh) { navigationEngine = null; ResetEngine(); // Turn off all NavMesh objects NavigationMesh[] navMeshes = FindObjectsOfType(typeof(NavigationMesh)) as NavigationMesh[]; foreach (NavigationMesh _navMesh in navMeshes) { if (defaultNavMesh != _navMesh) { _navMesh.TurnOff(); } } if (navigationEngine == null || navigationEngine.RequiresNavMeshGameObject) { if (defaultNavMesh != null) { defaultNavMesh.TurnOn(); } else if (navigationEngine != null) { AC.Char[] allChars = FindObjectsOfType(typeof(AC.Char)) as AC.Char[]; if (allChars.Length > 0) { ACDebug.LogWarning("No NavMesh set. Characters will not be able to PathFind until one is defined - please choose one using the Scene Manager."); } } } }
public override void AssignValues(List<ActionParameter> parameters) { if (sceneSetting == SceneSetting.DefaultNavMesh) { if (KickStarter.sceneSettings.navigationMethod == AC_NavigationMethod.PolygonCollider && changeNavMeshMethod == ChangeNavMeshMethod.ChangeNumberOfHoles) { hole = AssignFile <PolygonCollider2D> (parameters, parameterID, constantID, hole); replaceHole = AssignFile <PolygonCollider2D> (parameters, replaceParameterID, replaceConstantID, replaceHole); } else { newNavMesh = AssignFile <NavigationMesh> (parameters, parameterID, constantID, newNavMesh); } } else if (sceneSetting == SceneSetting.DefaultPlayerStart) { playerStart = AssignFile <PlayerStart> (parameters, parameterID, constantID, playerStart); } else if (sceneSetting == SceneSetting.SortingMap) { sortingMap = AssignFile <SortingMap> (parameters, parameterID, constantID, sortingMap); } else if (sceneSetting == SceneSetting.SortingMap) { tintMap = AssignFile <TintMap> (parameters, parameterID, constantID, tintMap); } else if (sceneSetting == SceneSetting.OnLoadCutscene || sceneSetting == SceneSetting.OnStartCutscene) { cutscene = AssignFile <Cutscene> (parameters, parameterID, constantID, cutscene); } }
/** * <summary>Deserialises a string of data, and restores the GameObject to its previous state.</summary> * <param name = "stringData">The data, serialised as a string</param> */ public override void LoadData(string stringData) { NavMesh2DData data = Serializer.LoadScriptData <NavMesh2DData> (stringData); if (data == null) { return; } SavePrevented = data.savePrevented; if (savePrevented) { return; } if (GetComponent <NavigationMesh>()) { NavigationMesh navMesh = GetComponent <NavigationMesh>(); navMesh.polygonColliderHoles.Clear(); KickStarter.navigationManager.navigationEngine.ResetHoles(navMesh); if (!string.IsNullOrEmpty(data._linkedIDs)) { int[] linkedIDs = StringToIntArray(data._linkedIDs); for (int i = 0; i < linkedIDs.Length; i++) { PolygonCollider2D polyHole = Serializer.returnComponent <PolygonCollider2D> (linkedIDs[i]); if (polyHole != null) { navMesh.AddHole(polyHole); } } } } }
private void ResetHoles(NavigationMesh navMesh, bool changesMade) { if (changesMade) { navMesh.ResetHoles(); } }
override public void AssignValues(List <ActionParameter> parameters) { if (sceneSetting == SceneSetting.DefaultNavMesh) { if (KickStarter.sceneSettings.navigationMethod == AC_NavigationMethod.PolygonCollider && changeNavMeshMethod == ChangeNavMeshMethod.ChangeNumberOfHoles) { hole = AssignFile <PolygonCollider2D> (parameters, parameterID, constantID, hole); replaceHole = AssignFile <PolygonCollider2D> (parameters, replaceParameterID, replaceConstantID, replaceHole); } else { newNavMesh = AssignFile <NavigationMesh> (parameters, parameterID, constantID, newNavMesh); } } else if (sceneSetting == SceneSetting.DefaultPlayerStart) { playerStart = AssignFile <PlayerStart> (parameters, parameterID, constantID, playerStart); } else if (sceneSetting == SceneSetting.SortingMap) { sortingMap = AssignFile <SortingMap> (parameters, parameterID, constantID, sortingMap); } else if (sceneSetting == SceneSetting.TintMap) { tintMap = AssignFile <TintMap> (parameters, parameterID, constantID, tintMap); } else if (sceneSetting == SceneSetting.OnLoadCutscene || sceneSetting == SceneSetting.OnStartCutscene) { cutscene = AssignFile <Cutscene> (parameters, parameterID, constantID, cutscene); } }
public override void OnReset(NavigationMesh navMesh) { if (!Application.isPlaying) { return; } is2D = true; ResetHoles(navMesh); if (navMesh != null && navMesh.characterEvasion != CharacterEvasion.None && navMesh.GetComponent <PolygonCollider2D>() != null) { PolygonCollider2D[] polys = navMesh.GetComponents <PolygonCollider2D>(); if (polys != null && polys.Length > 1) { ACDebug.LogWarning("Character evasion cannot occur for multiple PolygonColliders - only the first on the active NavMesh will be affected."); } for (int i = 0; i < polys.Length; i++) { if (!polys[i].isTrigger) { ACDebug.LogWarning("The PolygonCollider2D on " + navMesh.gameObject.name + " is not a Trigger."); } } } }
public override void OnReset(NavigationMesh navMesh) { if (!Application.isPlaying) { return; } is2D = true; ResetHoles(navMesh); if (navMesh != null && navMesh.characterEvasion != CharacterEvasion.None && navMesh.GetComponent <PolygonCollider2D>() != null) { PolygonCollider2D[] polys = navMesh.GetComponents <PolygonCollider2D>(); if (polys != null && polys.Length > 1) { ACDebug.LogWarning("Character evasion cannot occur for multiple PolygonColliders - only the first on the active NavMesh will be affected."); } for (int i = 0; i < polys.Length; i++) { if (!polys[i].isTrigger) { ACDebug.LogWarning("The PolygonCollider2D on " + navMesh.gameObject.name + " is not a Trigger."); } } } if (navMesh == null && KickStarter.settingsManager != null && KickStarter.settingsManager.movementMethod == MovementMethod.PointAndClick) { ACDebug.LogWarning("Could not initialise NavMesh - was one set as the Default in the Settings Manager?"); } }
override public void AssignValues (List<ActionParameter> parameters) { SceneSettings sceneSettings = GameObject.FindWithTag (Tags.gameEngine).GetComponent <SceneSettings>(); if (sceneSetting == SceneSetting.DefaultNavMesh) { if (sceneSettings.navigationMethod == AC_NavigationMethod.PolygonCollider && changeNavMeshMethod == ChangeNavMeshMethod.ChangeNumberOfHoles) { hole = AssignFile <PolygonCollider2D> (parameters, parameterID, constantID, hole); } else { newNavMesh = AssignFile <NavigationMesh> (parameters, parameterID, constantID, newNavMesh); } } else if (sceneSetting == SceneSetting.DefaultPlayerStart) { playerStart = AssignFile <PlayerStart> (parameters, parameterID, constantID, playerStart); } else if (sceneSetting == SceneSetting.SortingMap) { sortingMap = AssignFile <SortingMap> (parameters, parameterID, constantID, sortingMap); } else if (sceneSetting == SceneSetting.OnLoadCutscene || sceneSetting == SceneSetting.OnStartCutscene) { cutscene = AssignFile <Cutscene> (parameters, parameterID, constantID, cutscene); } }
public override void SceneSettingsGUI() { #if UNITY_EDITOR EditorGUILayout.BeginHorizontal(); KickStarter.sceneSettings.navMesh = (NavigationMesh)EditorGUILayout.ObjectField("Default NavMesh:", KickStarter.sceneSettings.navMesh, typeof(NavigationMesh), true); if (!SceneSettings.IsUnity2D()) { EditorGUILayout.EndHorizontal(); EditorGUILayout.HelpBox("This pathfinding method is only compatible with 'Unity 2D' mode.", MessageType.Warning); EditorGUILayout.BeginHorizontal(); } else if (KickStarter.sceneSettings.navMesh == null) { if (GUILayout.Button("Create", GUILayout.MaxWidth(60f))) { NavigationMesh newNavMesh = null; newNavMesh = SceneManager.AddPrefab("Navigation", "NavMesh2D", true, false, true).GetComponent <NavigationMesh>(); newNavMesh.gameObject.name = "Default NavMesh"; KickStarter.sceneSettings.navMesh = newNavMesh; EditorGUIUtility.PingObject(newNavMesh.gameObject); } } EditorGUILayout.EndHorizontal(); #endif }
/** * <summary>Deserialises a string of data, and restores the GameObject to its previous state.</summary> * <param name = "stringData">The data, serialised as a string</param> */ public override void LoadData(string stringData) { NavMesh2DData data = Serializer.LoadScriptData <NavMesh2DData> (stringData); if (data == null) { return; } if (GetComponent <NavigationMesh>()) { NavigationMesh navMesh = GetComponent <NavigationMesh>(); navMesh.polygonColliderHoles.Clear(); if (data._linkedIDs.Length > 0) { int[] linkedIDs = StringToIntArray(data._linkedIDs); for (int i = 0; i < linkedIDs.Length; i++) { PolygonCollider2D polyHole = Serializer.returnComponent <PolygonCollider2D> (linkedIDs[i]); if (polyHole != null) { navMesh.AddHole(polyHole); } } } } }
/** * <summary>Serialises appropriate GameObject values into a string.</summary> * <returns>The data, serialised as a string</returns> */ public override string SaveData() { NavMesh2DData navMesh2DData = new NavMesh2DData(); navMesh2DData.objectID = constantID; navMesh2DData.savePrevented = savePrevented; if (GetComponent <NavigationMesh>()) { NavigationMesh navMesh = GetComponent <NavigationMesh>(); List <int> linkedIDs = new List <int>(); for (int i = 0; i < navMesh.polygonColliderHoles.Count; i++) { if (navMesh.polygonColliderHoles[i].GetComponent <ConstantID>()) { linkedIDs.Add(navMesh.polygonColliderHoles[i].GetComponent <ConstantID>().constantID); } else { ACDebug.LogWarning("Cannot save " + this.gameObject.name + "'s holes because " + navMesh.polygonColliderHoles[i].gameObject.name + " has no Constant ID!"); } } navMesh2DData._linkedIDs = ArrayToString <int> (linkedIDs.ToArray()); } return(Serializer.SaveScriptData <NavMesh2DData> (navMesh2DData)); }
/** * Provides a space for any custom Editor GUI code that should be displayed in the NavigationMesh inspector. */ public virtual NavigationMesh NavigationMeshGUI(NavigationMesh _target) { _target.disableRenderer = CustomGUILayout.ToggleLeft("Disable mesh renderer?", _target.disableRenderer, "", "If True, the MeshRenderer will be disabled when the game begins"); #if UNITY_5 || UNITY_2017_1_OR_NEWER _target.ignoreCollisions = CustomGUILayout.ToggleLeft("Ignore collisions?", _target.ignoreCollisions, "", "If True, then Physics collisions with this GameObject's Collider will be disabled"); #endif return(_target); }
/** * Provides a space for any custom Editor GUI code that should be displayed in the NavigationMesh inspector. */ public virtual NavigationMesh NavigationMeshGUI(NavigationMesh _target) { _target.disableRenderer = EditorGUILayout.ToggleLeft("Disable mesh renderer?", _target.disableRenderer); #if UNITY_5 || UNITY_2017_1_OR_NEWER _target.ignoreCollisions = EditorGUILayout.ToggleLeft("Ignore collisions?", _target.ignoreCollisions); #endif return(_target); }
/** * <summary>Creates a new instance of the 'Scene: Change setting' Action, set to change the default NavMesh</summary> * <param name = "newNavMesh">The new NavMesh</param> * <returns>The generated Action</returns> */ public static ActionNavMesh CreateNew_ChangeDefaultNavMesh(NavigationMesh newNavMesh) { ActionNavMesh newAction = (ActionNavMesh)CreateInstance <ActionNavMesh>(); newAction.sceneSetting = SceneSetting.DefaultNavMesh; newAction.changeNavMeshMethod = ChangeNavMeshMethod.ChangeNavMesh; newAction.newNavMesh = newNavMesh; return(newAction); }
protected void ResetHoles(NavigationMesh navMesh, bool rebuild) { if (navMesh == null) { return; } CalcSearchRadius(navMesh); PolygonCollider2D[] polys = navMesh.PolygonCollider2Ds; if (polys == null || polys.Length == 0) { return; } for (int p = 0; p < polys.Length; p++) { polys[p].pathCount = 1; // Holes can only go in the first polygon if (p > 0 || navMesh.polygonColliderHoles.Count == 0) { if (rebuild) { RebuildVertexArray(navMesh.transform, polys[p], p); CreateCache(p); } continue; } Vector2 scaleFac = new Vector2(1f / navMesh.transform.localScale.x, 1f / navMesh.transform.localScale.y); foreach (PolygonCollider2D hole in navMesh.polygonColliderHoles) { if (hole != null) { polys[p].pathCount++; List <Vector2> newPoints = new List <Vector2>(); foreach (Vector2 holePoint in hole.points) { Vector2 relativePosition = hole.transform.TransformPoint(holePoint) - navMesh.transform.position; newPoints.Add(new Vector2(relativePosition.x * scaleFac.x, relativePosition.y * scaleFac.y)); } polys[p].SetPath(polys[p].pathCount - 1, newPoints.ToArray()); hole.gameObject.layer = LayerMask.NameToLayer(KickStarter.settingsManager.deactivatedLayer); hole.isTrigger = true; } } if (rebuild) { RebuildVertexArray(navMesh.transform, polys[p], p); CreateCache(p); } } }
public override NavigationMesh NavigationMeshGUI(NavigationMesh _target) { _target = base.NavigationMeshGUI(_target); _target.characterEvasion = (CharacterEvasion)EditorGUILayout.EnumPopup("Character evasion:", _target.characterEvasion); if (_target.characterEvasion != CharacterEvasion.None) { _target.characterEvasionPoints = (CharacterEvasionPoints)EditorGUILayout.EnumPopup("Evasion accuracy:", _target.characterEvasionPoints); _target.characterEvasionYScale = EditorGUILayout.Slider("Evasion y-scale:", _target.characterEvasionYScale, 0.1f, 1f); EditorGUILayout.HelpBox("Note: Characters can only be avoided if they have a Circle Collider 2D (no Trigger) component on their base.\n\n" + "For best results, set a non-zero 'Pathfinding update time' in the Settings Manager.", MessageType.Info); if (_target.transform.lossyScale != Vector3.one) { EditorGUILayout.HelpBox("For character evasion to work, the NavMesh must have a unit scale (1,1,1).", MessageType.Warning); } #if UNITY_ANDROID || UNITY_IOS EditorGUILayout.HelpBox("This is an expensive calculation - consider setting this to 'None' for mobile platforms.", MessageType.Warning); #endif } _target.accuracy = EditorGUILayout.Slider("Accuracy:", _target.accuracy, 0f, 1f); int numOptions = _target.polygonColliderHoles.Count; numOptions = EditorGUILayout.IntField("Number of holes:", _target.polygonColliderHoles.Count); if (numOptions < 0) { numOptions = 0; } if (numOptions < _target.polygonColliderHoles.Count) { _target.polygonColliderHoles.RemoveRange(numOptions, _target.polygonColliderHoles.Count - numOptions); } else if (numOptions > _target.polygonColliderHoles.Count) { if (numOptions > _target.polygonColliderHoles.Capacity) { _target.polygonColliderHoles.Capacity = numOptions; } for (int i = _target.polygonColliderHoles.Count; i < numOptions; i++) { _target.polygonColliderHoles.Add(null); } } for (int i = 0; i < _target.polygonColliderHoles.Count; i++) { _target.polygonColliderHoles [i] = (PolygonCollider2D)EditorGUILayout.ObjectField("Hole #" + i.ToString() + ":", _target.polygonColliderHoles [i], typeof(PolygonCollider2D), true); } return(_target); }
public override void OnReset(NavigationMesh navMesh) { if (!Application.isPlaying) { return; } if (navMesh == null && KickStarter.settingsManager != null && KickStarter.settingsManager.movementMethod == MovementMethod.PointAndClick) { ACDebug.LogWarning("Could not initialise NavMesh - was one set as the Default in the Settings Manager?"); } }
private void ResetHoles(NavigationMesh navMesh, bool rebuild) { if (navMesh == null || navMesh.GetComponent <PolygonCollider2D>() == null) { return; } CalcSearchRadius(navMesh); PolygonCollider2D poly = navMesh.GetComponent <PolygonCollider2D>(); poly.pathCount = 1; if (navMesh.polygonColliderHoles.Count == 0) { if (rebuild) { RebuildVertexArray(navMesh.transform, poly); CreateCache(); } return; } Vector2 scaleFac = new Vector2(1f / navMesh.transform.localScale.x, 1f / navMesh.transform.localScale.y); foreach (PolygonCollider2D hole in navMesh.polygonColliderHoles) { if (hole != null) { poly.pathCount++; List <Vector2> newPoints = new List <Vector2>(); foreach (Vector2 holePoint in hole.points) { Vector2 relativePosition = hole.transform.TransformPoint(holePoint) - navMesh.transform.position; newPoints.Add(new Vector2(relativePosition.x * scaleFac.x, relativePosition.y * scaleFac.y)); } poly.SetPath(poly.pathCount - 1, newPoints.ToArray()); hole.gameObject.layer = LayerMask.NameToLayer(KickStarter.settingsManager.deactivatedLayer); hole.isTrigger = true; } } if (rebuild) { RebuildVertexArray(navMesh.transform, poly); CreateCache(); } }
public override void OnInspectorGUI() { NavigationMesh _target = (NavigationMesh)target; _target.disableRenderer = EditorGUILayout.ToggleLeft("Disable mesh renderer?", _target.disableRenderer); #if UNITY_5 _target.ignoreCollisions = EditorGUILayout.ToggleLeft("Ignore collisions?", _target.ignoreCollisions); #endif if (AdvGame.GetReferences() && AdvGame.GetReferences().settingsManager&& AdvGame.GetReferences().settingsManager.IsUnity2D()) { _target.moveAroundChars = EditorGUILayout.ToggleLeft("Attempt to move around characters when pathfinding?", _target.moveAroundChars); int numOptions = _target.polygonColliderHoles.Count; numOptions = EditorGUILayout.IntField("Number of holes:", _target.polygonColliderHoles.Count); if (numOptions < 0) { numOptions = 0; } if (numOptions < _target.polygonColliderHoles.Count) { _target.polygonColliderHoles.RemoveRange(numOptions, _target.polygonColliderHoles.Count - numOptions); } else if (numOptions > _target.polygonColliderHoles.Count) { if (numOptions > _target.polygonColliderHoles.Capacity) { _target.polygonColliderHoles.Capacity = numOptions; } for (int i = _target.polygonColliderHoles.Count; i < numOptions; i++) { _target.polygonColliderHoles.Add(null); } } for (int i = 0; i < _target.polygonColliderHoles.Count; i++) { _target.polygonColliderHoles [i] = (PolygonCollider2D)EditorGUILayout.ObjectField("Hole #" + i.ToString() + ":", _target.polygonColliderHoles [i], typeof(PolygonCollider2D), true); } } if (GUI.changed) { EditorUtility.SetDirty(_target); } }
public override void OnInspectorGUI() { NavigationMesh _target = (NavigationMesh)target; if (KickStarter.navigationManager) { KickStarter.navigationManager.ResetEngine(); if (KickStarter.navigationManager.navigationEngine != null) { EditorGUILayout.BeginVertical("Button"); _target = KickStarter.navigationManager.navigationEngine.NavigationMeshGUI(_target); EditorGUILayout.EndVertical(); } } UnityVersionHandler.CustomSetDirty(_target); }
public override void ResetHoles(NavigationMesh navMesh) { if (navMesh == null || navMesh.GetComponent <MeshCollider>() == null || navMesh.GetComponent <MeshCollider>().sharedMesh == null) { return; } if (navMesh.GetComponent <MeshFilter>() && navMesh.GetComponent <MeshFilter>().sharedMesh) { navMesh.GetComponent <MeshCollider>().sharedMesh = navMesh.GetComponent <MeshFilter>().sharedMesh; ACDebug.LogWarning(navMesh.gameObject.name + " has no MeshCollider mesh - temporarily using MeshFilter mesh instead."); } else { ACDebug.LogWarning(navMesh.gameObject.name + " has no MeshCollider mesh."); } }
private void UnloadNavMesh(int navMeshInt) { NavigationMesh navMesh = Serializer.returnComponent <NavigationMesh> (navMeshInt); if (navMesh && KickStarter.sceneSettings && KickStarter.sceneSettings.navigationMethod != AC_NavigationMethod.UnityNavigation) { if (KickStarter.sceneSettings.navMesh) { NavigationMesh oldNavMesh = KickStarter.sceneSettings.navMesh; oldNavMesh.TurnOff(); } //navMesh.collider.GetComponent <NavigationMesh>().TurnOn (); navMesh.TurnOn(); KickStarter.sceneSettings.navMesh = navMesh; } }
public override NavigationMesh NavigationMeshGUI(NavigationMesh _target) { _target = base.NavigationMeshGUI(_target); _target.characterEvasion = (CharacterEvasion)EditorGUILayout.EnumPopup("Character evasion:", _target.characterEvasion); if (_target.characterEvasion != CharacterEvasion.None) { EditorGUILayout.HelpBox("Note: Characters can only be avoided if they have a Circle Collider 2D (no Trigger) component on their base.\n\n" + "For best results, set a non-zero 'Pathfinding update time' in the Settings Manager.", MessageType.Info); } int numOptions = _target.polygonColliderHoles.Count; numOptions = EditorGUILayout.IntField("Number of holes:", _target.polygonColliderHoles.Count); if (numOptions < 0) { numOptions = 0; } if (numOptions < _target.polygonColliderHoles.Count) { _target.polygonColliderHoles.RemoveRange(numOptions, _target.polygonColliderHoles.Count - numOptions); } else if (numOptions > _target.polygonColliderHoles.Count) { if (numOptions > _target.polygonColliderHoles.Capacity) { _target.polygonColliderHoles.Capacity = numOptions; } for (int i = _target.polygonColliderHoles.Count; i < numOptions; i++) { _target.polygonColliderHoles.Add(null); } } for (int i = 0; i < _target.polygonColliderHoles.Count; i++) { _target.polygonColliderHoles [i] = (PolygonCollider2D)EditorGUILayout.ObjectField("Hole #" + i.ToString() + ":", _target.polygonColliderHoles [i], typeof(PolygonCollider2D), true); } return(_target); }
public override void AssignValues(List <ActionParameter> parameters) { if (sceneSettings == null) { return; } switch (sceneSetting) { case SceneSetting.DefaultNavMesh: if (KickStarter.sceneSettings.navigationMethod == AC_NavigationMethod.PolygonCollider && changeNavMeshMethod == ChangeNavMeshMethod.ChangeNumberOfHoles) { runtimeHole = AssignFile <PolygonCollider2D> (parameters, parameterID, constantID, hole); runtimeReplaceHole = AssignFile <PolygonCollider2D> (parameters, replaceParameterID, replaceConstantID, replaceHole); runtimeNewNavMesh = null; } else { runtimeHole = null; runtimeReplaceHole = null; runtimeNewNavMesh = AssignFile <NavigationMesh> (parameters, parameterID, constantID, newNavMesh); } break; case SceneSetting.DefaultPlayerStart: runtimePlayerStart = AssignFile <PlayerStart> (parameters, parameterID, constantID, playerStart); break; case SceneSetting.SortingMap: runtimeSortingMap = AssignFile <SortingMap> (parameters, parameterID, constantID, sortingMap); break; case SceneSetting.TintMap: runtimeTintMap = AssignFile <TintMap> (parameters, parameterID, constantID, tintMap); break; case SceneSetting.OnLoadCutscene: case SceneSetting.OnStartCutscene: runtimeCutscene = AssignFile <Cutscene> (parameters, parameterID, constantID, cutscene); break; } }
private void UnloadNavMesh(int navMeshInt, SceneSettings sceneSettings) { NavigationMesh navMesh = Serializer.returnComponent <NavigationMesh> (navMeshInt, sceneSettings.gameObject); if (navMesh && sceneSettings && sceneSettings.navigationMethod != AC_NavigationMethod.UnityNavigation) { if (sceneSettings.navMesh) { NavigationMesh oldNavMesh = sceneSettings.navMesh; oldNavMesh.TurnOff(); } navMesh.TurnOn(); sceneSettings.navMesh = navMesh; // Bugfix: Need to cycle this otherwise weight caching doesn't always work navMesh.TurnOff(); navMesh.TurnOn(); } }
public override void OnInspectorGUI() { NavigationMesh _target = (NavigationMesh)target; if (KickStarter.navigationManager) { KickStarter.navigationManager.ResetEngine(); if (KickStarter.navigationManager.navigationEngine != null) { EditorGUILayout.BeginVertical("Button"); _target = KickStarter.navigationManager.navigationEngine.NavigationMeshGUI(_target); EditorGUILayout.EndVertical(); } } if (GUI.changed) { EditorUtility.SetDirty(_target); } }
public override void TurnOn(NavigationMesh navMesh) { if (navMesh == null || KickStarter.settingsManager == null) { return; } if (LayerMask.NameToLayer(KickStarter.settingsManager.navMeshLayer) == -1) { ACDebug.LogError("Can't find layer " + KickStarter.settingsManager.navMeshLayer + " - please define it in Unity's Tags Manager (Edit -> Project settings -> Tags and Layers)."); } else if (!string.IsNullOrEmpty(KickStarter.settingsManager.navMeshLayer)) { navMesh.gameObject.layer = LayerMask.NameToLayer(KickStarter.settingsManager.navMeshLayer); } if (navMesh.GetComponent <Collider2D>() == null) { ACDebug.LogWarning("A 2D Collider component must be attached to " + navMesh.gameObject.name + " for pathfinding to work - please attach one."); } }
public override void OnReset(NavigationMesh navMesh) { if (!Application.isPlaying) { return; } is2D = true; ResetHoles(navMesh); if (navMesh != null && navMesh.characterEvasion != CharacterEvasion.None && navMesh.GetComponent <PolygonCollider2D>() != null) { PolygonCollider2D[] polys = navMesh.PolygonCollider2Ds; if (polys != null && polys.Length > 1) { ACDebug.LogWarning("Character evasion cannot occur for multiple PolygonColliders - only the first on the active NavMesh will be affected."); } for (int i = 0; i < polys.Length; i++) { if (!polys[i].isTrigger) { ACDebug.LogWarning("The PolygonCollider2D on " + navMesh.gameObject.name + " is not a Trigger.", navMesh.gameObject); } if (polys[i].offset != Vector2.zero) { ACDebug.LogWarning("The PolygonCollider2D on " + navMesh.gameObject.name + " has a non-zero Offset - this can cause pathfinding errors. Clear this offset and adjust the GameObject's position if necessary.", navMesh.gameObject); } } } if (navMesh == null && KickStarter.settingsManager != null && KickStarter.settingsManager.movementMethod == MovementMethod.PointAndClick) { ACDebug.LogWarning("Could not initialise NavMesh - was one set as the Default in the Scene Manager?"); } }
protected void AddCharHoles(PolygonCollider2D[] navPolys, AC.Char charToExclude, NavigationMesh navigationMesh) { if (navigationMesh.characterEvasion == CharacterEvasion.None) { return; } ResetHoles(KickStarter.sceneSettings.navMesh, false); for (int p = 0; p < navPolys.Length; p++) { if (p > 0) { return; } if (navPolys[p].transform.lossyScale != Vector3.one) { ACDebug.LogWarning("Cannot create evasion Polygons inside NavMesh '" + navPolys[p].gameObject.name + "' because it has a non-unit scale."); continue; } Vector2 navPosition = navPolys[p].transform.position; foreach (AC.Char character in KickStarter.stateHandler.Characters) { CircleCollider2D circleCollider2D = character.GetComponent <CircleCollider2D>(); if (circleCollider2D != null && (character.charState == CharState.Idle || navigationMesh.characterEvasion == CharacterEvasion.AllCharacters) && (charToExclude == null || character != charToExclude) && Perform2DOverlapPoint(character.transform.position, NavigationEngine_PolygonCollider.results, 1 << KickStarter.sceneSettings.navMesh.gameObject.layer) != 0) { if (character.IsPlayer && KickStarter.settingsManager.movementMethod == MovementMethod.Direct) { // In this particular case, do not set Is Trigger } else { circleCollider2D.isTrigger = true; } List <Vector2> newPoints3D = new List <Vector2>(); Vector2 centrePoint = character.transform.TransformPoint(circleCollider2D.offset); float radius = circleCollider2D.radius * character.transform.localScale.x; float yScaler = navigationMesh.characterEvasionYScale; switch (navigationMesh.characterEvasionPoints) { case CharacterEvasionPoints.Four: newPoints3D.Add(centrePoint + new Vector2(dir_n.x * radius, dir_n.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_e.x * radius, dir_e.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_s.x * radius, dir_s.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_w.x * radius, dir_w.y * radius * yScaler)); break; case CharacterEvasionPoints.Eight: newPoints3D.Add(centrePoint + new Vector2(dir_n.x * radius, dir_n.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_ne.x * radius, dir_ne.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_e.x * radius, dir_e.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_se.x * radius, dir_se.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_s.x * radius, dir_s.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_sw.x * radius, dir_sw.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_w.x * radius, dir_w.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_nw.x * radius, dir_nw.y * radius * yScaler)); break; case CharacterEvasionPoints.Sixteen: newPoints3D.Add(centrePoint + new Vector2(dir_n.x * radius, dir_n.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_nne.x * radius, dir_nne.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_ne.x * radius, dir_ne.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_nee.x * radius, dir_nee.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_e.x * radius, dir_e.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_see.x * radius, dir_see.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_se.x * radius, dir_se.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_sse.x * radius, dir_sse.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_s.x * radius, dir_s.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_ssw.x * radius, dir_ssw.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_sw.x * radius, dir_sw.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_sww.x * radius, dir_sww.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_w.x * radius, dir_w.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_nww.x * radius, dir_nww.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_nw.x * radius, dir_nw.y * radius * yScaler)); newPoints3D.Add(centrePoint + new Vector2(dir_nnw.x * radius, dir_nnw.y * radius * yScaler)); break; } navPolys[p].pathCount++; List <Vector2> newPoints = new List <Vector2>(); for (int i = 0; i < newPoints3D.Count; i++) { // Only add a point if it is on the NavMesh if (Perform2DOverlapPoint(newPoints3D[i], NavigationEngine_PolygonCollider.results, 1 << KickStarter.sceneSettings.navMesh.gameObject.layer) != 0) { newPoints.Add(newPoints3D[i] - navPosition); } else { Vector2 altPoint = GetLineIntersect(newPoints3D[i], centrePoint); if (altPoint != Vector2.zero) { newPoints.Add(altPoint - navPosition); } } } if (newPoints.Count > 1) { navPolys[p].SetPath(navPolys[p].pathCount - 1, newPoints.ToArray()); } } } RebuildVertexArray(navPolys[p].transform, navPolys[p], p); } }
/** * <summary>Enables the NavMesh so that it can be used in pathfinding.</summary> * <param name = "navMeshOb">The NavigationMesh gameobject to enable</param> */ public virtual void TurnOn(NavigationMesh navMesh) { }
/** * Provides a space for any custom Editor GUI code that should be displayed in the NavigationMesh inspector. */ public virtual NavigationMesh NavigationMeshGUI(NavigationMesh _target) { _target.disableRenderer = EditorGUILayout.ToggleLeft ("Disable mesh renderer?", _target.disableRenderer); #if UNITY_5 _target.ignoreCollisions = EditorGUILayout.ToggleLeft ("Ignore collisions?", _target.ignoreCollisions); #endif return _target; }
public override void ResetHoles(NavigationMesh navMesh) { ResetHoles(navMesh, true); }
private void ResetHoles(NavigationMesh navMesh, bool rebuild) { if (navMesh == null || navMesh.GetComponent <PolygonCollider2D>() == null) return; PolygonCollider2D poly = navMesh.GetComponent <PolygonCollider2D>(); poly.pathCount = 1; if (navMesh.polygonColliderHoles.Count == 0) { if (rebuild) { RebuildVertexArray (navMesh.transform, poly); } return; } Vector2 scaleFac = new Vector2 (1f / navMesh.transform.localScale.x, 1f / navMesh.transform.localScale.y); foreach (PolygonCollider2D hole in navMesh.polygonColliderHoles) { if (hole != null) { poly.pathCount ++; List<Vector2> newPoints = new List<Vector2>(); foreach (Vector2 holePoint in hole.points) { Vector2 relativePosition = hole.transform.TransformPoint (holePoint) - navMesh.transform.position; newPoints.Add (new Vector2 (relativePosition.x * scaleFac.x, relativePosition.y * scaleFac.y)); } poly.SetPath (poly.pathCount-1, newPoints.ToArray ()); hole.gameObject.layer = LayerMask.NameToLayer (KickStarter.settingsManager.deactivatedLayer); hole.isTrigger = true; } } if (rebuild) { RebuildVertexArray (navMesh.transform, poly); } }
public override void ShowGUI(List<ActionParameter> parameters) { if (KickStarter.sceneSettings == null) { return; } sceneSetting = (SceneSetting) EditorGUILayout.EnumPopup ("Scene setting to change:", sceneSetting); if (sceneSetting == SceneSetting.DefaultNavMesh) { if (KickStarter.sceneSettings.navigationMethod == AC_NavigationMethod.meshCollider || KickStarter.sceneSettings.navigationMethod == AC_NavigationMethod.PolygonCollider) { if (KickStarter.sceneSettings.navigationMethod == AC_NavigationMethod.PolygonCollider) { changeNavMeshMethod = (ChangeNavMeshMethod) EditorGUILayout.EnumPopup ("Change NavMesh method:", changeNavMeshMethod); } if (KickStarter.sceneSettings.navigationMethod == AC_NavigationMethod.meshCollider || changeNavMeshMethod == ChangeNavMeshMethod.ChangeNavMesh) { parameterID = Action.ChooseParameterGUI ("New NavMesh:", parameters, parameterID, ParameterType.GameObject); if (parameterID >= 0) { constantID = 0; newNavMesh = null; } else { newNavMesh = (NavigationMesh) EditorGUILayout.ObjectField ("New NavMesh:", newNavMesh, typeof (NavigationMesh), true); constantID = FieldToID <NavigationMesh> (newNavMesh, constantID); newNavMesh = IDToField <NavigationMesh> (newNavMesh, constantID, false); } } else if (changeNavMeshMethod == ChangeNavMeshMethod.ChangeNumberOfHoles) { holeAction = (InvAction) EditorGUILayout.EnumPopup ("Add or remove hole:", holeAction); string _label = "Hole to add:"; if (holeAction == InvAction.Remove) { _label = "Hole to remove:"; } parameterID = Action.ChooseParameterGUI (_label, parameters, parameterID, ParameterType.GameObject); if (parameterID >= 0) { constantID = 0; hole = null; } else { hole = (PolygonCollider2D) EditorGUILayout.ObjectField (_label, hole, typeof (PolygonCollider2D), true); constantID = FieldToID <PolygonCollider2D> (hole, constantID); hole = IDToField <PolygonCollider2D> (hole, constantID, false); } if (holeAction == InvAction.Replace) { replaceParameterID = Action.ChooseParameterGUI ("Hole to remove:", parameters, replaceParameterID, ParameterType.GameObject); if (replaceParameterID >= 0) { replaceConstantID = 0; replaceHole = null; } else { replaceHole = (PolygonCollider2D) EditorGUILayout.ObjectField ("Hole to remove:", replaceHole, typeof (PolygonCollider2D), true); replaceConstantID = FieldToID <PolygonCollider2D> (replaceHole, replaceConstantID); replaceHole = IDToField <PolygonCollider2D> (replaceHole, replaceConstantID, false); } } } } else { EditorGUILayout.HelpBox ("This action is not compatible with the Unity Navigation pathfinding method, as set in the Scene Manager.", MessageType.Warning); } } else if (sceneSetting == SceneSetting.DefaultPlayerStart) { parameterID = Action.ChooseParameterGUI ("New default PlayerStart:", parameters, parameterID, ParameterType.GameObject); if (parameterID >= 0) { constantID = 0; playerStart = null; } else { playerStart = (PlayerStart) EditorGUILayout.ObjectField ("New default PlayerStart:", playerStart, typeof (PlayerStart), true); constantID = FieldToID <PlayerStart> (playerStart, constantID); playerStart = IDToField <PlayerStart> (playerStart, constantID, false); } } else if (sceneSetting == SceneSetting.SortingMap) { parameterID = Action.ChooseParameterGUI ("New SortingMap:", parameters, parameterID, ParameterType.GameObject); if (parameterID >= 0) { constantID = 0; sortingMap = null; } else { sortingMap = (SortingMap) EditorGUILayout.ObjectField ("New SortingMap:", sortingMap, typeof (SortingMap), true); constantID = FieldToID <SortingMap> (sortingMap, constantID); sortingMap = IDToField <SortingMap> (sortingMap, constantID, false); } } else if (sceneSetting == SceneSetting.TintMap) { parameterID = Action.ChooseParameterGUI ("New TintMap:", parameters, parameterID, ParameterType.GameObject); if (parameterID >= 0) { constantID = 0; tintMap = null; } else { tintMap = (TintMap) EditorGUILayout.ObjectField ("New TintMap:", tintMap, typeof (TintMap), true); constantID = FieldToID <TintMap> (tintMap, constantID); tintMap = IDToField <TintMap> (tintMap, constantID, false); } } else if (sceneSetting == SceneSetting.OnLoadCutscene) { parameterID = Action.ChooseParameterGUI ("New OnLoad cutscene:", parameters, parameterID, ParameterType.GameObject); if (parameterID >= 0) { constantID = 0; cutscene = null; } else { cutscene = (Cutscene) EditorGUILayout.ObjectField ("New OnLoad custscne:", cutscene, typeof (Cutscene), true); constantID = FieldToID <Cutscene> (cutscene, constantID); cutscene = IDToField <Cutscene> (cutscene, constantID, false); } } else if (sceneSetting == SceneSetting.OnStartCutscene) { parameterID = Action.ChooseParameterGUI ("New OnStart cutscene:", parameters, parameterID, ParameterType.GameObject); if (parameterID >= 0) { constantID = 0; cutscene = null; } else { cutscene = (Cutscene) EditorGUILayout.ObjectField ("New OnStart cutscene:", cutscene, typeof (Cutscene), true); constantID = FieldToID <Cutscene> (cutscene, constantID); cutscene = IDToField <Cutscene> (cutscene, constantID, false); } } AfterRunningOption (); }
public override void ResetHoles(NavigationMesh navMesh) { ResetHoles (navMesh, true); }
public override void OnReset(NavigationMesh navMesh) { ResetHoles (navMesh); }
public override NavigationMesh NavigationMeshGUI(NavigationMesh _target) { _target = base.NavigationMeshGUI (_target); _target.characterEvasion = (CharacterEvasion) EditorGUILayout.EnumPopup ("Character evasion:", _target.characterEvasion); if (_target.characterEvasion != CharacterEvasion.None) { EditorGUILayout.HelpBox ("Note: Characters can only be avoided if they have a Circle Collider 2D (no Trigger) component on their base.\n\n" + "For best results, set a non-zero 'Pathfinding update time' in the Settings Manager.", MessageType.Info); } int numOptions = _target.polygonColliderHoles.Count; numOptions = EditorGUILayout.IntField ("Number of holes:", _target.polygonColliderHoles.Count); if (numOptions < 0) { numOptions = 0; } if (numOptions < _target.polygonColliderHoles.Count) { _target.polygonColliderHoles.RemoveRange (numOptions, _target.polygonColliderHoles.Count - numOptions); } else if (numOptions > _target.polygonColliderHoles.Count) { if (numOptions > _target.polygonColliderHoles.Capacity) { _target.polygonColliderHoles.Capacity = numOptions; } for (int i=_target.polygonColliderHoles.Count; i<numOptions; i++) { _target.polygonColliderHoles.Add (null); } } for (int i=0; i<_target.polygonColliderHoles.Count; i++) { _target.polygonColliderHoles [i] = (PolygonCollider2D) EditorGUILayout.ObjectField ("Hole #" + i.ToString () + ":", _target.polygonColliderHoles [i], typeof (PolygonCollider2D), true); } return _target; }
protected void CalcSearchRadius(NavigationMesh navMesh) { searchRadius = 0.1f - (0.08f * navMesh.accuracy); }
/** * <summary>Called when the scene begins or is reset.</summary> * <param name = "navMesh">The NavigationMesh that is active in the scene.</param> */ public virtual void OnReset(NavigationMesh navMesh) { }
public override NavigationMesh NavigationMeshGUI(NavigationMesh _target) { _target = base.NavigationMeshGUI(_target); _target.characterEvasion = (CharacterEvasion)CustomGUILayout.EnumPopup("Character evasion:", _target.characterEvasion, "", "The condition for which dynamic 2D pathfinding can occur by generating holes around characters"); if (_target.characterEvasion != CharacterEvasion.None) { _target.characterEvasionPoints = (CharacterEvasionPoints)CustomGUILayout.EnumPopup("Evasion accuracy:", _target.characterEvasionPoints, "", "The number of vertices created around characters to evade"); _target.characterEvasionYScale = CustomGUILayout.Slider("Evasion y-scale:", _target.characterEvasionYScale, 0.1f, 1f, "", "The scale of generated character evasion 'holes' in the NavMesh in the y-axis, relative to the x-axis"); EditorGUILayout.HelpBox("Note: Characters can only be avoided if they have a Circle Collider 2D (no Trigger) component on their base.\n\n" + "For best results, set a non-zero 'Pathfinding update time' in the Settings Manager.", MessageType.Info); if (_target.transform.lossyScale != Vector3.one) { EditorGUILayout.HelpBox("For character evasion to work, the NavMesh must have a unit scale (1,1,1).", MessageType.Warning); } #if UNITY_ANDROID || UNITY_IOS EditorGUILayout.HelpBox("This is an expensive calculation - consider setting this to 'None' for mobile platforms.", MessageType.Warning); #endif } _target.accuracy = CustomGUILayout.Slider("Accuracy:", _target.accuracy, 0f, 1f, "", "A float that can be used as an accuracy parameter, should the algorithm require one"); _target.gizmoColour = CustomGUILayout.ColorField("Gizmo colour:", _target.gizmoColour, "", "The colour of its Gizmo when used for 2D polygons"); EditorGUILayout.Separator(); GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); EditorGUILayout.LabelField("NavMesh holes", EditorStyles.boldLabel); for (int i = 0; i < _target.polygonColliderHoles.Count; i++) { EditorGUILayout.BeginHorizontal(); _target.polygonColliderHoles [i] = (PolygonCollider2D)CustomGUILayout.ObjectField <PolygonCollider2D> ("Hole #" + i.ToString() + ":", _target.polygonColliderHoles [i], true, "", "A shape within the boundary of this PolygonCollider2D to create a hole from"); if (GUILayout.Button("-", GUILayout.MaxWidth(20f))) { _target.polygonColliderHoles.RemoveAt(i); i = -1; continue; } EditorGUILayout.EndHorizontal(); if (_target.polygonColliderHoles[i] != null && _target.polygonColliderHoles[i].GetComponent <NavMeshBase>()) { EditorGUILayout.HelpBox("A NavMesh cannot use its own Polygon Collider component as a hole!", MessageType.Warning); } } if (GUILayout.Button("Create new hole")) { _target.polygonColliderHoles.Add(null); } if (_target.GetComponent <PolygonCollider2D>() != null) { int numPolys = _target.GetComponents <PolygonCollider2D>().Length; if (numPolys > 1) { if (_target.polygonColliderHoles.Count > 0) { EditorGUILayout.HelpBox("Holes will only work if they are within the boundaries of " + _target.gameObject.name + "'s FIRST PolygonCollider component.", MessageType.Warning); } if (_target.characterEvasion != CharacterEvasion.None) { EditorGUILayout.HelpBox("Character-evasion will only work within the boundaries of " + _target.gameObject.name + "'s FIRST PolygonCollider component.", MessageType.Warning); } } } return(_target); }
public override void TurnOn(NavigationMesh navMesh) { ACDebug.LogWarning ("Cannot enable NavMesh " + navMesh.gameObject.name + " as this scene's Navigation Method is Unity Navigation."); }
/** * Integrates all PolygonCollider2D objects in the polygonColliderHoles List into the base PolygonCollider2D shape. * This is called automatically by AddHole() and RemoveHole() once the List has been amended */ public virtual void ResetHoles(NavigationMesh navMesh) { }
public override void TurnOn(NavigationMesh navMesh) { if (navMesh == null) return; if (LayerMask.NameToLayer (KickStarter.settingsManager.navMeshLayer) == -1) { ACDebug.LogError ("Can't find layer " + KickStarter.settingsManager.navMeshLayer + " - please define it in Unity's Tags Manager (Edit -> Project settings -> Tags and Layers)."); } else if (KickStarter.settingsManager.navMeshLayer != "") { navMesh.gameObject.layer = LayerMask.NameToLayer (KickStarter.settingsManager.navMeshLayer); } if (navMesh.GetComponent <Collider2D>() == null) { ACDebug.LogWarning ("A 2D Collider component must be attached to " + navMesh.gameObject.name + " for pathfinding to work - please attach one."); } }
private void ResetHoles(NavigationMesh navMesh, bool changesMade) { if (changesMade) { navMesh.ResetHoles (); } }
public override void ResetHoles(NavigationMesh navMesh) { if (navMesh == null || navMesh.GetComponent <MeshCollider>() == null || navMesh.GetComponent <MeshCollider>().sharedMesh == null) return; if (navMesh.GetComponent <MeshFilter>() && navMesh.GetComponent <MeshFilter>().sharedMesh) { navMesh.GetComponent <MeshCollider>().sharedMesh = navMesh.GetComponent <MeshFilter>().sharedMesh; ACDebug.LogWarning (navMesh.gameObject.name + " has no MeshCollider mesh - temporarily using MeshFilter mesh instead."); } else { ACDebug.LogWarning (navMesh.gameObject.name + " has no MeshCollider mesh."); } }