This is similar to what I did with my personal nameplate addon. I modified it to duplicate the layout of your existing code. If you're running this on Classic, change the first hook from
AcquireUnitFrame to
OnNamePlateCreated.
Lua Code:
local TextObjects={};
hooksecurefunc(NamePlateDriverFrame,"AcquireUnitFrame",function(self,base)
if base:IsForbidden() then return; end
local uf=base.UnitFrame; if TextObjects[uf] then return; end
local healthtext=uf.healthBar:CreateFontString(nil,"OVERLAY",FontName);--"NumberFontNormalSmall");
healthtext:SetPoint("RIGHT",0,0);
healthtext:SetFont(STANDARD_TEXT_FONT,12,"OUTLINE");
TextObjects[uf]=healthtext;
end);
hooksecurefunc("CompactUnitFrame_UpdateHealth",function(self)
local healthtext=TextObjects[self]; if not healthtext then return; end
if UnitIsUnit(frame.displayedUnit,"player") or not UnitIsFriend("player",frame.displayedUnit) then
healthtext:SetFormattedText("%.0f%%",100*UnitHealth(self.displayedUnit)/UnitHealthMax(self.displayedUnit));
else healthtext:SetText(nil); end
end);