Facebook SDK集成 - LoadPicture方法错误

时间:2014-12-09 22:02:31

标签: function methods sdk

所以你好@ll这是关于stackoverflow的第一篇帖子/问题:D

我需要帮助@:

所以我在我的游戏中成功整合了Facebook SDk,但我不会让个人资料图片工作...... 所以我在Facebook“Tutotial”Way上尝试了oldshool,并按照所有步骤实现登录功能等等...... 我已经下载了“friendsmash_start”示例并实现了Stuff ......

我的主要问题是我没有解决这个问题,也无法弄清楚我做错了什么,所以我希望得到帮助。

以下是来自MainMenu脚本的完整代码,这是我唯一改变的样本...就像在教程中...

Unity向我显示此错误 - > “当前上下文中不存在名称'LoadPicture'......因此错误出现在这两部分代码中:

“LoadPicture(Util.GetPictureURL(”me“,128,128),MyPictureCallback);”在“OnLoggedIn”函数

“LoadPicture(Util.GetPictureURL(”me“,128,128),MyPictureCallback);”在“MyPictureCallback”函数中

我不明白这是因为我在脚本中搜索了这个函数“LoadPicture”并且没有找到任何内容......根据facebook的“教程”:

https://developers.facebook.com/docs/games/unity/unity-tutorial?locale=de_DE

函数应该在那里,因为他们在那里写道:

“在MainMenu.cs中查看LoadPicture方法,了解我们如何使用Unity WWW类加载图API返回的图像。”

