Skip to content

Instantly share code, notes, and snippets.

@gekidoslair
Last active September 8, 2017 05:57
Show Gist options
  • Save gekidoslair/e2c34470b3bab69f01794893d81be63d to your computer and use it in GitHub Desktop.
Save gekidoslair/e2c34470b3bab69f01794893d81be63d to your computer and use it in GitHub Desktop.
Simple Customization Script, has a data model that could be used to save / store the customizations (for a save game or persistent state) as well as a simple randomization system for picking random elements at startup. The 'mandatory' flag is there because it does a pre-check to see if an element from this particular category should even be used…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PixelWizards.GameSystem.Controllers
{
public class CharacterCustomization
{
public int curBackpack = 0;
public int curBag = 0;
public int curBodykit = 0;
public int curGlasses = 0;
public int curHair = 0;
public int curHeaddress = 0;
public int curHelmet = 0;
public int curMask = 0;
public int curPants = 0;
public int curShirt = 0;
public int curShoes = 0;
public int curVest = 0;
}
[ExecuteInEditMode]
public class Customizer : MonoBehaviour
{
public bool doRandomCustomization = false;
public CharacterCustomization dataModel = new CharacterCustomization();
public bool mandatoryBackpack = false;
public List<GameObject> backpacks = new List<GameObject>();
public bool mandatoryBag = false;
public List<GameObject> bags = new List<GameObject>();
public bool mandatoryBodykit = false;
public List<GameObject> bodykit = new List<GameObject>();
public bool mandatoryGlasses = false;
public List<GameObject> glasses = new List<GameObject>();
public bool mandatoryHair = false;
public List<GameObject> hairstyles = new List<GameObject>();
public bool mandatoryHeaddress = false;
public List<GameObject> headdress = new List<GameObject>();
public bool mandatoryHelmet = false;
public List<GameObject> helmets = new List<GameObject>();
public bool mandatoryMasks = false;
public List<GameObject> masks = new List<GameObject>();
public bool mandatoryPants = false;
public List<GameObject> pants = new List<GameObject>();
public bool mandatoryShirt = false;
public List<GameObject> shirts = new List<GameObject>();
public bool mandatoryShoes = false;
public List<GameObject> shoes = new List<GameObject>();
public bool mandatoryVest = false;
public List<GameObject> vests = new List<GameObject>();
// Use this for initialization
private void Start()
{
if (doRandomCustomization)
DoRandomizer();
}
public void DoRandomizer()
{
dataModel.curBackpack = RandomizeGroup(backpacks, mandatoryBackpack);
dataModel.curBag = RandomizeGroup(bags, mandatoryBag);
dataModel.curBodykit = RandomizeGroup(bodykit, mandatoryBodykit);
dataModel.curGlasses = RandomizeGroup(glasses, mandatoryGlasses);
dataModel.curHair = RandomizeGroup(hairstyles, mandatoryHair);
dataModel.curHeaddress = RandomizeGroup(headdress, mandatoryHeaddress);
dataModel.curHelmet = RandomizeGroup(helmets, mandatoryHelmet);
dataModel.curMask = RandomizeGroup(masks, mandatoryMasks);
dataModel.curPants = RandomizeGroup(pants, mandatoryPants);
dataModel.curShirt = RandomizeGroup(shirts, mandatoryShirt);
dataModel.curShoes = RandomizeGroup(shoes, mandatoryShoes);
dataModel.curVest = RandomizeGroup(vests, mandatoryVest);
}
private int RandomizeGroup(List<GameObject> thisGroup, bool isMandatory)
{
int current = -1;
if (thisGroup.Count > 0)
{
DisableAllInGroup(thisGroup);
if ( isMandatory)
{
current = DoRandomSingle(thisGroup);
}
else
{
var useGroup = (UnityEngine.Random.Range(0, 100) > 50) ? true : false;
if (useGroup)
{
current = DoRandomSingle(thisGroup);
}
else
{
current = -1;
}
}
}
return current;
}
private int DoRandomSingle( List<GameObject> thisGroup)
{
var current = UnityEngine.Random.Range(0, thisGroup.Count - 1);
thisGroup[current].SetActive(true);
return current;
}
private void DisableAllInGroup( List<GameObject> thisGroup)
{
foreach (var thisEntry in thisGroup)
{
thisEntry.SetActive(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment