Skip to content

Instantly share code, notes, and snippets.

@PeteMichaud
Last active August 29, 2015 14:27
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 PeteMichaud/14308106832489f1c029 to your computer and use it in GitHub Desktop.
Save PeteMichaud/14308106832489f1c029 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using AgencyScripts.Library;
public class Avatar {
public const int AVATAR_SIZE = 512;
private Sprite _sprite = null;
public Sprite Sprite
{
get {
if(_sprite == null)
{
_sprite = Sprite.Create(
Texture,
new Rect(0, 0, AVATAR_SIZE, AVATAR_SIZE),
new Vector2(0.5f,0.5f), //pivot
100f);
}
return _sprite;
}
}
private Texture2D _texture;
public Texture2D Texture
{
get {
if(_texture == null)
{
_texture = GenerateTexture();
}
return _texture;
}
}
private Color32 _blankColor = new Color32();
private Color32 _mainColor;
public Color32 MainColor
{
get {
if(_mainColor.SameAs(_blankColor))
_mainColor = RandomColor.Get ();
return _mainColor;
}
}
private Color32 _backgroundColor;
public Color32 BackgroundColor
{
get{
if(_backgroundColor.SameAs(_blankColor))
_backgroundColor = MainColor.GetComplement();
return _backgroundColor;
}
}
public Avatar()
{
_mainColor = _blankColor;
_backgroundColor = _blankColor;
}
private Texture2D GenerateTexture()
{
Texture2D t = new Texture2D(AVATAR_SIZE, AVATAR_SIZE);
Color32[] fillColorArray = t.GetPixels32();
for(var i = 0; i < fillColorArray.Length; ++i)
{
fillColorArray[i] = BackgroundColor;
}
t.SetPixels32( fillColorArray );
t.Apply(false);
return t;
}
}
...
RawImage image = prefab.transform.Find("imgAgentPicture").GetComponent<RawImage>();
image.texture = agent.Avatar.Texture;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment