简体   繁体   中英

C# Accessors issue

I'm trying to pass a string between two classes inside Unity. Normally I'd make the string static and access it directly, however, I need this string to modifiable in Unities inspector and if I set it as static I can't do this.

I've used get and set once or twice before but it's been a while and I'm rusty. So, I've set my code up like this in the class where I'm setting the string:

public string pdfString;
private string pdfInstance;
public string PDF { get { return pdfInstance; } }

void Start()
    {
        UI.SetActive(false);
        pdfInstance = pdfString;
    }

Public pdfString is what I'm setting in the inspector. I created the private string because I thought the get value had an issue with passing a public string. I tested this, and the private returns the correct string.

Now, in the class I'm trying to pass the string over, I have done the following:

public string pdfFile = "";
private SetSelectedInfo pdf;
void Start ()
{
    pdf = new SetSelectedInfo();
}

// Update is called once per frame
void Update () {
    pdfFile = pdf.PDF;
}

public void OpenPDFFile()
{
    System.Diagnostics.Process.Start(Application.dataPath + "/PDFS/" + pdfFile);
}

When I debug out pdfFile value it returns a null. Can someone see what I've done wrong in setting this up?

The singleton pattern is powerful, but with great power comes great responsibility. It's way over used, especially in game development, so it should certainly be handled with care.

If the only reason you wanted to make it static was so that it could be accessed, let me provide you with an alternative way that utalizes Components and Unity's inspector window.

public class SetSelectInfo : MonoBehaviour 
{
     public string PDF;

     public void OpenPDFFile()
     {
         System.Diagnostics.Process.Start(Application.dataPath + "/PDFS/" + pdfFile);
     }
}

Now you can set the PDF string in your inspector. Then in any class that you want to have a reference to this, just expose it to the inspector as well, like so:

public AnyOtherClass : MonoBehavior
{
    public SetSelectInfo SelectInfo;

    void Start()
    {
         Debug.log(SelectInfo.PDF); // Do whatever you want here, or any other place in the script
    }
}

Once you attach this script to a GameObject in your game scene, and you have a GameObject with the SetSelectInfo script on it, you can drag the GameObject over into the slot on the inspector and link them together.

If that last part doesn't make sense to you, I can make some pictures and post them. But it really is jsut as simple as clicking the object then dragging the other object into the field space on the inspector (the same place you are setting the pdf string)

Personally i always try to avoid this with a GameObject for every Scene containing all Variables.

But a clearly cleaner Solution is the Singleton Pattern like the Wiki tells. It will make sure that no other Object will reference to it. If you're not sure how the Singleton Pattern work read Jon Skeets - Implementing of Singleton Pattern or the Wikipedia Topic about it.

The your code should look something like this:

public class SetSelectedInfo : Singleton<SetSelectedInfo> 
{        
    public string pdfString;
    private string pdfInstance;
    public string PDF { get { return pdfInstance; } }

    protected SetSelectedInfo () 
    {

    } 

    void Start()
    {
        UI.SetActive(false);
        pdfInstance = pdfString;
    }
}

And the Method:

public void OpenPDFFile()
{
    string file = SetSelectedInfo.Instance.pdfFile;
    System.Diagnostics.Process.Start(Application.dataPath + "/PDFS/" + file);
}  

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