Skip to content

Instantly share code, notes, and snippets.

@gekidoslair
Last active April 20, 2025 00:27
Show Gist options
  • Save gekidoslair/fd678aef6276df99578fe7c96601688f to your computer and use it in GitHub Desktop.
Save gekidoslair/fd678aef6276df99578fe7c96601688f to your computer and use it in GitHub Desktop.
Gley Traffic System vehicle material randomizer, add this to your Vehicles to randomize materials when they are activated
/*
* 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 System.Collections.Generic;
using Gley.TrafficSystem;
using UnityEngine;
namespace PixelWizards.Gameplay.TrafficSystem
{
/// <summary>
/// Listens to when a vehicle is activated and if it is
/// this vehicle then randomizes the material for
/// the specified mesh renderer.
/// Add this to the same game object as the VehicleComponent
/// </summary>
public class VehicleColorRandomizer : MonoBehaviour
{
[Header("Which mesh are we applying the random material to?")]
[SerializeField] private MeshRenderer colorMeshRenderer;
[Header("List of materials to randomize")]
[SerializeField]
private List<Material> materials = new();
private VehicleComponent thisVehicle;
private void Awake()
{
Events.OnVehicleActivated += VehicleActivatedHandler;
thisVehicle = gameObject.GetComponent<VehicleComponent>();
}
private void VehicleActivatedHandler(int vehicleIndex, int waypointIndex)
{
var vehicle = API.GetVehicleComponent(vehicleIndex);
if (thisVehicle != vehicle)
{
return;
}
RandomizeMaterial();
}
[ContextMenu("Randomize Material")]
private void RandomizeMaterial()
{
// randomize materials
var randomMat = materials[Random.Range(0, materials.Count)];
if (colorMeshRenderer)
{
colorMeshRenderer.sharedMaterial = randomMat;
}
}
}
}
@gekidoslair
Copy link
Author

Updated, this is under MIT license

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment