oddgames / UIToolkit

Single draw call UI solution for Unity with multi resolution support and more.
517 stars 153 forks source link

how to use UIButton for downloading images like fb friends picture and will display accordingly in the UIButton #132

Closed jovel6 closed 11 years ago

jovel6 commented 11 years ago

using LitJson; using UnityEngine; using System.Collections; using System.Text; using Prime31; using System.Collections.Generic;

public class LoadJSON_FBFriends : MonoBehaviour {

private bool _movedContainer;
public Material loadImageMaterial;

private UIToolkit myToolkit;
UIButton touchable,b1;

private string userId;
private Dictionary<string, string> friends;
ArrayList friendsList;
public GUIStyle friendsFontStyle;

void Start()
{
    StartCoroutine(getImage());

    var scrollable = new UIScrollableVerticalLayout( 0 );
    scrollable.alignMode = UIAbstractContainer.UIContainerAlignMode.Center;
    scrollable.position = new Vector3( 0, 3, 0 );
    var width = UI.scaleFactor * 800;
    scrollable.setSize( width, Screen.height / 0.10f );

    for( var i = 0; i < 13; i++ )
    {

        if( i == 13 ) 
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );

        }
        else if( i % 12 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0);
        }
        else if( i % 11 == 0 )
        {
            touchable = UIButton.create( "FriendsAvatar.png", "FriendsAvatar.png", 0, 0);
        }
        else if( i % 10 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 9 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 8 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 7 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 6 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 5 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 4 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 3 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else if( i % 2 == 0 )
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0 );
        }
        else 
        {
            touchable = UIButton.create( "FriendsListBox.png", "FriendsListBox.png", 0, 0);
        }

        loadImage(touchable, i);

        scrollable.addChild(touchable);

    }

}

IEnumerator getImage()
{

    string url = "https://graph.facebook.com/xxxxxxxx/friends?limit=5&fields=first_name,picture&access_token=xxxxxxxx";

    WWW www = new WWW(url);
    yield return www;

    ProcessFriendsDisplay(www.text);

}

public void ProcessFriendsDisplay(string jsonString)
{
    JsonData jsonFriends = JsonMapper.ToObject(jsonString);
    ConverterScript fbfriends = new ConverterScript();

    friendsList = new ArrayList();

    for(int i = 0; i<jsonFriends["data"].Count; i++)
    {   

        fbfriends.picture = jsonFriends["data"][i]["picture"]["data"]["url"].ToString();
        Debug.Log (fbfriends.picture + i);

        loadImage(touchable, i);

    }

}

void loadImage(UIButton uib, int i) 
{

    Texture2D tex = new Texture2D(50, 50);

    string UIButton = "touchable" + i;
    GameObject obj = GameObject.Find ("UIButton");
    obj.SendMessage("LoadBook", uib);

    uib.manager.renderer.material.mainTextureScale = new Vector2(18.0f,20.4f);
    uib.manager.renderer.material.mainTexture = tex;

}