Skip to content

Instantly share code, notes, and snippets.

@Westerveld Westerveld/PeopleGen.cs
Last active Mar 15, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.