summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/DoorController.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-23 10:08:29 +0800
committerchai <215380520@qq.com>2024-05-23 10:08:29 +0800
commit8722a9920c1f6119bf6e769cba270e63097f8e25 (patch)
tree2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/DoorController.cs
parent3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff)
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/DoorController.cs')
-rw-r--r--Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/DoorController.cs59
1 files changed, 59 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/DoorController.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/DoorController.cs
new file mode 100644
index 0000000..7056294
--- /dev/null
+++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/DoorController.cs
@@ -0,0 +1,59 @@
+using UnityEngine;
+
+namespace Pathfinding.Examples {
+ /// <summary>Example script used in the example scenes</summary>
+ [HelpURL("https://arongranberg.com/astar/documentation/stable/doorcontroller.html")]
+ public class DoorController : MonoBehaviour {
+ private bool open = false;
+
+ public PathfindingTag opentag = 1;
+ public PathfindingTag closedtag = 1;
+ public bool updateGraphsWithGUO = true;
+ public float yOffset = 5;
+
+ Bounds bounds;
+
+ public void Start () {
+ // Capture the bounds of the collider while it is closed
+ bounds = GetComponent<Collider>().bounds;
+
+ // Initially open the door
+ SetState(open);
+ }
+
+ void OnGUI () {
+ // Show a UI button for opening and closing the door
+ if (GUI.Button(new Rect(5, yOffset, 100, 22), "Toggle Door")) {
+ SetState(!open);
+ }
+ }
+
+ public void SetState (bool open) {
+ this.open = open;
+
+ if (updateGraphsWithGUO) {
+ // Update the graph below the door
+ // Set the tag of the nodes below the door
+ // To something indicating that the door is open or closed
+ GraphUpdateObject guo = new GraphUpdateObject(bounds);
+ var tag = open ? opentag : closedtag;
+
+ // There are only 32 tags
+ if (tag > 31) { Debug.LogError("tag > 31"); return; }
+
+ guo.modifyTag = true;
+ guo.setTag = tag;
+ guo.updatePhysics = false;
+
+ AstarPath.active.UpdateGraphs(guo);
+ }
+
+ // Play door animations
+ if (open) {
+ GetComponent<Animation>().Play("Open");
+ } else {
+ GetComponent<Animation>().Play("Close");
+ }
+ }
+ }
+}