Skip to content

Instantly share code, notes, and snippets.

@d8ahazard
Created August 27, 2019 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d8ahazard/1021694b26f19ce0ea02c1d79f25c0d1 to your computer and use it in GitHub Desktop.
Save d8ahazard/1021694b26f19ce0ea02c1d79f25c0d1 to your computer and use it in GitHub Desktop.
A quickie script to take all items in a SECTR terrain chunk and add them as children of that chunk.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
[ExecuteInEditMode]
public class SectorCreator : MonoBehaviour
{
public bool drawCurrentChildren = false;
public bool drawPositionChildren = false;
public List<GameObject> children = new List<GameObject>();
public List<GameObject> orphans = new List<GameObject>();
private SECTR_Chunk myChunk;
private SECTR_Sector mySector;
private Bounds thisBounds;
void Start() {
thisBounds = gameObject.GetComponent<SECTR_Sector>().TotalBounds;
children = new List<GameObject>();
}
void Awake() {
thisBounds = gameObject.GetComponent<SECTR_Sector>().TotalBounds;
children = new List<GameObject>();
}
public void SelectChildren() {
myChunk = GetComponent<SECTR_Chunk>();
mySector = GetComponent<SECTR_Sector>();
List<GameObject> cList = new List<GameObject>();
if (gameObject.GetComponent<SECTR_Sector>() != null) {
foreach (GameObject go in FindObjectsOfType<GameObject>()) {
string m_Scene = SceneManager.GetActiveScene().name;
bool inScene = (go.scene.name == m_Scene);
bool exclude = (go.layer == 9 || go.layer == 10);
Renderer rend = go.GetComponent<Renderer>();
if (rend != null && !go.transform.IsChildOf(transform) && inScene && !exclude) {
if (InBounds(thisBounds, rend.bounds)) cList.Add(go);
}
}
}
if (cList.Count != children.Count) {
children = cList;
} else {
#if UNITY_EDITOR
// if (children.Count > 0) Selection.objects = children.ToArray();
#endif
}
}
public void ClearChildren() {
List<GameObject> clear = new List<GameObject>();
foreach (Transform cc in transform) {
Renderer rend = cc.GetComponent<Renderer>();
if (rend != null) {
if (!InBounds(thisBounds, rend.bounds)) {
clear.Add(cc.gameObject);
}
}
}
if (orphans.Count > 0) {
GameObject oc = GameObject.Find("Orphans");
foreach(GameObject orphan in orphans) {
orphan.transform.parent = oc.transform;
}
#if UNITY_EDITOR
//Selection.objects = orphans.ToArray();
#endif
orphans = new List<GameObject>();
} else {
orphans = clear;
}
}
#if UNITY_EDITOR
public void AddChildren() {
if (CheckSectorLoaded()) {
foreach (GameObject child in children) {
if (child.transform.parent != transform) {
GameObject parent = PrefabUtility.GetOutermostPrefabInstanceRoot(child);
if (parent != null) {
parent.transform.parent = transform;
} else {
child.transform.parent = transform;
}
}
}
}
}
public void DrawCurrentChildren() {
drawCurrentChildren = !drawCurrentChildren;
SceneView.RepaintAll();
}
public void DrawPositionChildren() {
drawPositionChildren = !drawPositionChildren;
SceneView.RepaintAll();
}
protected void OnDrawGizmos() {
Gizmos.color = Color.yellow;
//Gizmos.DrawWireCube(thisBounds.center, thisBounds.size);
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.green;
if (drawPositionChildren) {
foreach (GameObject kid in children) {
Bounds b = kid.GetComponent<Renderer>().bounds;
Gizmos.DrawWireCube(b.center, b.size);
}
}
Gizmos.color = Color.blue;
if (drawCurrentChildren) {
foreach(Transform t in transform) {
Renderer r = t.gameObject.GetComponent<Renderer>();
if (r != null) {
Bounds b = r.bounds;
Gizmos.DrawWireCube(b.center, b.size);
}
}
}
Gizmos.color = Color.red;
Gizmos.DrawWireCube(thisBounds.center, thisBounds.size);
}
private bool CheckSectorLoaded() {
EditorGUILayout.BeginHorizontal();
bool editMode = !EditorApplication.isPlaying && !EditorApplication.isPaused;
bool alreadyExported = myChunk && System.IO.File.Exists(UnityToOSPath(myChunk.NodeName));
if (!mySector.Frozen) {
Debug.Log("We can add this as a child.");
return true;
} else {
Debug.Log("We can't add this as a child.");
return false;
}
}
private static string UnityToOSPath(string path) {
return string.IsNullOrEmpty(path) ? null : Application.dataPath + path.Replace("Assets", "");
}
#endif
static bool InBounds(Bounds Encapsulator, Bounds Encapsulated) {
Vector3 boundPoint1 = Encapsulated.min;
Vector3 boundPoint2 = Encapsulated.max;
Vector3 boundPoint3 = new Vector3(boundPoint1.x, boundPoint1.y, boundPoint2.z);
Vector3 boundPoint4 = new Vector3(boundPoint1.x, boundPoint2.y, boundPoint1.z);
Vector3 boundPoint5 = new Vector3(boundPoint2.x, boundPoint1.y, boundPoint1.z);
Vector3 boundPoint6 = new Vector3(boundPoint1.x, boundPoint2.y, boundPoint2.z);
Vector3 boundPoint7 = new Vector3(boundPoint2.x, boundPoint1.y, boundPoint2.z);
Vector3 boundPoint8 = new Vector3(boundPoint2.x, boundPoint2.y, boundPoint1.z);
Vector3[] boundPoints = new Vector3[] { boundPoint1, boundPoint2, boundPoint3, boundPoint4, boundPoint5, boundPoint6, boundPoint7, boundPoint8 };
foreach (Vector3 bp in boundPoints) {
//if (!Encapsulator.Contains(bp)) return false;
}
//return true;
return Encapsulator.Contains(Encapsulated.min) && Encapsulator.Contains(Encapsulated.max);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment