There is an event specifically for updating portraits:
UNIT_PORTRAT_UPDATE
But, you do not need to implement all that stuff yourself. oUF already provides a Portrait element. You can easily use that, plus an override function to set your own texture:
Create the portrait, inside your frame's Spawn function:
Code:
local portrait = self:CreateTexture(nil, "ARTWORK")
portrait:SetPoint("LEFT", self, "RIGHT")
portrait:SetSize(64, 64)
portrait.Override = UpdatePortrait
self.Portrait = portrait
Then add an override function somewhere
before your Spawn function:
Code:
local function UpdatePortrait(self, event, unit)
if not unit or not UnitIsUnit(self.unit, unit) then return end
if not UnitIsPlayer(unit) then
-- This unit isn't a player.
-- Use a fallback texture, or just hide the portrait.
return
end
local _, class = UnitClass(unit)
local _, race = UnitRace(unit)
if not class or not race then
-- Class and/or race information is not available for some reason.
-- Use a fallback texture, or just hide the portrait.
return
end
local texture
if UnitSex(unit) == 3 then
-- Female
texture = "Interface\\AddOns\\oUF_HL2\\faces\\" .. class .. "-" .. race .. "-f-80.tga"
else
-- Male (or unknown)
texture = "Interface\\AddOns\\oUF_HL2\\faces\\" .. class .. "-" .. race .. "-m-80.tga"
end
self.Portrait:SetTexture(texture)
end
Some other observations:
(1) The first return from UnitRace is localized. You need to use the second return, which is locale-independent, so your addon works in all locales, not just your own. Rename your images accordingly. See
this page for a list of locale-independent race names to use.
(2) There's no need to convert the numeric return from UnitSex into a string. Just use the number directly and save yourself the table lookup.