简体   繁体   中英

How can I change an instance variable from within a static method?

I am implementing Unity3D ads into my Android game. I'm using a helper class that Unity provides to help manage different events.

If a user watches a video and completes it, I reward the user with play money(candy). This method is called after a video ad has been initialized:

private static void HandleShowResult (ShowResult result)
    {
        switch (result)
        {
        case ShowResult.Finished:
            Debug.Log("The ad was successfully shown.");

            break;
        case ShowResult.Skipped:
            Debug.Log("The ad was skipped before reaching the end.");
            break;
        case ShowResult.Failed:
            Debug.LogError("The ad failed to be shown.");
            break;
        }
    }

In the class comments it says I should customize this method to perform actions based on whether the ad was successfully shown or not.

If the ad was successfully shown I want to update the user's candy. Here is my CandyManager class that updates the candy that the user collects or earns in the game:

[System.Serializable]
public class CandyManger : MonoBehaviour {

    // START
    public Text candyBarText; 
    public Text candyBarTextShadow;

    // PLAY
    public Text candyBarText2; 
    public Text candyBarTextShadow2;

    public int candy; 

    void Update(){
        candyBarText.text = "" + candy;
        candyBarTextShadow.text = "" + candy;

        candyBarText2.text = "" + candy;
        candyBarTextShadow2.text = "" + candy;
    }
}

What I would like to do is be able to update the candy like this from within the static HandleShowResult():

candyManager.candy = candyManager.candy + 5;

"candyManager" is the current instance of the CandyManager class

If I can't update an instance variable from a static method, then how can I update it when a video is successfully shown?

First off, there is nothing stopping you from performing operations on an instanced object from a static method, in fact, if the static method is in a different class, there is no difference in what you need to do in order to perform the operation.

That requirement is simple, you need to have a reference to the instance of the object you wish to perform the operation on. The special thing about static is that it doesn't have access to the this reference/pointer regarding its class members, so you still need a reference to the instance for it to work, whereas a normal class member can just use this (usually implicitly).

So, find your object and get its CandyManager component, and call normally:

//In the static method
GameObject.Find("MyManagerObject").GetComponent<CandyManager>().UpdateCandy(value);

Or something like that. In other technologies, (or even in Unity) you can just pass the reference into the static method. As noted in the comments, the static class could also raise an event that CandyManager listens for.

To update an instance field from a static member, it is necessary to have the instance. One possible way is to store the CandyManager instance as a static property in you class with HandleShowResult :

public static class MyClass{
    private static void HandleShowResult (ShowResult result)
    {
        //...
        //Access to MyCandyManager
    }

    public static CandyManager MyCandyManager{get;set;}
}

But in this case, it is important that you have only one instance of CandyManager . With multiple instances is it more difficult to distinguish between them.

PS: It is also possible to store a static property in the CandyManager type. In this case, you use the singleton pattern .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM