123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using UnityEngine;
- using System.Collections;
- namespace Pathfinding.Examples {
-
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_object_placer.php")]
- public class ObjectPlacer : MonoBehaviour {
-
-
-
-
- public GameObject go;
-
- public bool direct = false;
-
- public bool issueGUOs = true;
-
- void Update () {
- if (Input.GetKeyDown("p")) {
- PlaceObject();
- }
- if (Input.GetKeyDown("r")) {
- StartCoroutine(RemoveObject());
- }
- }
- public void PlaceObject () {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
-
- if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
- Vector3 p = hit.point;
- GameObject obj = GameObject.Instantiate(go, p, Quaternion.identity) as GameObject;
- if (issueGUOs) {
- Bounds b = obj.GetComponent<Collider>().bounds;
- GraphUpdateObject guo = new GraphUpdateObject(b);
- AstarPath.active.UpdateGraphs(guo);
- if (direct) {
- AstarPath.active.FlushGraphUpdates();
- }
- }
- }
- }
- public IEnumerator RemoveObject () {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
-
- if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
-
- if (hit.collider.isTrigger || hit.transform.gameObject.name == "Ground") yield break;
- Bounds b = hit.collider.bounds;
- Destroy(hit.collider);
- Destroy(hit.collider.gameObject);
- if (issueGUOs) {
-
-
-
- yield return new WaitForEndOfFrame();
- GraphUpdateObject guo = new GraphUpdateObject(b);
- AstarPath.active.UpdateGraphs(guo);
- if (direct) {
- AstarPath.active.FlushGraphUpdates();
- }
- }
- }
- }
- }
- }
|