-
-
Save PeteMichaud/14308106832489f1c029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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