Last active
April 20, 2025 00:27
-
-
Save gekidoslair/e3526fd97cca8050a286d8864829b00d to your computer and use it in GitHub Desktop.
Unity Helper script to speed up setting up the required gameobject hierarchy for the Gley Traffic System (v3 as of this post)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed under the MIT license | |
* | |
* MIT License | |
* Copyright (c) 2025 Mike Wuetherick | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
using Gley.TrafficSystem; | |
using Gley.UrbanSystem.Internal; | |
using UnityEngine; | |
namespace PixelWizards.Gameplay.TrafficSystem | |
{ | |
/// <summary> | |
/// Sdd into a scripts folder | |
/// Helper to speed setup of vehicles for the Gley Traffic System (v3) | |
/// </summary> | |
public class NewVehicleCreator : MonoBehaviour | |
{ | |
[SerializeField] | |
private GameObject mainVehiclePrefab; | |
public GameObject VehiclePrefab => mainVehiclePrefab; | |
public void SetupNewVehicle() | |
{ | |
if (gameObject.GetComponent<VehicleComponent>() != null) | |
{ | |
// vehicle is already setup, verify | |
Debug.Log("Vehicle Already setup? Validating..."); | |
} | |
// car holder | |
var ch = ValidateElement("CarHolder", transform); | |
// wheels | |
ValidateElement("CarHolder/Wheels", ch); | |
// body | |
var body = ValidateElement("CarHolder/Body", ch); | |
if (mainVehiclePrefab) | |
{ | |
// parent the vehicle under the body for now | |
mainVehiclePrefab.transform.parent = body.transform; | |
mainVehiclePrefab.transform.localPosition = Vector3.zero; | |
} | |
else | |
{ | |
Debug.Log("Missing vehicle prefab, parent under 'Body' object!"); | |
} | |
// lights | |
var lights = ValidateElement("CarHolder/Lights", ch); | |
ValidateElement("CarHolder/Lights/BlinkersLeft", lights); | |
ValidateElement("CarHolder/Lights/BlinkersRight", lights); | |
ValidateElement("CarHolder/Lights/FrontLights", lights); | |
ValidateElement("CarHolder/Lights/RearLights", lights); | |
ValidateElement("CarHolder/Lights/ReverseLights", lights); | |
ValidateElement("CarHolder/Lights/StopLights", lights); | |
// colliders | |
ValidateElement("CarHolder/Colliders", ch); | |
// front trigger | |
ValidateElement("CarHolder/FrontTriggerHolder", ch); | |
// setup the vehicle components | |
if (!gameObject.TryGetComponent(out Rigidbody rb)) | |
{ | |
Debug.Log("Missing rigidbody, adding..."); | |
gameObject.AddComponent<Rigidbody>(); | |
} | |
if (!gameObject.TryGetComponent(out VehicleComponent vh)) | |
{ | |
Debug.Log("Missing VehicleComponent, adding..."); | |
gameObject.AddComponent<VehicleComponent>(); | |
} | |
if (!gameObject.TryGetComponent(out EngineSoundComponent esc)) | |
{ | |
Debug.Log("Missing EngineSoundComponent, adding..."); | |
gameObject.AddComponent<EngineSoundComponent>(); | |
} | |
if (!gameObject.TryGetComponent(out VehicleLightsComponent vlc)) | |
{ | |
Debug.Log("Missing VehicleLightComponent, adding..."); | |
gameObject.AddComponent<VehicleLightsComponent>(); | |
} | |
Debug.Log("Vehicle configured properly!"); | |
} | |
public Transform ValidateElement(string name, Transform parent) | |
{ | |
var existing = transform.Find(name); | |
// does an object with this name exist? | |
if (existing) | |
{ | |
Debug.Log($"Element: {name} exists, checking parent..."); | |
// check if it has the correct parent | |
if (existing.parent == parent) | |
{ | |
return existing; | |
} | |
Debug.Log($"Element: {name}, invalid parent, updating to {parent.name}"); | |
existing.SetParent(parent); | |
existing.transform.localPosition = Vector3.zero; | |
} | |
else | |
{ | |
// extract the last name | |
var elements = name.Split('/'); | |
var properName = elements[^1]; | |
Debug.Log($"Missing: {properName}, creating with parent {parent.name}"); | |
var go = new GameObject(properName); | |
go.transform.SetParent(parent); | |
go.transform.localPosition = Vector3.zero; | |
existing = go.transform; | |
} | |
return existing; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed under the MIT license | |
* | |
* MIT License | |
* Copyright (c) 2025 Mike Wuetherick | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
using UnityEditor; | |
using UnityEngine; | |
namespace PixelWizards.Gameplay.TrafficSystem | |
{ | |
/// <summary> | |
/// Custom inspector for the NewVehicleCreator | |
/// Add into an 'Editor' folder (or asmdef) in your project | |
/// </summary> | |
[CustomEditor(typeof(NewVehicleCreator)), CanEditMultipleObjects] | |
public class NewVehicleCreatorEditor : Editor | |
{ | |
private static NewVehicleCreator entry; | |
private void OnEnable() | |
{ | |
entry = (NewVehicleCreator)target; | |
} | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
DrawDefaultInspector(); | |
GUILayout.Label("Optional: specify a prefab for the vehicle, will parent under the 'Body'", EditorStyles.helpBox); | |
GUILayout.Space(15); | |
GUILayout.BeginHorizontal(); | |
{ | |
if (GUILayout.Button("Setup Vehicle")) | |
{ | |
entry.SetupNewVehicle(); | |
} | |
} | |
GUILayout.EndHorizontal(); | |
// we might have deleted ourselves, so check before applying any changes | |
if (serializedObject != null) | |
{ | |
serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
} | |
} |
Added MIT license header
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fix validation for light elements