So I'm trying to programmatically load Sprites into a struct I have and the code does not seem to be working as I expect. Here's what I have so far:
public class WeatherManager : Monobehaviour
{
#region Variables
private List<Weather> weatherList = new List<Weather>();
#endregion
...
public void Start()
{
InitialiseWeatherList();
}
...
private void InitialiseWeatherList()
{
if(weatherList.Count == 0)
{
weatherList.Add(new Weather(WeatherType.Cloudy, "Cloudy", "Weather_Cloudy"));
weatherList.Add(new Weather(WeatherType.PartlyCloudy, "Partly Cloudy", "Weather_PartlyCloudy"));
weatherList.Add(new Weather(WeatherType.Rainy, "Rainy", "Weather_Rainy"));
weatherList.Add(new Weather(WeatherType.Snowing, "Snowing", "Weather_Snowing"));
weatherList.Add(new Weather(WeatherType.Sunny, "Sunny", "Weather_Sunny"));
weatherList.Add(new Weather(WeatherType.Thunderstorm, "Thunderstorm", "Weather_Thunderstorm"));
StartCoroutine(LoadWeatherIcons());
}
}
private IEnumerator LoadWeatherIcons()
{
foreach(Weather weather in weatherList)
{
yield return weather.LoadWeatherIcon();
weather.SetWeatherIcon(weather.GetWeatherIcon());
}
foreach (Weather weather in weatherList)
{
if(weather.GetWeatherIcon() == null)
{
Debug.LogError($"Failed to load weather icon for {weather.GetWeatherName()}");
}
}
}
}
My Weather struct is as follows:
public struct Weather
{
private WeatherType weatherType;
private AssetReference weatherIconReference;
private Sprite weatherIcon;
private string weatherName;
private string weatherIconName;
public Weather(WeatherType weatherType, string weatherName, string weatherIconName)
{
this.weatherType = weatherType;
this.weatherIconReference = null;
this.weatherIcon = null;
this.weatherName = weatherName;
this.weatherIconName = weatherIconName;
}
public WeatherType GetWeatherType() { return weatherType; }
public Sprite GetWeatherIcon() { return weatherIcon; }
public string GetWeatherName() { return weatherName; }
public string GetWeatherIconName() { return weatherIconName; }
public void SetWeatherIcon(Sprite weatherIcon)
{
if(this.weatherIcon == null)
{
this.weatherIcon = weatherIcon;
}
else
{
Debug.LogError("Could not reassign weather icon.");
}
}
public IEnumerator LoadWeatherIcon()
{
AsyncOperationHandle<Sprite> loadOperation = Addressables.LoadAssetAsync<Sprite>(weatherIconName);
yield return loadOperation;
if(loadOperation.Status == AsyncOperationStatus.Succeeded)
{
weatherIcon = loadOperation.Result;
}
else
{
Debug.LogError($"Failed to load weather icon for {weatherName}");
}
}
}
}
However, when I run my project, I get the following errors:
Failed to load weather icon for Cloudy
UnityEngine.Debug:LogError (object)
...
For all of the entries within the weatherList. I think my Addressables Group
is set correctly for these assets. When I change the name of one of them, I get an error referring to the key not being correct, so it seems to be doing as it should.
However, when I look at my weatherList entries, I get Weather Icon None(Sprite)
which tells me that the weather icon is not assigned as I expect it to be.
I've definitely not got something correct here but because this is a new system to me, I'm not sure where it's going wrong.
Thanks!