Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Need help with really simple editor script

Discussion in 'Scripting' started by Not_Sure, May 17, 2023.

  1. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    I don't know a thing about making editor scripts and really should learn, but I really need a very basic script and have no idea what to do.

    All I need is a script that will turn off object when the scene editor camera moves away a certain distance.

    I'm dying here swapping around map chunks and just want to have a script automatically do it.

    What would it look like?

    Where would I start?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Unity Learn has some good guides on editor tools
     
  3. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Yeah, and I'm looking through it right now. Still not sure what to do.
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Do your map chunks have colliders? If so, you could do OverlapSphere

    Code (csharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class AutoCuller : MonoBehaviour {
    6.  
    7.   [SerializeField] Transform _chunksParent;
    8.   [SerializeField] [Min(0f)] float _range;
    9.   [SerializeField] LayerMask _layerMask;
    10.  
    11. #if UNITY_EDITOR
    12.   private const float THRESHOLD = 1E-2f;
    13.   private const int MAX_COLLIDERS = 16;
    14.  
    15.   Vector3 _lastPos;
    16.   Colliders _colliders;
    17.   HashSet<GameObject> _hset;
    18.  
    19.   void Start() => process(forced: true);
    20.   void OnValidate() => process(forced: true);
    21.   void Update() => process();
    22.  
    23.   void process(bool forced = false) {
    24.     if(!forced && !hasMoved()) return;
    25.  
    26.     _colliders??= new Colliders[MAX_COLLIDERS];
    27.  
    28.     var count = Physics.OverlapSphereNonAlloc(
    29.       position: transform.position,
    30.       radius: _range,
    31.       results: _colliders,
    32.       layerMask: _layerMask,
    33.       queryTriggerInteraction: QueryTriggerInteraction.Ignore
    34.     );
    35.  
    36.     updateChildrenIn(_chunksParent, _colliders, count);
    37.   }
    38.  
    39.   bool hasMoved() {
    40.     var delta = transform.position - _lastPos;
    41.     _lastPos = transform.position;
    42.     return changed(delta.x) || changed(delta.y) || changed(delta.z);
    43.     static bool changed(float n) => Mathf.Abs(n) >= THRESHOLD;
    44.   }
    45.  
    46.   void updateChildrenIn(Transform parent, Collider[] colliders, int count) {
    47.     if(count == 0) return;
    48.  
    49.     if(_hset is null) _hset = new HashSet<GameObject>();
    50.       else _hset.Clear();
    51.  
    52.     for(int i = 0; i < count; i++)
    53.       _hset.Add(colliders[i].gameObject);
    54.  
    55.     foreach(var xf in parent) {
    56.       var go = xf.gameObject;
    57.       go.SetActive(_hset.Contains(go));
    58.     }
    59.   }
    60.  
    61.   void OnDrawGizmosSelected() {
    62.     Gizmos.color = Color.yellow;
    63.     Gizmos.DrawWireSphere(transform.position, _range);
    64.   }
    65. #endif
    66.  
    67. }
    Something like that.
     
    Last edited: May 17, 2023