I’ve been messing with this for a few hour and I just cant seem to get what I am doing wrong here. I’ve read and searched for awhile on Unity Answers, the Forums, and MSDN: but I just can’t understand what is going on. Any help and tips would be extremely appreciated. Basically what the script is doing is downloading a list of images via WWW, and using another script to cache them or load them from the file.
If I take away the part referring to the instance, it tells me I am pointing to non-static methods, when they have static in their name.
The Complete error is:
This is the full FlagsCache file, about 90% of it is from Unity Answers.
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Text;
public class FlagsCache : MonoBehaviour {
static public FlagsCache instance;
void Awake()
{
instance = this;
}
static public WWW getCachedWWW(string url, string imgname)
{
string filePath = Application.persistentDataPath;
filePath += "/" + imgname;
string loadFilepath = filePath;
bool web = false;
WWW www;
bool useCached = false;
useCached = System.IO.File.Exists(filePath);
if (useCached)
{
//check how old
System.DateTime written = File.GetLastWriteTimeUtc(filePath);
System.DateTime now = System.DateTime.UtcNow;
double totalHours = now.Subtract(written).TotalHours;
if (totalHours > 300)
useCached = false;
}
if (System.IO.File.Exists(filePath))
{
string pathforwww = "file://" + loadFilepath;
Debug.Log("TRYING FROM CACHE " + url + " file " + pathforwww);
www = new WWW(pathforwww);
}
else
{
web = true;
www = new WWW(url);
}
instance.StartCoroutine(doLoad(www, filePath, web));
return www;
}
static IEnumerator doLoad(WWW www, string filePath, bool web)
{
yield return www;
if (www.error == null)
{
if (web)
{
//System.IO.Directory.GetFiles
Debug.Log("SAVING DOWNLOAD " + www.url + " to " + filePath);
// string fullPath = filePath;
File.WriteAllBytes(filePath, www.bytes);
Debug.Log("SAVING DONE " + www.url + " to " + filePath);
//Debug.Log("FILE ATTRIBUTES " + File.GetAttributes(filePath));
//if (File.Exists(fullPath))
// {
// Debug.Log("File.Exists " + fullPath);
// }
}
else
{
Debug.Log("SUCCESS CACHE LOAD OF " + www.url);
}
}
else
{
if (!web)
{
File.Delete(filePath);
}
Debug.Log("WWW ERROR " + www.error);
}
}
}
This is the one who calls the Cache file and tells them what to do. I wrote this one myself. I have been messing with them both for awhile trying to figure it out and learn a very rememberable lesson, but it just isn’t working out.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FlagsLoader : MonoBehaviour {
public Dropdown FlagOption;
int FlagsType;
public UnityEngine.UI.Button FlagPreview;
public GameObject FlagsChooser;
public string flagsURL;
string[] RetrievedFlags;
[SerializeField] Transform menuPanel;
[SerializeField] GameObject buttonPrefab;
public void StartFlags()
{
StartCoroutine(GetTheFlags());
}
//Fill Array with IEnumerator
public void GetFlags()
{
if (FlagsType == 0)
{
flagsURL = "http://site.com/WorldFlags/";
}
else
{
if (FlagsType == 1)
{
flagsURL = "http://site.com/CustomFlags/";
}
}
FlagsCache instance = new FlagsCache();
for (int i = 0; i < RetrievedFlags.Length; i++)
{
GameObject button = (GameObject)Instantiate(buttonPrefab);
string index = GetFlagData(RetrievedFlags[i], "Nation:");
string imgname = GetFlagData(RetrievedFlags[i], "URL:");
button.GetComponentInChildren<Text>().text = index;
button.GetComponent<Button>().onClick.AddListener(
() => { SetFlag(index, imgname); }
);
button.GetComponent<RawImage>().texture = FlagsCache.getCachedWWW(flagsURL + imgname, imgname).texture;
button.GetComponent<RawImage>().SetNativeSize();
button.transform.parent = menuPanel;
}
}
string GetFlagData(string Data, string Indexes)
{
string value = Data.Substring(Data.IndexOf(Indexes) + Indexes.Length);
if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
return value;
}
void SetFlag(string index, string flagurl)
{
RegistrationController.Flag = index;
FlagPreview.GetComponent<RawImage>().texture = FlagsCache.getCachedWWW(flagsURL + flagurl, flagurl).texture;
FlagsChooser.SetActive(false);
}
public void ChangeFlagsDisplay()
{
if (FlagOption.value == 0)
{
FlagsType = 0;
GetFlags();
} else
{
if (FlagOption.value == 1)
{
FlagsType = 1;
GetFlags();
}
}
}
IEnumerator GetTheFlags()
{
WWWForm FlagForm = new WWWForm();
FlagForm.AddField("type", FlagsType);
WWW GetFlagsData = new WWW("http://site.com//CustomFlags.php", FlagForm);
yield return GetFlagsData;
string GetFlagsDataString = GetFlagsData.text;
print(GetFlagsDataString);
RetrievedFlags = GetFlagsDataString.Split(';');
GetFlags();
}
}
Any help is extremely appreciated, and thank you so much for reading :).