Skip to content

Instantly share code, notes, and snippets.

@jxnblk
Created October 16, 2023 15:31
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 jxnblk/3e21f2494863b4e13a79c783bb05f248 to your computer and use it in GitHub Desktop.
Save jxnblk/3e21f2494863b4e13a79c783bb05f248 to your computer and use it in GitHub Desktop.
Novantica modular characters scripts for Unity
using System.Collections.Generic;
using UnityEngine;
public class ModularCharacter : MonoBehaviour {
[System.Serializable]
public class Section {
public string name;
public List<GameObject> parts = new List<GameObject>();
public int index = 0;
}
public List<Section> sections = new List<Section>();
public ModularColors colors;
public float minScale = 0.9f;
public float maxScale = 1.1f;
public bool autorando = false;
Renderer[] meshes;
void Start () {
meshes = GetComponentsInChildren<Renderer>();
Render();
}
void OnEnable () {
if (autorando) Randomize();
}
void Render () {
foreach (Section section in sections) {
RenderSection(section);
}
}
void RenderSection (Section section) {
if (section.parts.Count < 1) return;
foreach (GameObject go in section.parts) {
go.SetActive(false);
}
GameObject part = section.parts[section.index];
part.SetActive(true);
}
[ContextMenu("Randomize")]
public void Randomize () {
foreach (Section section in sections) {
RandomizeSection(section);
}
colors.Randomize();
float s = Random.Range(minScale, maxScale);
transform.localScale = new Vector3(s, s, s);
}
public void RandomizeSection (Section section) {
int n = Random.Range(0, section.parts.Count);
section.index = n;
RenderSection(section);
}
}
using System.Collections.Generic;
using UnityEngine;
public class ModularColors : MonoBehaviour {
public List<Texture> textures = new List<Texture>();
public int index = 0;
public bool autorando = false;
Renderer[] meshes = new Renderer[0];
void OnEnable () {
if (autorando) {
Randomize();
} else {
Render();
}
}
[ContextMenu("Render")]
public void Render () {
if (textures.Count < 1) {
Debug.LogWarning($"[ModularColors] {name}: missing textures");
return;
}
meshes = GetComponentsInChildren<Renderer>();
Texture tex = textures[index];
foreach (Renderer mesh in meshes) {
mesh.material.mainTexture = tex;
}
}
[ContextMenu("Randomize")]
public void Randomize () {
index = Random.Range(0, textures.Count);
Render();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment