Skip to content

Instantly share code, notes, and snippets.

@Westerveld
Last active March 15, 2019 16: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 Westerveld/65d4ac0b76a5ed971fdba531b272bb27 to your computer and use it in GitHub Desktop.
Save Westerveld/65d4ac0b76a5ed971fdba531b272bb27 to your computer and use it in GitHub Desktop.
A couple of scripts to use in Unity to create procedural people and text as shown at https://thisisverypointless.itch.io/procpeeps
using UnityEngine;
public class PeopleGen : MonoBehaviour
{
[SerializeField]
Sprite[] eyes;
[SerializeField]
Sprite[] hair;
[SerializeField]
Sprite[] mouth;
[SerializeField]
Sprite[] hat;
[SerializeField]
[Range(0, 100)]
float chanceOfHat;
[SerializeField]
int amountToSpawn;
[SerializeField]
Person person;
[SerializeField]
TextGen textG;
public void GenPerson()
{
if (!person.gameObject.activeSelf)
person.gameObject.SetActive(true);
person.eyes.sprite = eyes[Random.Range(0, eyes.Length)];
person.eyes.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
person.hair.sprite = hair[Random.Range(0, hair.Length)];
person.mouth.sprite = mouth[Random.Range(0, mouth.Length)];
person.mouth.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
if(Random.Range(0,100) > chanceOfHat)
{
person.hat.sprite = hat[Random.Range(0, hat.Length)];
}
else
{
person.hat.sprite = null;
}
person.SetColor();
textG.SetText();
}
}
using UnityEngine;
public class Person : MonoBehaviour
{
public SpriteRenderer body, eyes, mouth, hair, hat;
public void SetColor()
{
body.color = new Color(Random.Range(0.15f, 0.85f), Random.Range(0.15f, 0.85f), Random.Range(0.15f, 0.85f));
hair.color = new Color(Random.Range(0.15f, 0.85f), Random.Range(0.15f, 0.85f), Random.Range(0.15f, 0.850f));
}
}
using UnityEngine;
using TMPro;
using System.Text.RegularExpressions;
public class TextGen : MonoBehaviour
{
[SerializeField]
TMP_Text charName, charInfo;
string greeting, sName, verb, noun, adjective;
string[] greetings, sNames, verbs, nouns, adjectives;
[SerializeField]
TextAsset greetFile, nameFile, verbFile, nounFile, adjectFile;
void Start()
{
greetings = StoreIntoArray(greetFile);
sNames = StoreIntoArray(nameFile);
verbs = StoreIntoArray(verbFile);
nouns = StoreIntoArray(nounFile);
adjectives = StoreIntoArray(adjectFile);
}
string[] StoreIntoArray(TextAsset file)
{
string text = file.text;
string[] arr = Regex.Split(text, "\n");
return arr;
}
public void SetText()
{
SetWords();
charName.text = greeting + "\n My name is " + sName;
charInfo.text = "I am " + adjective + "\nwhen I see "+ verb + "\n" + noun;
}
void SetWords()
{
greeting = greetings[Random.Range(0, greetings.Length)];
sName = sNames[Random.Range(0, sNames.Length)];
adjective = adjectives[Random.Range(0, adjectives.Length)];
verb = verbs[Random.Range(0, verbs.Length)];
noun = nouns[Random.Range(0, nouns.Length)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment