Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. public class Node : MonoBehaviour
  7. {
  8.     public GameObject[] neighbors;
  9.     public float range = 5.0f;
  10.  
  11.     private void OnDrawGizmosSelected()
  12.     {
  13.         Gizmos.color = Color.red;
  14.         Gizmos.DrawWireSphere(gameObject.transform.position, range);
  15.     }
  16.  
  17.     public void RecalcNeighbors()
  18.     {
  19.         GameObject[] allNodes = GameObject.FindGameObjectsWithTag("node");
  20.         List<GameObject> result = new List<GameObject>();
  21.         foreach (GameObject o in allNodes)
  22.         {
  23.             if(Vector3.Distance(o.transform.position, gameObject.transform.position) < range)
  24.             {
  25.                 result.Add(o);
  26.             }
  27.         }
  28.         neighbors = result.ToArray();
  29.     }
  30. }
  31.  
  32. [CustomEditor(typeof(Node))]
  33. public class NodeInspector : Editor
  34. {
  35.     Node node;
  36.     private void OnEnable()
  37.     {
  38.         node = (Node)target;
  39.     }
  40.  
  41.     public override void OnInspectorGUI()
  42.     {
  43.         DrawDefaultInspector();
  44.  
  45.         if(GUILayout.Button("Recalc Neighbors"))
  46.         {
  47.             node.RecalcNeighbors();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement