GetPortraitSprite() by image name
Posted: Tue Oct 20, 2020 9:57 pm
In Actor.cs GetPortraitSprite() it picks a sprite either by pic=index or by DialogueManager.LoadAsset. However, it is reasonable to also check if the sprite was assigned in the editor rather than Resources. Using Resources is a bad idea in general because it causes that asset to load when the game starts and remains in memory forever. Picking by filename contains more information and is less likely to mess up than picking by index. I propose the following improvement:
Code: Select all
public Sprite GetPortraitSprite()
{
//--- Was: return UITools.GetSprite(portrait, spritePortrait);
//--- Instead, check for override set by SetPortrait():
var originalDebugLevel = DialogueDebug.level; // Suppress logging for Lua return Actor[].Current_Portrait.
DialogueDebug.level = DialogueDebug.DebugLevel.Warning;
string imageName = DialogueLua.GetActorField(Name, DialogueSystemFields.CurrentPortrait).asString;
DialogueDebug.level = originalDebugLevel;
if (string.IsNullOrEmpty(imageName))
{
return GetPortraitSprite(1);
}
else if (imageName.StartsWith("pic="))
{
return GetPortraitSprite(Tools.StringToInt(imageName.Substring("pic=".Length)));
}
else
{
// KevinJ:
for (int index = 0; index < alternatePortraits.Count; index++)
{
if (alternatePortraits[index] != null && string.Equals(alternatePortraits[index].name, imageName))
{
return UITools.GetSprite(alternatePortraits[index], null);
}
}
for (int index = 0; index < spritePortraits.Count; index++)
{
if (spritePortraits[index] != null && string.Equals(spritePortraits[index].name, imageName))
{
return UITools.GetSprite(null, spritePortraits[index]);
}
}
return UITools.CreateSprite(DialogueManager.LoadAsset(imageName) as Texture2D);
}
}