但是我找不到它:(

希望有人帮助我,我不能......

遗憾的是,这个问题很长时间......这很有意思。 :(

这是MAINMENU.CS的完整代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Facebook.MiniJSON;
using System;





public class MainMenu : MonoBehaviour
{
//   Inspector tunable members   //

public Texture ButtonTexture;
public Texture PlayTexture;                 //  Texture for main menu button icons
public Texture BragTexture;
public Texture ChallengeTexture;
public Texture StoreTexture;
public Texture FullScreenTexture;
public Texture FullScreenActiveTexture;

public Texture ResourcesTexture;

public Vector2 CanvasSize;                  // size of window on canvas

public Rect LoginButtonRect;                // Position of login button

public Vector2 ResourcePos;                 // position of resource indicators (not used yet)

public Vector2 ButtonStartPos;              // position of first button in main menu
public float ButtonScale;                   // size of main menu buttons
public float ButtonYGap;                    // gap between buttons in main menu
public float ChallengeDisplayTime;          // Number of seconds the request sent message is displayed for
public Vector2 ButtonLogoOffset;            // Offset determining positioning of logo on buttons
public float TournamentStep;                // Spacing between tournament entries
public float MouseScrollStep = 40;          // Amount score table moves with each step of the mouse wheel

public PaymentDialog paymentDialog;

public GUISkin MenuSkin;           

public int CoinBalance;
public int NumLives;
public int NumBombs;

public Texture[] CelebTextures;
public string [] CelebNames;



//   Private members   //


private static MainMenu instance;

private static List<object>                 friends         = null;
private static Dictionary<string, string>   profile         = null;
private static List<object>                 scores          = null;
private static Dictionary<string, Texture>  friendImages    = new Dictionary<string, Texture>();



private Vector2 scrollPosition = Vector2.zero;



private bool    haveUserPicture       = false;
private float   tournamentLength      = 0;
private int     tournamentWidth       = 512;

private int     mainMenuLevel         = 0; // Level index of main menu

private string popupMessage;
private float popupTime;
private float popupDuration;


void Awake()
{
    Util.Log("Awake");

    paymentDialog = ((PaymentDialog)(GetComponent("PaymentDialog")));

    // allow only one instance of the Main Menu
    if (instance != null && instance != this)
    {
        Destroy(gameObject);
        return;
    }

    #if UNITY_WEBPLAYER
    // Execute javascript in iframe to keep the player centred
    string javaScript = @"
        window.onresize = function() {
          var unity = UnityObject2.instances[0].getUnity();
          var unityDiv = document.getElementById(""unityPlayerEmbed"");

          var width =  window.innerWidth;
          var height = window.innerHeight;

          var appWidth = " + CanvasSize.x + @";
          var appHeight = " + CanvasSize.y + @";

          unity.style.width = appWidth + ""px"";
          unity.style.height = appHeight + ""px"";

          unityDiv.style.marginLeft = (width - appWidth)/2 + ""px"";
          unityDiv.style.marginTop = (height - appHeight)/2 + ""px"";
          unityDiv.style.marginRight = (width - appWidth)/2 + ""px"";
          unityDiv.style.marginBottom = (height - appHeight)/2 + ""px"";
        }

        window.onresize(); // force it to resize now";
    Application.ExternalCall(javaScript);
    #endif
    DontDestroyOnLoad(gameObject);
    instance = this;

    // Initialize FB SDK     
    FB.Init(SetInit, OnHideUnity); 
}

private void SetInit()
{
    Util.Log("SetInit");
    enabled = true; // "enabled" is a property inherited from MonoBehaviour                  
    if (FB.IsLoggedIn)
    {
        Util.Log("Already logged in");
        OnLoggedIn();
    }
}

private void OnHideUnity(bool isGameShown)
{
    Util.Log("OnHideUnity");
    if (!isGameShown)
    {
        // pause the game - we will need to hide                                             
        Time.timeScale = 0;
    }
    else
    {
        // start the game back up - we're getting focus again                                
        Time.timeScale = 1;
    }
}  

private void QueryScores()
{
    FB.API("/app/scores?fields=score,user.limit(20)", Facebook.HttpMethod.GET, ScoresCallback);
}


private int getScoreFromEntry(object obj)
{
    Dictionary<string,object> entry = (Dictionary<string,object>) obj;
    return Convert.ToInt32(entry["score"]);
}

void ScoresCallback(FBResult result) 
{
    Util.Log("ScoresCallback");
    if (result.Error != null)
    {
        Util.LogError(result.Error);
        return;
    }

    scores = new List<object>();
    List<object> scoresList = Util.DeserializeScores(result.Text);

    foreach(object score in scoresList) 
    {
        var entry = (Dictionary<string,object>) score;
        var user = (Dictionary<string,object>) entry["user"];

        string userId = (string)user["id"];

        if (string.Equals(userId,FB.UserId))
        {
            // This entry is the current player
            int playerHighScore = getScoreFromEntry(entry);
            Util.Log("Local players score on server is " + playerHighScore);
            if (playerHighScore < GameStateManager.Score)
            {
                Util.Log("Locally overriding with just acquired score: " + GameStateManager.Score);
                playerHighScore = GameStateManager.Score;
            }

            entry["score"] = playerHighScore.ToString();
            GameStateManager.HighScore = playerHighScore;
        }

        scores.Add(entry);
        if (!friendImages.ContainsKey(userId))
        {
            // We don't have this players image yet, request it now
            LoadPictureAPI(Util.GetPictureURL(userId, 128, 128),pictureTexture =>
            {
                if (pictureTexture != null)
                {
                    friendImages.Add(userId, pictureTexture);
                }
            });
        }
    }

    // Now sort the entries based on score
    scores.Sort(delegate(object firstObj,
                         object secondObj)
            {
                return -getScoreFromEntry(firstObj).CompareTo(getScoreFromEntry(secondObj));
            }
        );
}



void OnApplicationFocus( bool hasFocus ) 
{
  Util.Log ("hasFocus " + (hasFocus ? "Y" : "N"));
}

// Convenience function to check if mouse/touch is the tournament area
private bool IsInTournamentArea (Vector2 p)
{
    return p.x > Screen.width-tournamentWidth;
}


// Scroll the tournament view by some delta
private void ScrollTournament(float delta)
{
    scrollPosition.y += delta;
    if (scrollPosition.y > tournamentLength - Screen.height)
        scrollPosition.y = tournamentLength - Screen.height;
    if (scrollPosition.y < 0)
        scrollPosition.y = 0;
}


// variables for keeping track of scrolling
private Vector2 mouseLastPos;
private bool mouseDragging = false;


void Update()
{
    if(Input.touches.Length > 0) 
    {
        Touch touch = Input.touches[0];
        if (IsInTournamentArea (touch.position) && touch.phase == TouchPhase.Moved)
        {
            // dragging
            ScrollTournament (touch.deltaPosition.y*3);
        }
    }

    if (Input.GetAxis("Mouse ScrollWheel") < 0)
    {
        ScrollTournament (MouseScrollStep);
    }
    else if (Input.GetAxis("Mouse ScrollWheel") > 0)
    {
        ScrollTournament (-MouseScrollStep);
    }

    if (Input.GetMouseButton(0) && IsInTournamentArea(Input.mousePosition))
    {
        if (mouseDragging)
        {
            ScrollTournament (Input.mousePosition.y - mouseLastPos.y);
        }
        mouseLastPos = Input.mousePosition;
        mouseDragging = true;
    }
    else
        mouseDragging = false;
}

//  Button drawing logic //

private Vector2 buttonPos;  // Keeps track of where we've got to on the screen as we draw buttons

private void BeginButtons()
{
    // start drawing buttons at the chosen start position
    buttonPos = ButtonStartPos;
}

private bool DrawButton(string text, Texture texture)
{
    // draw a single button and update our position
    bool result = GUI.Button(new Rect (buttonPos.x,buttonPos.y, ButtonTexture.width * ButtonScale, ButtonTexture.height * ButtonScale),text,MenuSkin.GetStyle("menu_button"));
    Util.DrawActualSizeTexture(ButtonLogoOffset*ButtonScale+buttonPos,texture,ButtonScale);
    buttonPos.y += ButtonTexture.height*ButtonScale + ButtonYGap;

    if (paymentDialog.DialogEnabled)
        result = false;

    return result;
}



void OnGUI()
{
    GUI.skin = MenuSkin;
    if (Application.loadedLevel != mainMenuLevel) return;  // don't display anything except when in main menu


    GUILayout.Box("", MenuSkin.GetStyle("panel_welcome"));

    if (!FB.IsLoggedIn)
    {
        GUI.Label((new Rect(179, 11, 287, 160)), "Login to Facebook", MenuSkin.GetStyle("text_only"));
        if (GUI.Button(LoginButtonRect, "", MenuSkin.GetStyle("button_login")))
        {
            FB.Login("email,publish_actions", LoginCallback);
        }
    }    

    if (FB.IsLoggedIn)
    {
        string panelText = "Welcome ";


        panelText += (!string.IsNullOrEmpty(GameStateManager.Username)) ? string.Format("{0}!", GameStateManager.Username) : "Smasher!";

        if (GameStateManager.UserTexture != null) 
            GUI.DrawTexture( (new Rect(8,10, 150, 150)), GameStateManager.UserTexture);


        GUI.Label( (new Rect(179 , 11, 287, 160)), panelText, MenuSkin.GetStyle("text_only"));
    }



    string subTitle = "Let's smash some friends!";
    if (GameStateManager.Score > 0) 
    {
        subTitle = "Score: " + GameStateManager.Score.ToString();
    }
    if (!string.IsNullOrEmpty(subTitle))
    {
        GUI.Label( (new Rect(132, 28, 400, 160)), subTitle, MenuSkin.GetStyle("sub_title"));
    }



    BeginButtons();

    if (DrawButton("Play",PlayTexture))
    {
        onPlayClicked();
    }




    if (FB.IsLoggedIn)
    {
        // Draw resources bar
        Util.DrawActualSizeTexture(ResourcePos,ResourcesTexture);

        Util.DrawSimpleText(ResourcePos + new Vector2(47,5)  ,MenuSkin.GetStyle("resources_text"),string.Format("{0}",CoinBalance));
        Util.DrawSimpleText(ResourcePos + new Vector2(137,5) ,MenuSkin.GetStyle("resources_text"),string.Format("{0}",NumBombs));
        Util.DrawSimpleText(ResourcePos + new Vector2(227,5) ,MenuSkin.GetStyle("resources_text"),string.Format("{0}",NumLives));
    }



    #if UNITY_WEBPLAYER
    if (Screen.fullScreen)
    {
        if (DrawButton("Full Screen",FullScreenActiveTexture))
            SetFullscreenMode(false);
    }
    else 
    {
        if (DrawButton("Full Screen",FullScreenTexture))
            SetFullscreenMode(true);
    }
    #endif



    DrawPopupMessage();


}


public void AddPopupMessage(string message, float duration)
{
    popupMessage = message;
    popupTime = Time.realtimeSinceStartup;
    popupDuration = duration;
}
public void DrawPopupMessage()
{
    if (popupTime != 0 && popupTime + popupDuration > Time.realtimeSinceStartup)
    {
        // Show message that we sent a request
        Rect PopupRect = new Rect();
        PopupRect.width = 800;
        PopupRect.height = 100;
        PopupRect.x = Screen.width / 2 - PopupRect.width / 2;
        PopupRect.y = Screen.height / 2 - PopupRect.height / 2;
        GUI.Box(PopupRect,"",MenuSkin.GetStyle("box"));
        GUI.Label(PopupRect, popupMessage, MenuSkin.GetStyle("centred_text"));        
    }

}

void TournamentGui() 
{
    GUILayout.BeginArea(new Rect((Screen.width - 450),0,450,Screen.height));

    // Title box
    GUI.Box   (new Rect(0,    - scrollPosition.y, 100,200), "",           MenuSkin.GetStyle("tournament_bar"));
    GUI.Label (new Rect(121 , - scrollPosition.y, 100,200), "Tournament", MenuSkin.GetStyle("heading"));

    Rect boxRect = new Rect();

    if(scores != null)
    {
        var x = 0;
        foreach(object scoreEntry in scores) 
        {
            Dictionary<string,object> entry = (Dictionary<string,object>) scoreEntry;
            Dictionary<string,object> user = (Dictionary<string,object>) entry["user"];

            string name     = ((string) user["name"]).Split(new char[]{' '})[0] + "\n";
            string score     = "Smashed: " + entry["score"];

            boxRect = new Rect(0, 121+(TournamentStep*x)-scrollPosition.y , 100,128);
            // Background box
            GUI.Box(boxRect,"",MenuSkin.GetStyle("tournament_entry"));

            // Text
            GUI.Label (new Rect(24, 136 + (TournamentStep * x) - scrollPosition.y, 100,128), (x+1)+".", MenuSkin.GetStyle("tournament_position"));      // Rank e.g. "1.""
            GUI.Label (new Rect(250,145 + (TournamentStep * x) - scrollPosition.y, 300,100), name, MenuSkin.GetStyle("tournament_name"));               // name   
            GUI.Label (new Rect(250,193 + (TournamentStep * x) - scrollPosition.y, 300,50), score, MenuSkin.GetStyle("tournament_score"));              // score
            Texture picture;
            if (friendImages.TryGetValue((string) user["id"], out picture)) 
            {
                GUI.DrawTexture(new Rect(118,128+(TournamentStep*x)-scrollPosition.y,115,115), picture);  // Profile picture
            }
            x++;
        }

    }
    else GUI.Label (new Rect(180,270,512,200), "Loading...", MenuSkin.GetStyle("text_only"));

    // Record length so we know how far we can scroll to
    tournamentLength = boxRect.y + boxRect.height + scrollPosition.y;

    GUILayout.EndArea();
}


//  React to menu buttons  //


private void onPlayClicked()
{
    Util.Log("onPlayClicked");
    if (friends != null && friends.Count > 0)
    {
        // Select a random friend and get their picture
        Dictionary<string, string> friend = Util.RandomFriend(friends);
        GameStateManager.FriendName = friend["first_name"];
        GameStateManager.FriendID = friend["id"];
        GameStateManager.CelebFriend = -1;
        LoadPictureURL(friend["image_url"],FriendPictureCallback);
    }
    else
    {
        //We can't access friends
        GameStateManager.CelebFriend = UnityEngine.Random.Range(0,CelebTextures.Length - 1);
        GameStateManager.FriendName = CelebNames[GameStateManager.CelebFriend];
    }

    // Start the main game
    Application.LoadLevel("GameStage");
    GameStateManager.Instance.StartGame();
}

public void SetFullscreenMode (bool on)
{
    if (on)
    {
        Screen.SetResolution (Screen.currentResolution.width, Screen.currentResolution.height, true);
    }
    else
    {
        Screen.SetResolution ((int)CanvasSize.x, (int)CanvasSize.y, false);
    }
}

public static void FriendPictureCallback(Texture texture)
{
    GameStateManager.FriendTexture = texture;
}

delegate void LoadPictureCallback (Texture texture);


IEnumerator LoadPictureEnumerator(string url, LoadPictureCallback callback)    
{
    WWW www = new WWW(url);
    yield return www;
    callback(www.texture);
}
void LoadPictureAPI (string url, LoadPictureCallback callback)
{
    FB.API(url,Facebook.HttpMethod.GET,result =>
    {
        if (result.Error != null)
        {
            Util.LogError(result.Error);
            return;
        }

        var imageUrl = Util.DeserializePictureURLString(result.Text);

        StartCoroutine(LoadPictureEnumerator(imageUrl,callback));
    });
}
void LoadPictureURL (string url, LoadPictureCallback callback)
{
    StartCoroutine(LoadPictureEnumerator(url,callback));

}

void LoginCallback(FBResult result)
{
    Util.Log("LoginCallback");

    if (FB.IsLoggedIn)
    {
        OnLoggedIn();
    }
}

void OnLoggedIn()
{
    Util.Log("Logged in. ID: " + FB.UserId);

    // Reqest player info and profile picture                                                                           
    FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
    LoadPicture(Util.GetPictureURL("me", 128, 128), MyPictureCallback);
}

void APICallback(FBResult result)
{
    Util.Log("APICallback");
    if (result.Error != null)
    {
        Util.LogError(result.Error);
        // Let's just try again                                                                                                
        FB.API("/me?fields=id,first_name,friends.limit(100).fields(first_name,id)", Facebook.HttpMethod.GET, APICallback);
        return;
    }

    profile = Util.DeserializeJSONProfile(result.Text);
    GameStateManager.Username = profile["first_name"];
    friends = Util.DeserializeJSONFriends(result.Text);
}

void MyPictureCallback(Texture texture)
{
    Util.Log("MyPictureCallback");

    if (texture == null)
    {
        // Let's just try again
        LoadPicture(Util.GetPictureURL("me", 128, 128), MyPictureCallback);
        return;`
    }
    GameStateManager.UserTexture = texture;
}      
}

1 个答案:

答案 0 :(得分:-1)

更改单词&#34; LoadPicture&#34; for&#34; LoadPictureAPI&#34;。

供参考,请参阅: https://github.com/fbsamples/friendsmash-unity/blob/master/friendsmash_payments_start/Assets/Scripts/MainMenu.cs